speech recognition python code not working

后端 未结 13 1749
傲寒
傲寒 2020-12-15 10:23

I am running the following code in Python 2.7 with pyAudio installed.

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:             


        
相关标签:
13条回答
  • 2020-12-15 10:58

    have you tried replacing

        print("You said " + r.recognize(audio))
        except LookupError:                          
        print("Could not understand audio")
    

    with

        text = r.recognize_google(audio)
        print("You said : {}".format(text))
        text = r.recognize_google(audio)
        except:
            print("Sorry could not recognize your voice")
    

    ensure pyaudio.h is installed by running the below command

        sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio
    
    0 讨论(0)
  • 2020-12-15 10:59

    try to add

    r.adjust_for_ambient_noise(source,duration=1)
    

    where r is recogniser instance, like this

    import speech_recognition as sr
    
    r=sr.Recognizer()
    print(sr.Microphone.list_microphone_names())
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source,duration=1)
        # r.energy_threshold()
        print("say anything : ")
        audio= r.listen(source)
        try:
            text = r.recognize_google(audio)
            print(text)
        except:
            print("sorry, could not recognise")
    
    0 讨论(0)
  • 2020-12-15 11:00

    I have solved the same problem for me with the following (noise suppression), The "listen" function has problems with environment noise. So the running code is only blinking waiting.

    Use this ambient noise suppression/adjustment; r.adjust_for_ambient_noise(source, duration=5)

    Referance Tuesday, March 28, 2017 Easy Speech Recognition in Python with PyAudio and Pocketsphinx

    0 讨论(0)
  • 2020-12-15 11:00

    Put the Try and Except inside the indentation.

    Here is my Working code:-

    while True:
        r = sr.Recognizer()
            with sr.Microphone() as source:
                print("Say Something")
                audio=r.listen(source)
            try:    
                print(r.recognize_google(audio),"\n")
            except:
                pass
    
    0 讨论(0)
  • 2020-12-15 11:00

    Install in your python prompt (Anaconda Prompt) 1. pip install pyaudio 2. pip install --upgrade pyaudio 3. pip install wheel 4. pip install google-api-python-client 5. pip install monotonic 6. pip install SpeechRecognition

    import speech_recognition as sr r = sr.Recognizer()
    with sr.Microphone() as source:
    print("listening...") audio = r.record(source,duration=3) try: str=r.recognize_google(audio) print(str) except: print("some error occurred!")

    0 讨论(0)
  • 2020-12-15 11:08

    You may have to install these things first:

    1. pip install pyaudio
    2. pip install --upgrade pyaudio
    3. pip install wheel
    4. pip install google-api-python-client
    5. sudo apt-get install flac
    6. pip install monotonic
    7. pip install SpeechRecognition

    After that, refer the site (https://realpython.com/python-speech-recognition/) it will clearly explain what you wanted.


    Here I am attaching the code I've edited from that site. Since I am new it will not be perfect, but I've tried. This is to check weather the voice input is similar to the text I've given and also it will print what you said.


    #!/usr/bin/ python
    import time
    
    import speech_recognition as sr
    
    
    def recognize_speech_from_mic(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)
            audio = recognizer.listen(source)
    
        # set up the response object
        response = {
            "success": True,
            "error": None,
            "transcription": None
        }
    
        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
    
    
    if __name__ == "__main__":
    
        NUM_GUESSES = 1
        PROMPT_LIMIT = 2
        # create recognizer and mic instances
        recognizer = sr.Recognizer()
        microphone = sr.Microphone()
    
        word = "hello world"
    
        time.sleep(3)
    
        for i in range(NUM_GUESSES):
            for j in range(PROMPT_LIMIT):
                print('Guess {}. Speak!'.format(i+1))
                guess = recognize_speech_from_mic(recognizer, microphone)
                if guess["transcription"]:
                    break
                if not guess["success"]:
                    break
                print("I didn't catch that")
    
            # if there was an error, stop the game
            if guess["error"]:
                print("ERROR: {}".format(guess["error"]))
                break
    
            # show the user the transcription
            print("You said: {}".format(guess["transcription"]))
    
            # determine if guess is correct and if any attempts remain
            guess_is_correct = guess["transcription"].lower() == word.lower()
            user_has_more_attempts = i < NUM_GUESSES - 1
    
            if guess_is_correct:
                print("Correct!".format(word))
                break
            elif user_has_more_attempts:
                print("Incorrect. Try again.\n")
            else:
                print("Sorry, output is not similar to '{}'.".format(word))
                break
    
    0 讨论(0)
提交回复
热议问题