-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update GitHub issue template, add threaded workers example, update do…
…cumentation for listen_in_background
- Loading branch information
Showing
5 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
#!/usr/bin/env python3 | |||
|
|||
# NOTE: this example requires PyAudio because it uses the Microphone class | |||
|
|||
from queue import Queue | |||
|
|||
import speech_recognition as sr | |||
|
|||
|
|||
r = sr.Recognizer() | |||
audio_queue = Queue() | |||
|
|||
def recognize_worker(): | |||
# this runs in a background thread | |||
while True: | |||
audio = audio_queue.get() # retrieve the next audio processing job from the main thread | |||
if audio is None: break # stop processing if the main thread is done | |||
|
|||
# received audio data, now we'll recognize it using Google Speech Recognition | |||
try: | |||
# for testing purposes, we're just using the default API key | |||
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` | |||
# instead of `r.recognize_google(audio)` | |||
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio)) | |||
except sr.UnknownValueError: | |||
print("Google Speech Recognition could not understand audio") | |||
except sr.RequestError as e: | |||
print("Could not request results from Google Speech Recognition service; {0}".format(e)) | |||
|
|||
audio_queue.task_done() # mark the audio processing job as completed in the queue | |||
|
|||
# start a new thread to recognize audio, while this thread focuses on listening | |||
recognize_thread = threading.Thread(target=recognize_worker) | |||
recognize_thread.daemon = True | |||
recognize_thread.start() | |||
with sr.Microphone() as source: | |||
try: | |||
while True: # repeatedly listen for phrases and put the resulting audio on the audio processing job queue | |||
audio_queue.put(r.listen(source)) | |||
except KeyboardInterrupt: # allow Ctrl + C to shut down the program | |||
pass | |||
|
|||
audio_queue.join() # block until all current audio processing jobs are done | |||
audio_queue.put(None) # tell the recognize_thread to stop | |||
recognize_thread.join() # wait for the recognize_thread to actually stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters