Python and speech recognition

What did you say?

As I often talked about text-to speech, why we do non talk about speech recognition? As also https://realpython.com/ talked about this topic, I want to share a little code here to give a sample of its possible utilization.

import time
import speech_recognition as sr


def elaborate_recording(recognizer, microphone):
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("A problem with the recognizer")
    if not isinstance(microphone, sr.Microphone):
        raise TypeError("A problem with the microphone")
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    elaboration = {
        "success": True,
        "error": None,
        "transcription": None
    }
    try:
        elaboration["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        elaboration["success"] = False
        elaboration["error"] = "API not available"
    except sr.UnknownValueError:
        elaboration["error"] = "I did not understand"

    return elaboration


if __name__ == "__main__":
    print("I start the recognizer and the mic.. start talking")
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    print("Dici qualcosa adesso / Say something now ... pause 3 second ... continue talking")
    time.sleep(3)
    print("NOW!")
    guess = elaborate_recording(recognizer, microphone)
    print(guess["transcription"])


Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.