-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.py
51 lines (41 loc) · 1.86 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
47
48
49
50
51
#!/usr/bin/env python3
# NOTE: this example requires PyAudio because it uses the Microphone class
# Graham Hughes Nov 11, 2016
import speech_recognition as sr
import os
# obtain audio from the microphone
r = sr.Recognizer()
r.pause_threshold = 0.5
#r.energy_threshold = 3000 # Adjust to ambient noise (this is high, requires personal mic)
r.energy_threshold = 3500
def send_words():
words = ""
with sr.Microphone() as source:
print(">>>")
try:
audio = r.listen(source, timeout = 3)
print("done recording")
except:
return None
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
words = r.recognize_google(audio)
return words
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))
# if not words:
# # recognize speech using IBM Speech to Text
# IBM_USERNAME = "11702311-ff64-438e-98d5-c911a38d826d" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
# IBM_PASSWORD = "PXss8FokhHZ4" # IBM Speech to Text passwords are mixed-case alphanumeric strings
# try:
# words = r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD)
# return words
# except sr.UnknownValueError:
# print("IBM Speech to Text could not understand audio")
# except sr.RequestError as e:
# print("Could not request results from IBM Speech to Text service; {0}".format(e))