-
Notifications
You must be signed in to change notification settings - Fork 1
/
voiceRecognition.py
31 lines (27 loc) · 1.13 KB
/
voiceRecognition.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
import speech_recognition as sr
# from this library, we are using Google Speech Recognition engine
# this requires pyaudio to be installed: pip3 install pyaudio
# variable for checking if the program did't cath the voice
correct = True
r = sr.Recognizer()
while correct:
text = ''
with sr.Microphone() as source:
# listen for 1 second to calibrate the energy threshold for ambient noise levels
r.adjust_for_ambient_noise(source)
print('Ask your question:')
# read the audio data from the default microphone, will record for 10 seconds
# audio_data = r.record(source, duration=10)
print("Listening...")
audio_data = r.listen(source)
# convert speech to text
try:
text = r.recognize_google(audio_data)
print("You said: " + text)
is_correct = input('Is this correct? (y/n): ')
if is_correct == 'y':
correct = False
except:
print("Sorry, I didn't get that")
# except sr.RequestError as e:
#print("Could not request results from Google Speech Recognition service; {0}".format(e))