Skip to content

Instantly share code, notes, and snippets.

@hellodit
Last active January 27, 2021 04:11
Show Gist options
  • Save hellodit/a0b8201c1464991d706723b0d361eba0 to your computer and use it in GitHub Desktop.
Save hellodit/a0b8201c1464991d706723b0d361eba0 to your computer and use it in GitHub Desktop.
def speech_to_text_microphone(recognizer, microphone):
if not isinstance(recognizer, speech_recognition.Recognizer):
raise TypeError("`recognizer` must be `Recognizer` instance")
if not isinstance(microphone, speech_recognition.Microphone):
raise TypeError("`microphone` must be `Microphone` instance")
print("Say something!")
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,language="id-ID")
except speech_recognition.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except speech_recognition.UnknownValueError:
# speech was unintelligible
response["success"] = False
response["error"] = "Unable to recognize speech"
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment