简体   繁体   中英

How to fix Module Import Error with speech_recognition?

I'm currently trying to make some code that will listen to what you say, and then translate that into morse code.

import speech_recognition as sr
import playsound
from gtts import gTTS
import os

num = 1


def assistant_speaks(output):
    global num 

    # num to rename every audio file 
    # with different name to remove ambiguity 
    num += 1
    print("BRO : ", output)

    toSpeak = gTTS(text = output, lang ='en-uk', slow = False)
    # saving the audio file given by google text to speech
    file = str(num)+".mp3 "
    toSpeak.save(file)

    playsound.playsound(file, True)

    os.remove(file)



def get_audio():
    rObject = sr.Recognizer()
    audio = ''

    with sr.Microphone() as source:
        print("Speak...")
    
        # recording the audio using speech recognition
        audio = rObject.listen(source, phrase_time_limit = 5)
    print("Stop.") # limit 5 secs

    try:

        text = rObject.recognize_google(audio, language ='en-US')
        print("You : ", text)
        return text

    except:
        speak = "Could not understand your audio, please try again!"
        assistant_speaks(speak, grootmode)
        return 0 

However, Python is giving me some problems. Namely, it doesn't recognize that speech_recognition exists.

$ C:/Users/J/AppData/Local/Programs/Python/Python38/python.exe d:/J/Documents/p
ython_files/raspi/morse.py
Traceback (most recent call last):
  File "d:/J/Documents/python_files/raspi/morse.py", line 1, in <module>
    import speech_recognition as sr
ModuleNotFoundError: No module named 'speech_recognition'

However, it does.

cefpython3        66.0
certifi           2020.12.5
chardet           4.0.0
click             7.1.2
gTTS              2.2.2
idna              2.10
pip               21.0.1
playsound         1.2.2
PyAudio           0.2.11
pywin32           300
requests          2.25.1
six               1.15.0
SpeechRecognition 3.8.1
urllib3           1.26.3
winspeech         1.0.1

As far as I can tell I only have python version 3.8.8. I'm using Visual Studio Code for an editor, and I am using Windows 10 as my OS.

Can you share more info?

  • How did you install the python package? (pip3.8, Anaconda, ...)
  • If you are using pip, can you show: pip3.8 list
  • Are you using a virtual environment?
  • According to https://pypi.org/project/SpeechRecognition/ , you can do a quick test after installation. Can you try: python3.8 -m speech_recognition ?

Edit:

Ok, so now we know that the speech_recognition module can run under python3.8.

On the problem with vs code:

How are you executing the script (via the terminal by typing python3.8 <name of your script> or via the vs code run button)?

Assuming (yes I know it's dangerous) you installed it via these steps (on the vs code website) and run it via vs code, can you show the environment?

https://code.visualstudio.com/docs/python/environments

Note: By default, VS Code uses the interpreter identified by python:pythonPath setting when debugging code.

The Status Bar always shows the current interpreter.

在此处输入图像描述

On the minimum energy threshold:

The Troubleshooting guide, on the pypi page, might give some insight:

recognizer_instance.energy_threshold ... This value depends entirely on your microphone or audio data. There is no one-size-fits-all value, but good values typically range from 50 to 4000.

The recognizer can't recognize speech right after it starts listening for the first time.

The recognizer_instance.energy_threshold property is probably set to a value that is too high to start off with, and then being adjusted lower automatically by dynamic energy threshold adjustment. Before it is at a good level, the energy threshold is so high that speech is just considered ambient noise.

The solution is to decrease this threshold, or call recognizer_instance.adjust_for_ambient_noise beforehand, which will set the threshold to a good value automatically.

This might be difficult since the 'quick test' script does not allow for modifications. You could try taking main .py from the repo and adapting it (so you can tweak the energy_threshold value).

It might also be worth to list all the microphones, to be sure you are using the correct one.

import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM