简体   繁体   English

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

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

I'm trying to make a simple speech recognition program, and this is the code I have currently.我正在尝试制作一个简单的语音识别程序,这是我目前拥有的代码。

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)

It used to work, but now it doesn't.它曾经有效,但现在无效。 What it does do is print "say something:", but it doesn't listen for my voice.它所做的是打印“say something:”,但它不听我的声音。 I do have pyaudio installed, so that isn't the issue.我确实安装了 pyaudio,所以这不是问题。

My microphone has a little red LED that flashes when in use, so based on that I assume that python is using the microphone.我的麦克风有一个小的红色 LED,在使用时会闪烁,因此基于此我假设 python 正在使用麦克风。

I've reinstalled all of the packages, and also tried using PocketSphinx, but it doesn't solve the issue.我已经重新安装了所有的软件包,也尝试使用 PocketSphinx,但它并没有解决问题。

Why is this?为什么是这样? It's a bit infuriating and I don't understand why this could be happening.这有点令人气愤,我不明白为什么会发生这种情况。

Any help appreciated.任何帮助表示赞赏。

Look at this example code more closely . 更仔细地查看此示例代码

You don't need to pull out a variable for mic .您不需要为mic提取变量。

Also, that function, as documented says microphones currently hearing sound , which probably isn't want you want while working in a quiet environment.此外,如文件所示,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()

If you want the code from the docs, exactly如果你想要文档中的代码,正好

import Microphone, Recognizer from speech_recognition

Then use Microphone rather than sr.Microphone然后使用Microphone而不是sr.Microphone

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

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