ReSpeaker provides connectivity with the Microsoft Bing Speech API. Using this library, we can apply speech recognition such as speech-to-text. For further information about the Microsoft Bing Speech API, you can visit at https://azure.microsoft.com/en-us/services/cognitive-services/speech/.
To use the Microsoft Bing Speech API library, you should register and obtain an API key. Microsoft provides trial access to use. The API key can be found on your dashboard page of the Microsoft Bing Speech API. You can see it here:
Now we can write a Python program to use the Microsoft Bing Speech API:
import logging import time from threading import Thread, Event from respeaker import Microphone from respeaker.bing_speech_api import BingSpeechAPI # get a key from https://www.microsoft.com/cognitive-services/en-us/speech-api BING_KEY = '<--bing speech api-->' def task(quit_event): mic = Microphone(quit_event=quit_event) bing = BingSpeechAPI(key=BING_KEY) while not quit_event.is_set(): if mic.wakeup('respeaker'): print('Wake up') data = mic.listen() try: text = bing.recognize(data) if text: print('Recognized %s' % text) except Exception as e: print(e.message) def main(): print('ReSpeaker is running....') logging.basicConfig(level=logging.DEBUG) quit_event = Event() thread = Thread(target=task, args=(quit_event,)) thread.start() while True: try: time.sleep(1) except KeyboardInterrupt: print('Quit') quit_event.set() break thread.join() if __name__ == '__main__': main()
Save this program into a file called ch05_respeaker.py.
To run this program, you can type this command:
$ python ch05_respeaker.py
Since this program uses the Microsoft Bing Speech API, your ReSpeaker should be connected to the internet.
After running it, say respeaker until the program wakes up. Then, say something so the program converts speech to text. Speak slowly to make sure ReSpeaker can recognize your voice.