-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspeech.py
37 lines (27 loc) · 908 Bytes
/
speech.py
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
import speech_recognition as sr
class sTT:
def __init__(self):
self.mic_name = 'default'
self.sampling_rate = 48000
self.chunk_size = 2048
self.r = sr.Recognizer()
self.deviceID = None
def getDeviceID(self):
mic_list = sr.Microphone.list_microphone_names()
for i, microphone_name in enumerate(mic_list):
if microphone_name == self.mic_name:
deviceID = i
self.deviceID = deviceID
def getInput(self, field):
print(field)
with sr.Microphone(device_index = self.deviceID, sample_rate = self.sampling_rate, chunk_size = self.chunk_size) as source:
self.r.adjust_for_ambient_noise(source)
print("listening....")
audio = self.r.listen(source)
try:
text = self.r.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Pardon...")
except sr.RequestError as e:
print("Could not request results from server;{0}".format(e))