forked from Bryce-Ault/Measuring-Speech
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecognizers.py
More file actions
192 lines (153 loc) · 7.61 KB
/
Copy pathrecognizers.py
File metadata and controls
192 lines (153 loc) · 7.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
MIT License
Copyright (c) 2018 Michael Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import speech_recognition as sp_rec
from environment import CREDS as creds
from logger import LOGGER as log
# Sphinx Recognizer, Free to use but only decent at recognizing
def sphinx(rec, audio):
"""
Use sphinx to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_sphinx(audio)
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Sphinx could not understand audio'
log.error('Sphinx couldn\'t understand audio')
except sp_rec.RequestError as exp:
meta['error'] = 'Sphinx error; {0}'.format(exp)
log.error('Sphinx error; {0}'.format(exp))
return meta
def google(rec, audio):
"""
Use GOOGLE API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_google(audio) # TODO: this needs to be updated
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Google Speech Recognizer could not understand audio'
log.error('Google Speech Recognizer couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from Google Speech Recognition service; {0}'.format(exc)
log.error('Could not request results from Google Speech Recognition service; {0}'.format(exc))
return meta
def google_sound_cloud(rec, audio):
"""
Use GOOGLE SOUND CLOUD API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_google_cloud(audio, credentials_json=creds['GOOGLE_CLOUD_SPEECH'])
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Google Cloud Speech could not understand audio'
log.error('Google Cloud Speech couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from Google Cloud Speech service; {0}'.format(exc)
log.error('Could not request results from Google Cloud speech Recognition service; {0}'.format(exc))
return meta
def wit(rec, audio):
"""
Use WIT API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_wit(audio, key=creds['WIT_AI_KEY'])
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Wit.ai could not understand audio'
log.error('Wit.ai couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from Wit.ai service; {0}'.format(exc)
log.error('Could not request results from Wit.ai service; {0}'.format(exc))
return meta
def bing(rec, audio):
"""
Use Bing API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_bing(audio, key=creds['BING_KEY'])
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Microsoft Bing Voice Recognition could not understand audio'
log.error('Microsoft Bing Voice Recognition couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from Microsoft Bing Voice Recognition service; {0}'.format(exc)
log.error('Could not request results from Microsoft Bing Voice Recognition service; {0}'.format(exc))
return meta
def houndify(rec, audio):
"""
Use Houndify API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_houndify(audio,
client_id=creds['HOUNDIFY_CLIENT_ID'],
client_key=creds['HOUNDIFY_CLIENT_KEY'])
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'Houndify could not understand audio'
log.error('Houndify couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from Houndify service; {0}'.format(exc)
log.error('Could not request results from Houndify service; {0}'.format(exc))
return meta
def ibm(rec, audio):
"""
Use IBM API to recognize the natural language.
:rec: speech_recognition.Recognizer() The speech recognition engine.
:audio: speech_recognition.AudioData() The audio from the end user.
:returns: dict() meta-information: text transcipt
"""
meta = dict({})
try:
phrase = rec.recognize_ibm(audio,
username=creds['IBM_USERNAME'],
password=creds['IBM_PASSWORD'])
meta['text'] = phrase.split()
except sp_rec.UnknownValueError:
meta['error'] = 'IBM Speech to Text could not understand audio'
log.error('IBM Speech to Text couldn\'t understand audio')
except sp_rec.RequestError as exc:
meta['error'] = 'Could not request results from IBM Speech to Text service; {0}'.format(exc)
log.error('Could not request results from IBM Speech to Text service; {0}'.format(exc))
return meta