forked from Kitt-AI/snowboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import snowboydecoder | ||
import sys | ||
import signal | ||
import speech_recognition as sr | ||
import os | ||
|
||
""" | ||
This demo file shows you how to use the new_message_callback to interact with | ||
the recorded audio after a keyword is spoken. It leverages the | ||
speech_recognition library in order to convert the recorded audio into text. | ||
""" | ||
|
||
|
||
interrupted = False | ||
|
||
|
||
def newMessageCallback(fname): | ||
print("converting audio to text") | ||
r = sr.Recognizer() | ||
with sr.AudioFile(fname) as source: | ||
audio = r.record(source) # read the entire audio file | ||
# recognize speech 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(r.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)) | ||
|
||
os.remove(fname) | ||
|
||
|
||
|
||
def detectedCallback(): | ||
print('recording audio...', end='', flush=True) | ||
|
||
def signal_handler(signal, frame): | ||
global interrupted | ||
interrupted = True | ||
|
||
|
||
def interrupt_callback(): | ||
global interrupted | ||
return interrupted | ||
|
||
if len(sys.argv) == 1: | ||
print("Error: need to specify model name") | ||
print("Usage: python demo.py your.model") | ||
sys.exit(-1) | ||
|
||
model = sys.argv[1] | ||
|
||
# capture SIGINT signal, e.g., Ctrl+C | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.38) | ||
print('Listening... Press Ctrl+C to exit') | ||
|
||
# main loop | ||
detector.start(detected_callback=detectedCallback, new_message_callback=newMessageCallback, interrupt_check=interrupt_callback, sleep_time=0.01) | ||
|
||
detector.terminate() | ||
|
||
|
||
|
||
|