如何使用语音识别修复模块导入错误?

时间:2021-03-13 19:50:46

标签: python speech-recognition speech-to-text

我目前正在尝试编写一些代码来听你说的话,然后将其翻译成莫尔斯电码。

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 

然而,Python 给我带来了一些问题。即,它不识别语音识别存在。

$ 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'

但是,确实如此。

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

据我所知,我只有 Python 3.8.8 版。我使用 Visual Studio Code 作为编辑器,我使用 Windows 10 作为我的操作系统。

1 个答案:

答案 0 :(得分:0)

你能分享更多信息吗?

  • 你是如何安装python包的? (pip3.8, Anaconda, ...)
  • 如果您使用的是 pip,您能否显示:pip3.8 list
  • 您使用的是虚拟环境吗?
  • 根据https://pypi.org/project/SpeechRecognition/,您可以在安装后进行快速测试。你可以试试:python3.8 -m speech_recognition吗?

编辑:

好的,现在我们知道speech_recognition模块可以在python3.8下运行了。

关于 vs 代码的问题:

您如何执行脚本(通过终端输入 python3.8 <name of your script> 或通过 vs 代码运行按钮)?

假设(是的,我知道这很危险)您通过 these steps(在 vs code 网站上)安装它并通过 vs code 运行它,您能显示环境吗?

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

<块引用>

注意:默认情况下,VS Code 在调试代码时使用由 python:pythonPath 设置标识的解释器。

状态栏总是显示当前的解释器。

enter image description here

关于最小能量阈值:

pypi 页面上的故障排除指南可能会提供一些见解:

<块引用>

recognizer_instance.energy_threshold ... 该值完全取决于您的麦克风或音频数据。没有一刀切的值,但好的值通常在 50 到 4000 之间。

<块引用>

识别器在第一次开始收听后无法立即识别语音。

receiver_instance.energy_threshold 属性可能设置为一个太高的值,无法开始,然后通过动态能量阈值调整自动将其调低。在未达到良好水平之前,能量阈值太高以至于语音仅被视为环境噪声。

解决办法是降低这个阈值,或者提前调用recognizer_instance.adjust_for_ambient_noise,它会自动设置一个好的阈值。

这可能很困难,因为“快速测试”脚本不允许修改。您可以尝试从存储库中获取 main.py 并对其进行调整(以便您可以调整 energy_threshold 值)。

列出所有麦克风也是值得的,以确保您使用的是正确的麦克风。

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