繁体   English   中英

我在 python 上做了一个虚拟助手,我不知道为什么语音识别不听并打印我所说的

[英]Im making a virtual Assitant on python and i dont know why speech recognition doesnt listening and prints the what i have said

因此,正如我所说,我正在使用 Python 制作虚拟助手,而语音识别不听我的,它不打印我所说的内容。 下面是代码:

import speech_recognition as sr

def get_audio():
    print("listening...")
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))
        
    return said


get_audio()

在我以 python 运行和调试代码后,它只说我在听我在麦克风上尽可能大声说话但不起作用。 我的任务栏显示 python 正在使用麦克风但仍然不起作用,我正在使用 vscode。

请所有可以帮助我的人。

显然,根据您的描述,您的代码一直在监听,我认为这就是问题所在。 现在我将在下面给出一个更新的代码

import speech_recognition as sr

def get_audio():
    print("listening...")
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration = 1)
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)

        except Exception as e:
            print("Exception: " + str(e))
        
    return said


get_audio()

所以我添加了r.adjust_for_ambient_noise(source, duration = 1)它的作用是,它根据背景噪音调整阈值,然后听你说什么。

这应该可以正常工作!

暂无
暂无

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

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