-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechToText.py
57 lines (48 loc) · 1.66 KB
/
speechToText.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
53
54
55
56
57
import speech_recognition as sr
import urllib3
import pocketsphinx # used for offline speech recognisition, method called by recognizer instance of speech_recognition had to install it even when speechrecognition is not used
# check internet connection
def internet_on():
"""
check internet connection
@return: bool
"""
try:
http = urllib3.PoolManager()
http.request('GET', 'http://www.google.com')
return True
except:
return False
# obtain audio from the microphone
r = sr.Recognizer()
# listens through microphone and returns string
def listen_input():
"""
listens through microphone returns audio as text string
@param: NONE
@return: str
"""
print("Listening...")
with sr.Microphone() as source:
audio = r.listen(source)
try:
if not internet_on():
# offline mode
return r.recognize_sphinx(audio)
if internet_on():
text = r.recognize_google(audio,language="en-in")
# name by which audio file will be saved
fileName = text.replace(" ", "_")
#save listened audio in speechToText folder in mp3 file with name same as audioToText
#with open("speechToText/"+fileName+".mp3", "wb") as f:
# f.write(audio.get_wav_data())
# return audioToText
#print(text) #"You:>> "
return str(text)
except Exception as e:
#print(e)
#print("\n"+"me<<: pardon, please...")
return "NONE"
if __name__ == "__main__":
ans = listen_input()
print(ans)