-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.py
46 lines (36 loc) · 1 KB
/
voice.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
import speech_recognition as sr
import json
def record_sentence():
r = sr.Recognizer()
m = sr.Microphone()
try:
with m as source:
r.adjust_for_ambient_noise(source)
with m as source:
audio = r.listen(source)
try:
value = r.recognize_google(audio)
return value
except sr.UnknownValueError:
return "Error"
except sr.RequestError as e:
return "Google Speech Recognition Service Down"
except:
pass
return value
def process_sentence():
classes = None
with open('classes.json') as json_file:
classes = json.load(json_file)
sentence = record_sentence()
sentence = sentence.split()
keywords = []
for word in sentence:
for key in classes.keys():
if word in classes[key]:
keywords.append(word)
continue
keywords = list(set(keywords))
return keywords
keywords = process_sentence()
print(keywords)