Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

speech recognition - Raspberry Pi Zero W Python SpeechRecognition stops working without changing any code

I am working on a project involving the Raspberry Pi Zero W and speech recognition. It worked the first time, and then after I ran it again it was just stuck recording. Running python -m speech_recognition didn't work in the beginning, but now I fixed it, but my program still isn't working. Here is the code:

print("INFO: Importing libraries...")
import speech_recognition as sr
import i2c
print("INFO: Done.")
r = sr.Recognizer()
m = sr.Microphone(1)

lcd = i2c.lcd()

"""Sets parameters to stop recognizer.listening faster.

Parameters:
"r.pause_threshold":      an integer in seconds for how long the recognizer needs of silence to stop recording
"r.non_speaking_duration: seconds of non-speaking audio to keep on both sides of the recording, changed because it cannot be higher than `r.pause_threshold`
"""
r.pause_threshold = 0.3
r.non_speaking_duration = 0.3

def speech(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.

    Returns a dictionary with three keys:
    "success": a boolean indicating whether or not the API request was
               successful
    "error":   `None` if no error occured, otherwise a string containing
               an error message if the API could not be reached or
               speech was unrecognizable
    "transcription": `None` if speech could not be transcribed,
               otherwise a string containing the transcribed text
    """
    # check that recognizer and microphone arguments are appropriate type
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio
    # from the microphone
    with microphone as source:
        #recognizer.adjust_for_ambient_noise(source)
        print("INFO: Recording...")
        audio = recognizer.listen(source)

    print("INFO: Recognizing...")
    
    # set up the response object
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    # try recognizing the speech in the recording
    # if a RequestError or UnknownValueError exception is caught,
    #     update the response object accordingly
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"

    return response

while True:
     print("INFO: Ready for speech.")
     
     trans = speech(r, m)["transcription"]
     if trans == "exit":
         exit()
     if trans == None:
         trans = "*unaudable*"
     print(trans);
     lcd.lcd_clear()
     lcd.lcd_display_string(trans)
     

Thanks for your time!

question from:https://stackoverflow.com/questions/65884709/raspberry-pi-zero-w-python-speechrecognition-stops-working-without-changing-any

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...