pythonspeech-recognitionpyaudio

Cannot get audio through speech_recognition python by any method?


I am try to make SpeechRecognition 3.8.1 listen to my voice for four days. I have already seen the following stuff in the internet:

  1. https://github.com/Uberi/speech_recognition/issues/20
  2. https://www.geeksforgeeks.org/voice-assistant-using-python/
  3. https://pythonrepo.com/repo/Uberi-speech_recognition-python-audio
    • sudo apt-get install python-pyaudio python3-pyaudio does not work for me.
  4. speech recognition python code not working
  5. Errors on PyAudio on Visual studio code Python
  6. I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."?
  7. https://stackoverflow.com/questions/50424902/speechrecognition-python-package-does-not-listen?r=SearchResults&s=4|89.4428
  8. https://newbedev.com/speech-recognition-python-code-not-working-code-example
  9. https://www.py4u.net/discuss/22062

I have gone through many more discussions, ALL IN VAIN.
No solution worked for me till now. Please help!!
After a lot of unsuccessful attempts, I have at least managed to install pyaudio with the command pipwin install pyaudio


my python code:

import os
import pyttsx3, datetime, pyaudio
import speech_recognition as sr

# Initial Setup for pyttsx3 - speaking abilities
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)  # 0-male voice , 1-female voice
sr.Microphone.list_microphone_names()

# Initial Setup for speech_recognition - listening abilities
# r.energy_threshold = 10
# print(pyaudio.get_device_count() - 1)


def speak(speakable):
    """speak() takes a string and reads it loud"""
    engine.say(str(speakable))
    engine.runAndWait()


def takeCommand():
    pyaudio.PyAudio()
    r = sr.Recognizer()
    """It takes microphone input from the user and returns string output"""
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=0.9)
        print("Listening...")
        r.pause_threshold = 45
        audio = ""
        try:
            audio = r.listen(source)
            print("Recognizing...")
        except Exception as e:
            print("Listen err: ", e)
        try:
            print("Recognizing...")
            query = r.recognize_google(audio)
            print(f"User said: {query}\n")  # User query will be printed.
        except sr.UnknownValueError as e:
            print("Say that again please...")
            return "None"  # None string will be returned
        except Exception as err:
            print("Check your internet...")
            return "None"
    return query


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak("Good Morning!")

    elif hour >= 12 and hour < 18:
        speak("Good Afternoon!")

    else:
        speak("Good Evening!")
        speak(
            "Hello Sir, I am Friday, your Artificial intelligence assistant. Please tell me how may I help you"
        )


if __name__ == "__main__":
    os.system("CLS")
    while True:
        command = takeCommand().lower()
        print(f"Command: {command}")
        if "wish" in command:
            wishMe()

the output gets stuck at Listening.... Nothing happens after that.

More Info:

  1. I am using Windows 10 Home
  2. Code editor- vs code
  3. I am not using virtual env in this this program.
  4. I have also checked that Chrome Voice Search is working without any trouble.
  5. python3 -m speech_recognition result of the python3 -m speech_recognition command

Solution

  • Did you try with the python3 -m speech_recognition? You should see something like:

    ...
    A moment of silence, please...
    Set minimum energy threshold to 51.208131879528096
    Say something!
    Got it! Now to recognize it...
    You said hello
    

    If that does not work, you probably have something wrong with your audio system. Make sure that you can record your audio (https://onlinehardwaretest.com/microphone-test/) and consider rebooting your system.