-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechToText.py
More file actions
37 lines (31 loc) · 1.61 KB
/
SpeechToText.py
File metadata and controls
37 lines (31 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import speech_recognition as sr
import logging as L
import Settings
import VoiceHAB as VH
import Messages as M
def SpeechToText(UseOfflineSpeechRecognition = False):
with VH.Mic as SpeechSource:
SpeechAudio = VH.Rec.listen(SpeechSource)
RecognizedSpeech = ''
SpeechToTextEngine = Settings.SpeechToTextEngine.lower()
try:
if UseOfflineSpeechRecognition:
L.debug('Using Sphinx as offline speech recognition engine')
RecognizedSpeech = VH.Rec.recognize_sphinx(SpeechAudio)
else:
if SpeechToTextEngine == 'bing':
RecognizedSpeech = VH.Rec.recognize_bing(SpeechAudio, Settings.BingSpeechAPIKey)
elif SpeechToTextEngine == 'apiai':
RecognizedSpeech = VH.Rec.recognize_api(SpeechAudio, client_access_token = Settings.ApiAIClientAccessToken)
elif SpeechToTextEngine == 'witai':
RecognizedSpeech = VH.Rec.recognize_wit(SpeechAudio, key = Settings.WitAIClientAccessToken)
elif SpeechToTextEngine == 'sphinx':
RecognizedSpeech = VH.Rec.recognize_sphinx(SpeechAudio)
else:
RecognizedSpeech = VH.Rec.recognize_google(SpeechAudio)
L.debug('Spoken phrase: %s' % RecognizedSpeech)
except sr.UnknownValueError:
pass
except sr.RequestError:
M.ProcessMessage('%s Speech Recognition denied request for result' % Settings.SpeechToTextEngine)
return RecognizedSpeech