为什么是命令侦听器.听lib pyttsx3打印这个额外的文本?



基本上尝试遵循vid的步骤,一切都工作得很好,除了那几行代码:

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在这里有这个打印。这是相关的问题。

您可以通过传递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)

最新更新