基本上尝试按照视频的步骤进行操作,一切都很好,就是这些代码行:
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/