Optimus Prime
Optimus Prime

Reputation: 308

Python speech recognition is not working using SpeechRecognition 3.8.1 library

In my Python 3.8.6 project, I have installed "pip install SpeechRecognition" for my windows 10 computer. And the code sample is below,

    import speech_recognition as sr
    
    listener = sr.Recognizer()
    try:
        with sr.Microphone() as source:
            print('Listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            print(command)
    except:
        print("Something else went wrong")
        pass

After run this code, output is below,

Something else went wrong

I have already microphone build-in with my laptop and also I have checked it also using plug an external USB microphone. Then what is the problem in here? Is there need any hardware configuration to solve this issue?

Upvotes: 0

Views: 2162

Answers (1)

Arib Muhtasim
Arib Muhtasim

Reputation: 159

In the Speech Recognition Readme on PyPI, you can see there is a PyAudio Section. It means you have to have PyAudio Installed on your machine. But if you have PyAudio intsalled and you get an error, you need to share the error without the try except blocks, so we can analyze the error and give a solution.

To install PyAudio, execute pip install pyaudio in the terminal.

Sometimes pip install pyaudio can throw an error similar to this:

Collecting PyAudio
  Using cached https://files.pythonhosted.org/packages/ab/42/b4f04721c5c5bfc196ce156b3c768998ef8c0ae3654ed29ea5020c749a6b/PyAudio-0.2.11.tar.gz
Installing collected packages: PyAudio
  Running setup.py install for PyAudio ... error
    Complete output from command C:\Users\username\AppData\Local\Programs\Python\Python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\username\\AppData\\Local\\Temp\\pip-install-e5le61j0\\PyAudio\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\username\AppData\Local\Temp\pip-record-adj3zivl\install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.7
    copying src\pyaudio.py -> build\lib.win32-3.7
    running build_ext
    building '_portaudio' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    ----------------------------------------
Command "C:\Users\username\AppData\Local\Programs\Python\Python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\username\\AppData\\Local\\Temp\\pip-install-e5le61j0\\PyAudio\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\username\AppData\Local\Temp\pip-record-adj3zivl\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\username\AppData\Local\Temp\pip-install-e5le61j0\PyAudio\

Steps to fix this error:

  • Go to your terminal and execute python --version and in the case of Optimus Prime, his version is Python 3.8.6
  • Find if your Python Installation is 64 bit or 32 bit which you can see by going to your Python Terminal
  • Download a PyAudio Wheel file based on your Python version and your Python installation (64 bit / 32 bit)
  • Open up a Terminal in the directory where you have downloaded the Wheel File (.whl)
  • Then execute pip install <name of your wheel file>

And you are done!

Upvotes: 1

Related Questions