-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt1.py
162 lines (121 loc) · 3.97 KB
/
stt1.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
import snowboydecoder
import sys
import signal
from pygame import mixer
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 import play_music,pause_music
#from compliments import do
from news import get_news
import threading
import time
search_music = ""
mixer.music.load("/home/pi/snowboy/resources/ding.wav")
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)
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
class worker(threading.Thread):
def run(self):
t1 = threading.Thread(target=play_music(search_music))
t1.start()
got, dur = get_youtube(search_music)
time.sleep(dur)
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 lights") != -1:
rqst.get("http://192.168.1.150:91/?socket=1On")
speak("turning on lights")
elif text.find("stop") != -1:
pause_music()
push("youtube","stop")
speak("ok")
elif text.find("turn off 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)
push("wiki", got_wiki)
speak(got_wiki)
elif text.find("play") != 1:
global search_music
search_music = text[text.index("play") + len("play"):]
speak("playing" + search_music)
worker().start()
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 ")
interrupted = False
def hotword_detected():
mixer.music.play()
text = listen()
try:
action(text)
except:
speak("Sorry I don't know that one!")
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()