-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechRecognition.py
More file actions
31 lines (24 loc) · 904 Bytes
/
speechRecognition.py
File metadata and controls
31 lines (24 loc) · 904 Bytes
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
# Listen to the microphone and tries to recognize any words the user says
import speech_recognition as sr
"""Followed this documentation to record and recognize audio
https://pypi.org/project/SpeechRecognition/2.1.3/"""
def recognizeSpeech():
r = sr.Recognizer()
mic = sr.Microphone(device_index=0)
# Adjusts the recognizer sensitivity for ambient noise and records audio
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
response = ""
# Try recognizing the speech in the recording
try:
response = r.recognize_google(audio)
except sr.RequestError:
# API was unreachable or unresponsive
response = None
print("API unavailable")
except sr.UnknownValueError:
# speech was unintelligible
response = None
print("Unable to recognize speech")
return response