-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseAudioListener.py
67 lines (59 loc) · 2.16 KB
/
BaseAudioListener.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
58
59
60
61
62
63
64
65
66
67
from urllib.request import urlopen
from speech_recognition import Recognizer, Microphone, UnknownValueError, RequestError
import pyaudio as pa
import requestAPI
import random
from threading import Thread
from time import sleep
def randomStringId():
string=''
for i in range(5):
string+=chr(random.randint(50,100))
return string
def checkInternet():
try:
url=urlopen('http://www.google.com/')
return True
except:
return False
def coderunner():
print('CODERUNNER STARTED\n')
sleep(50)
ID = randomStringId()
mic = Recognizer()
print()
print('############################################################')
print("Chatbot is live, say something...")
print('############################################################')
print()
with Microphone() as source:
while(True):
try:
audio = mic.listen(source, timeout=3)
except:
continue
print("\n")
if checkInternet():
try:
decodedAudio = mic.recognize_google(audio)
print("--> "+decodedAudio)
requestAPI.messageBot(ID,decodedAudio)
except UnknownValueError:
print("\nGoogle Speech Recognition could not understand audio\n")
except RequestError as e:
print("\nCould not request results from Google Speech Recognition service; {0}\n".format(e))
else:
print("You are offline!")
print("Fetching offline results...")
try:
decodedAudio = mic.recognize_sphinx(audio)
requestAPI.messageBot(ID,decodedAudio)
except UnknownValueError:
print("Sphinx Speech Recognition could not understand audio")
except RequestError as e:
print("Could not request results from Sphinx Speech Recognition service; {0}".format(e))
def main():
Thread(target=requestAPI.rasaStart).start()
Thread(target=coderunner).start()
if __name__=='__main__':
main()