python - 为什么 lib pyttsx3 中的listen.listen 命令会打印这个额外的文本?

标签 python python-3.x voice-recognition pyttsx3

基本上尝试按照视频的步骤进行操作,一切都很好,就是这些代码行:

with sr.Microphone() as source:
print('listening..')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)

它给了我输出:

listening..
result2:
{   'alternative': [{'confidence': 0.97219545, 'transcript': 'hello there'}],
    'final': True}
hello there

Process finished with exit code 0

我只是想避免这条线:

  result2:
    {   'alternative': [{'confidence': 0.97219545, 'transcript': 'hello there'}],
        'final': True}

得到这样的结果:

listening..
hello there

Process finished with exit code 0

最佳答案

出于某种原因(看起来像是调试遗留物)speach_recognition 有此打印 here 。这是related issue .

您可以通过传递 show_all=True 来避免这种情况,然后手动执行库的操作:

with sr.Microphone() as source:
    print('listening..')
    voice = listener.listen(source)
    actual_result = listener.recognize_google(voice, show_all=True)

    if "confidence" in actual_result["alternative"]:
        best_hypothesis = max(actual_result["alternative"], key=lambda alternative: alternative["confidence"])
    else:
        best_hypothesis = actual_result["alternative"][0]
    confidence = best_hypothesis.get("confidence", 0.5)
    
    command = best_hypothesis["transcript"], confidence
    print(command)

没有信心:

with sr.Microphone() as source:
    print('listening..')
    voice = listener.listen(source)
    actual_result = listener.recognize_google(voice, show_all=True)

    if "confidence" in actual_result["alternative"]:
        best_hypothesis = max(actual_result["alternative"], key=lambda alternative: alternative["confidence"])
    else:
        best_hypothesis = actual_result["alternative"][0]
    
    command = best_hypothesis["transcript"]
    print(command)

关于python - 为什么 lib pyttsx3 中的listen.listen 命令会打印这个额外的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75087982/

相关文章:

python - 在 Python 中打印 {1}

python - 使用 Python 爬取网站

python - 在python中根据时间戳对字典进行排序

python - 未应用 Matplotlib 样式表

android - 谷歌语音识别超时

php - 使用 PHP 连接到 CMU Sphinx

python - 无法将 Django mongodb 引擎与 Django 一起使用?

python-3.x - 如何使用 #!/usr/bin/python3 解析 "bad interpreter: Permission denied"

python - Python 中矩形的可视化

Android:如何从我的应用程序中使用命令语音打开语音输入