-
Notifications
You must be signed in to change notification settings - Fork 1
/
speech.py
67 lines (54 loc) · 1.96 KB
/
speech.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
import random
import time
import speech_recognition as sr
# import appcopys
# def recognize_speech_from_mic(recognizer, microphone):
# if not isinstance(recognizer, sr.Recognizer):
# raise TypeError("`recognizer` must be `Recognizer` instance")
# if not isinstance(microphone, sr.Microphone):
# raise TypeError("`microphone` must be `Microphone` instance")
# 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 unavailable"
# except sr.UnknownValueError:
# response["error"] = "Unable to recognize speech"
# return response
# def start():
# recognizer = sr.Recognizer()
# microphone = sr.Microphone()
# for i in range(5):
# # for j in range(5):
# print('Speak!')
# guess = recognize_speech_from_mic(recognizer, microphone)
# print("I didn't catch that. What did you say?\n")
# if guess["error"]:
# print("ERROR: {}".format(guess["error"]))
# # show the user the transcription
# print("You said: {}".format(guess["transcription"]))
# if (guess["transcription"] == "gesture"):
# appcopy.gesture()
def start():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(source)
print("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
query= r.recognize_google(audio,language= 'en-in')
print(f"user said: {query}\n")
except Exception as e:
print("Couldn't recognize what you said, speak once more.")
return None
return query