繁体   English   中英

为什么我的 Python 语音识别码不起作用

[英]Why is my Python Speech Recognition code not working

我正在尝试制作一个简单的语音识别程序,这是我目前拥有的代码。

import speech_recognition as sr
from speech_recognition import Microphone
from speech_recognition import Recognizer

mic = sr.Microphone()
r = sr.Recognizer()
sr.Microphone.list_working_microphones()

for device_index in Microphone.list_working_microphones():
    Microphone(device_index=4)
    break
else:
    print("No working microphones found!")

with mic as source:
    print("say something:")
    audio = r.listen(source)
    request = r.recognize_google(audio)
    print(request)

它曾经有效,但现在无效。 它所做的是打印“say something:”,但它不听我的声音。 我确实安装了 pyaudio,所以这不是问题。

我的麦克风有一个小的红色 LED,在使用时会闪烁,因此基于此我假设 python 正在使用麦克风。

我已经重新安装了所有的软件包,也尝试使用 PocketSphinx,但它并没有解决问题。

为什么是这样? 这有点令人气愤,我不明白为什么会发生这种情况。

任何帮助表示赞赏。

更仔细地查看此示例代码

您不需要为mic提取变量。

此外,如文件所示,function 表示麦克风当前正在听到声音,这可能不是您在安静的环境中工作时想要的。

import speech_recognition as sr
import pyaudio
m = None
for device_index in sr.Microphone.list_working_microphones():
    m = sr.Microphone(device_index=device_index)
    break
else:
    raise Error("No working microphones found!")

if m is not None:
    print("Mic ready")

    # Now you are ready to recognize
    r = sr.Recognizer()

如果你想要文档中的代码,正好

import Microphone, Recognizer from speech_recognition

然后使用Microphone而不是sr.Microphone

暂无
暂无

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

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