-
Notifications
You must be signed in to change notification settings - Fork 0
/
s2t.py
52 lines (41 loc) · 1.47 KB
/
s2t.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import time
import speech_recognition as sr
import pyaudio
energy_threshold = 1000
sample_rate = 44100
chunk_size = 512
r = sr.Recognizer()
r.energy_threshold = energy_threshold # amplitude level to pick up
def recog():
with sr.Microphone(sample_rate = sample_rate, chunk_size = chunk_size) as source: #instance and chunck size to remove clicking sound
r.adjust_for_ambient_noise(source) # listen for 1 second to calibrate the energy threshold for ambient noise levels
print("Listening...")
audio = r.listen(source)
with open("/home/pi/Desktop/project/sample.wav",'wb') as sample:
sample.write(audio.get_wav_data())
print("Uploading...")
try:
return(r.recognize_google(audio,language="en-IN"))
except LookupError: # speech is unintelligible
print("speech is unintelligible")
return None
except sr.UnknownValueError:
print("!!")#Uploading Failed
return None
except sr.RequestError as e:
print("{0}".format(e))
return None
except:
print("!!!")#Dont talk nonsense
return None
def recog_to_file(fname):
print("Writing to file: "+fname)
newmsg = recog()
if newmsg is not None:
with open(fname,'a') as txtf:
txtf.write(newmsg+" ")
def main():
while True:
recog_to_file("/home/pi/Desktop/project/recogtxt.txt")
if __name__ == "__main__":
main()