5.7 KiB
Speech to text - Raspberry Pi
For dis part of di lesson, you go write code wey go change di speech wey dey di audio wey you capture to text using di speech service.
Send di audio go di speech service
You fit send di audio go di speech service using di REST API. To use di speech service, first you go need request access token, then use dat token take access di REST API. Dis access tokens dey expire after 10 minutes, so your code suppose dey request dem regularly to make sure say dem dey always up to date.
Task - get access token
-
Open di
smart-timerproject for your Pi. -
Comot di
play_audiofunction. You no need am again because you no want make di smart timer dey repeat wetin you talk. -
Add dis import for di top of di
app.pyfile:import requests -
Add dis code above di
while Trueloop to set some settings for di speech service:speech_api_key = '<key>' location = '<location>' language = '<language>'Change
<key>to di API key for your speech service resource. Change<location>to di location wey you use when you create di speech service resource.Change
<language>to di locale name for di language wey you go dey speak, likeen-GBfor English, orzn-HKfor Cantonese. You fit find list of di supported languages and dia locale names for di Language and voice support documentation on Microsoft docs. -
Under dis one, add dis function to get access token:
def get_access_token(): headers = { 'Ocp-Apim-Subscription-Key': speech_api_key } token_endpoint = f'https://{location}.api.cognitive.microsoft.com/sts/v1.0/issuetoken' response = requests.post(token_endpoint, headers=headers) return str(response.text)Dis one dey call di token issuing endpoint, e dey pass di API key as header. Di call go return access token wey you fit use call di speech services.
-
Under dis one, declare function wey go change di speech wey dey di audio wey you capture to text using di REST API:
def convert_speech_to_text(buffer): -
Inside dis function, set up di REST API URL and headers:
url = f'https://{location}.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1' headers = { 'Authorization': 'Bearer ' + get_access_token(), 'Content-Type': f'audio/wav; codecs=audio/pcm; samplerate={rate}', 'Accept': 'application/json;text/xml' } params = { 'language': language }Dis one dey build URL using di location of di speech services resource. E go then put di headers with di access token wey come from di
get_access_tokenfunction, plus di sample rate wey dem use capture di audio. Finally, e go define some parameters wey go follow di URL wey dey contain di language wey dey di audio. -
Under dis one, add dis code to call di REST API and get di text back:
response = requests.post(url, headers=headers, params=params, data=buffer) response_json = response.json() if response_json['RecognitionStatus'] == 'Success': return response_json['DisplayText'] else: return ''Dis one dey call di URL and decode di JSON value wey dey di response. Di
RecognitionStatusvalue wey dey di response go show if di call fit change di speech to text well, and if e beSuccess, di text go return from di function, but if e no beSuccess, e go return empty string. -
Above di
while True:loop, define function wey go process di text wey di speech to text service return. Dis function go just print di text for di console for now.def process_text(text): print(text) -
Finally, change di call to
play_audiofor diwhile Trueloop to call diconvert_speech_to_textfunction, and pass di text to diprocess_textfunction:text = convert_speech_to_text(buffer) process_text(text) -
Run di code. Press di button and talk for di microphone. Release di button when you don finish, and di audio go change to text and e go print for di console.
pi@raspberrypi:~/smart-timer $ python3 app.py Hello world. Welcome to IoT for beginners.Try different types of sentences, plus sentences wey words dey sound alike but get different meanings. For example, if you dey speak English, talk 'I want to buy two bananas and an apple too', and notice how e go use di correct to, two and too based on di context of di word, no be just di sound.
💁 You fit find dis code for di code-speech-to-text/pi folder.
😀 Your speech to text program work well!
Disclaimer:
Dis dokyument don use AI translation service Co-op Translator do di translation. Even as we dey try make am accurate, abeg sabi say automated translations fit get mistake or no dey correct well. Di original dokyument for im native language na di main source wey you go fit trust. For important information, e better make professional human translation dey use. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.