Skip to content

Instantly share code, notes, and snippets.

@AdroitAnandAI
Last active September 20, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdroitAnandAI/3f78ed91ec73a20f0abf45ff52c2090b to your computer and use it in GitHub Desktop.
Save AdroitAnandAI/3f78ed91ec73a20f0abf45ff52c2090b to your computer and use it in GitHub Desktop.
Speech Recognition Thread
# Speech Recognition Thread identifies the command from defined synonyms and inserting to stt Queue
def stopStream(stream_reader):
if stream_reader:
stream_reader.stop_stream()
stream_reader = None
def received_frames(frames, speech, stt):
speech.push_data(frames, finish_processing=False)
utt_text, is_stable = speech.get_result()
rh_result = utt_text.decode("utf-8").strip().lower()
if (len(rh_result) > 0):
stt.put(rh_result)
def load_device():
"""Reload audio device list"""
device_list, default_input_index, loopback_index = \
audio_helper.get_input_device_list()
if not device_list:
print("No audio devices available")
return device_list
# Function to search for match with any of the defined synonyms of each command
def detectSoundEvent(utterance, controls, control_syn):
utters = utterance.split(' ')[-3:]
utters.reverse()
print(utters)
for utter in utters:
for control in controls:
synonyms = control_syn.get(control)
for synonym in synonyms:
if synonym in utter:
print('Event Trigger: ' + control)
return control, utters[-1]
return None, utters[0] #last word will return as reversed
#####################################################################
# Initializing the Speech Recognition Thread
#####################################################################
# You can add more controls as you deem fit.
numbers = ['zero', 'one', 'two', 'three', 'four', \
'five', 'six', 'seven', 'eight', 'nine']
controls = ['left', 'right', 'up', 'down']
control_syn = {}
for control in controls:
control_syn.setdefault(control, [])
# Need to account for similar sounding words as speech recog is on the edge!
control_syn['left'].extend(['let', 'left', 'light', 'live', 'laugh'])
control_syn['right'].extend(['right', 'write', 'great', 'fight', 'might', 'ride'])
control_syn['up'].extend(['up', 'hop', 'hope', 'out'])
control_syn['down'].extend(['down', 'doubt', 'though'])
device_list = load_device()
stream_reader = audio_helper.StreamReader(
device_list[1][0], received_frames)
if not stream_reader.initialize():
print("Failed to initialize Stream Reader")
speech.close()
speech = None
return
speech = SpeechManager()
print('speech config = ' + str(SPEECH_CONFIG))
if not speech.initialize(SPEECH_CONFIG,
infer_device='CPU',
batch_size=8):
print("Failed to initialize ASR recognizer")
speech.close()
speech = None
return
stt = Queue()
prevUtterance = ''
reading_thread = Thread(target=stream_reader.read_stream, \
args=(speech, stt), daemon=True)
reading_thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment