语音识别python代码不工作

2024-04-28 23:06:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python 2.7中运行以下安装了pyAudio的代码。

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:                # use the default microphone as the audio source
    audio = r.listen(source)                   # listen for the first phrase and extract it into audio data

try:
    print("You said " + r.recognize(audio))    # recognize speech using Google Speech Recognition
except LookupError:                           # speech is unintelligible
    print("Could not understand audio")

输出给出一个闪烁的指针。就这样。请帮忙,因为我是新来的。


Tags: the代码importsourceaswithaudiospeech
3条回答

我用以下方法解决了同样的问题(噪音抑制), “listen”功能存在环境噪音问题。所以运行代码只是在等待。

使用该环境噪声抑制/调整; r.adjust_for_ambient_noise(source, duration=5)

推荐 Tuesday, March 28, 2017 Easy Speech Recognition in Python with PyAudio and Pocketsphinx

你试过更换吗

    print("You said " + r.recognize(audio))
    except LookupError:                          
    print("Could not understand audio")

    text = r.recognize_google(audio)
    print("You said : {}".format(text))
    text = r.recognize_google(audio)
    except:
        print("Sorry could not recognize your voice")

通过运行以下命令确保pyaudio.h已安装

    sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio

可能的原因可能是recognizer_instance.energy_threshold属性设置的值太高,无法开始使用。您应该降低此阈值,或调用recognizer_instance.adjust_for_ambient_noise(source, duration = 1)。你可以在Speech Recognition上了解更多

相关问题 更多 >