-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (150 loc) · 4.79 KB
/
main.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import snowboydecoder
import sys
import signal
import speech_recognition as sr
import pyaudio
import wave
from tts import speak
from youtube import get_youtube
from webpusher import push
import requests as rqst
from wiki import do_wiki as wiki
from imagesearch import find_image
from music.musicyt import play_music
from news import get_news
from compliments import do
import weather
from houndify_client import hound
import time
from pygame import mixer
import os
#from picture import click_picture
from threading import Thread
def find(name):
for root, dirs, files in os.walk('.'):
if name in files:
return True
search_music = "yo yo"
mixer.init()
d = {}
i = 0
dong = "music/dong.wav"
def music_play(search_music):
if find(search_music +".wav") == True:
mixer.music.load("music/" + search_music + '.wav')
mixer.music.play()
elif find(search_music +".wav") != True:
print("music reach")
play_music(search_music)
mixer.music.load("music/" + search_music + '.wav')
mixer.music.play()
def listen():
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 4
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
os.system("aplay " + dong)
print("record0ing...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
r = sr.Recognizer()
harvard = sr.AudioFile('file.wav')
with harvard as source:
audio = r.record(source)
type(audio)
text = r.recognize_google(audio)
return text
def action(text):
if text.find("YouTube") != -1:
search_term = text[text.index("play") + 4: text.index("on YouTube")]
got, dur = get_youtube(search_term)
list =[got , dur]
speak("playing" + search_term + "on youtube")
push("youtube", list)
elif text.find("turn on the lights") != -1:
rqst.get("http://192.168.1.150:91/?socket=1On")
speak("turning on lights")
elif text.find("turn off the lights") != -1:
rqst.get("http://192.168.1.150:91/?socket=1Off")
speak("turning off lights")
elif text.find("tell me something about") != -1:
search_wiki = text[text.index("tell me something about ") + len("tell me something about"):]
got_wiki = wiki(search_wiki)
img = find_image(search_wiki)
to_send=[got_wiki, img]
push("wiki", to_send)
speak(got_wiki)
elif text.find("show me a picture of") != -1:
search_image = text[text.index("show me a picture of") + len("show me a picture of"):].replace(" ", "")
print(search_image)
push("images", find_image(search_image))
speak("ok! heres how" + search_image + "looks ")
elif text.find("click a picture ") != -1:
#click_picture()
pass
elif text.find("play") != -1:
global search_music
search_music = text[text.index("play") + len("play"):]
speak("playing" + search_music)
push("music", search_music)
music_play(search_music)
got, dur = get_youtube(search_music)
time.sleep(dur)
elif text.find("stop") != -1:
mixer.music.pause()
push("stop", "")
elif text.find("continue") != -1:
mixer.music.unpause()
else:
got = hound(text)
push("text", got)
speak(got)
def start():
text = listen()
action(text)
interrupted = False
def hotword_detected():
global i
d["thread{0}".format(i)] = Thread(target=start)
d["thread{0}".format(i)].start()
i = i + 1
def signal_handler(signal, frame):
global interrupted
interrupted = True
def interrupt_callback():
global interrupted
return interrupted
if len(sys.argv) == 1:
print("Error: need to specify model name")
print("Usage: python demo.py your.model")
sys.exit(-1)
model = sys.argv[1]
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
print('Listening... Press Ctrl+C to exit')
# main loop
detector.start(detected_callback=hotword_detected,
interrupt_check=interrupt_callback,
sleep_time=0.03)
detector.terminate()