简体   繁体   English

如何使用 Speech_recognition 修复模块导入错误?

[英]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.但是,Python 给了我一些问题。 Namely, it doesn't recognize that speech_recognition exists.即,它不识别 Speech_recognition 存在。

$ 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.据我所知,我只有 python 版本 3.8.8。 I'm using Visual Studio Code for an editor, and I am using Windows 10 as my OS.我使用 Visual Studio Code 作为编辑器,我使用 Windows 10 作为我的操作系统。

Can you share more info?你能分享更多信息吗?

  • How did you install the python package?您是如何安装 python package 的? (pip3.8, Anaconda, ...) (pip3.8, Anaconda, ...)
  • If you are using pip, can you show: pip3.8 list如果您使用的是 pip,您可以显示: pip3.8 list
  • Are you using a virtual environment?你用的是虚拟环境吗?
  • According to https://pypi.org/project/SpeechRecognition/ , you can do a quick test after installation.根据https://pypi.org/project/SpeechRecognition/ ,您可以在安装后进行快速测试。 Can you try: python3.8 -m speech_recognition ?你可以试试: python3.8 -m speech_recognition吗?

Edit:编辑:

Ok, so now we know that the speech_recognition module can run under python3.8.好的,现在我们知道speech_recognition模块可以在python3.8下运行。

On the problem with vs code:关于vs代码的问题:

How are you executing the script (via the terminal by typing python3.8 <name of your script> or via the vs code run button)?你是如何执行脚本的(通过终端输入python3.8 <name of your script>或通过 vs code 运行按钮)?

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?假设(是的,我知道这很危险)您通过这些步骤(在 vs code 网站上)安装它并通过 vs code 运行它,您能展示一下环境吗?

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

Note: By default, VS Code uses the interpreter identified by python:pythonPath setting when debugging code.注意:默认情况下,VS Code 在调试代码时使用 python:pythonPath 设置标识的解释器。

The Status Bar always shows the current interpreter.状态栏总是显示当前的解释器。

在此处输入图像描述

On the minimum energy threshold:在最小能量阈值上:

The Troubleshooting guide, on the pypi page, might give some insight: pypi 页面上的故障排除指南可能会提供一些见解:

recognizer_instance.energy_threshold ... This value depends entirely on your microphone or audio data. recognizer_instance.energy_threshold ...这个值完全取决于你的麦克风或音频数据。 There is no one-size-fits-all value, but good values typically range from 50 to 4000.没有一刀切的价值,但好的价值通常在 50 到 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. identifyr_instance.energy_threshold 属性可能设置为太高而无法开始的值,然后通过动态能量阈值调整自动降低。 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.解决办法是降低这个阈值,或者提前调用recognizer_instance.adjust_for_ambient_noise,它会自动将阈值设置为一个合适的值。

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).您可以尝试从 repo 中获取main .py并对其进行调整(这样您就可以调整 energy_threshold 值)。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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