-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromeo.py
More file actions
51 lines (41 loc) · 1.59 KB
/
romeo.py
File metadata and controls
51 lines (41 loc) · 1.59 KB
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
import random
import time
import speech_recognition as sr
def recognize_from_mic(recognizer,microphone):
if not isinstance(recognizer,sr.Recognizer):
raise TypeError("`recognizer` must be an instance of `Recognizer`")
if not isinstance(microphone,sr.Microphone):
raise TypeError("`microphone` must be ab instance of `Microphone`")
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio=recognizer.listen(source)
response = {
"success": True,
"error": None,
"transcription": None
}
try:
response["transcription"]=recognizer.recognize_google(audio)
except sr.RequestError:
response["success"]=False
response["error"]="API not available"
except sr.UnknownValueError:
response["error"]="Unable to recognize Speech"
return response
if __name__=="__main__":
prompt_limit=5
recognizer=sr.Recognizer()
microphone=sr.Microphone()
print("Hey there!\nMy name is Romeo and I have a special talent. I can memeorize any phone number at once.")
time.sleep(3)
for i in range(prompt_limit):
print('\nTry me B-)')
guess = recognize_from_mic(recognizer, microphone)
if guess["transcription"]:
break
if not guess["success"]:
break
print("I didn't catch that. What did you say?\n")
if guess["error"]:
print("ERROR: {}".format(guess["error"]))
print("Well, as I told you... \nI'm too good at this.\nThe number is: {} \nThanks for the number BTW ;)".format(guess["transcription"]))