-
Notifications
You must be signed in to change notification settings - Fork 19
/
speech_recognition.py
71 lines (58 loc) · 2.1 KB
/
speech_recognition.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
import speech_recognition as sr
import time, datetime
import pyaudio
from piarm import PiArm
import re
from time import sleep
robo = PiArm()
robo.connect('/dev/ttyS0')
r = sr.Recognizer()
r.energy_threshold = 4000
sample_rate = 48000
chunk_size = 1024
pos = [[472, 497, 306, 478, 484, 570],
[471, 499,303 ,739, 486, 571],
[472 ,499,206 ,739 ,421, 576],
[579, 499 ,207, 739 ,408, 576],
[579, 499 ,206, 739 ,533, 576],
]
pos1 = [[579, 499 ,206 ,748 ,555, 944],
[579, 499 ,207 ,748 ,413, 944],
[498, 499 ,207 ,748 ,420, 944],
[498, 499 ,208 ,748 ,575, 944],
[496, 499 ,206 ,749 ,614 ,575]]
mic_list = sr.Microphone.list_microphone_names()
print(mic_list)
device_id = 0
for i in mic_list:
if i == 'Yeti Stereo Microphone: USB Audio (hw:1,0)':
device_id = mic_list.index(i)
while True:
with sr.Microphone(device_index = device_id, sample_rate = sample_rate,chunk_size = chunk_size) as source:
r.adjust_for_ambient_noise(source)
print("Say Something")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
text = text.lower()
print("you said: " + text)
if re.search("pic", text):
if re.search('item', text):
print("pick item")
for command in pos:
for ID in range(6):
robo.servoWrite(ID + 1, command[ID], 500)
sleep(1)
elif re.search("place", text):
print("Place item")
for command in pos1:
for ID in range(6):
robo.servoWrite(ID + 1, command[ID], 500)
sleep(1)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
except AssertionError or KeyboardInterrupt:
print("Problem with Audio Source")
break