python - 如果我没有说什么,语音识别就会停止,如何让它一直在听
问题描述
想要创建 2 个按钮来启动语音识别并继续,直到我点击按钮完成。问题是当我单击开始并且没有说它停止时:
import tkinter as tk
import speech_recognition as sr
window = tk.Tk()
window.title("Voice to Text")
window.geometry("300x350")
def startvoice():
r = sr.Recognizer()
with sr.Microphone() as source:
print("speak now")
audio = r.listen(source)
try:
startvoice.voice2text = r.recognize_google(audio)
print("you said:{}".format(startvoice.voice2text))
except:
print("Your voice not clear")
def finished():
text_field.focus()
text_field.delete('1.0', tk.END)
text_field.insert('1.0', startvoice.voice2text)
text_field = tk.Text(master=window, height=20, width=40)
text_field.grid(column=0, row=2)
button1 = tk.Button(text="Start", width=16, command=startvoice)
button1.grid(column=0, row=0)
button1 = tk.Button(text="finish", width=16, command=finished)
button1.grid(column=0, row=1)
window.mainloop()
解决方案
根据文档,该listen
方法有一个超时参数,这可能是您正在寻找的,例如
audio = r.listen(source, timeout=60)
会让你在放弃前一分钟。
推荐阅读
- php - 来自 PHP 代码的 MySQL 数据库表
- c# - c# 中的事件侦听器,用于侦听 TFS 订阅
- ibm-cloud - Watson Assistant 中不同实体的相同值
- amazon-web-services - 从 Sagemaker Estimator 训练后获取权重
- spring - 如何在 Junit 测试中比较 ModelAndView 对象?
- php - 在 laravel 表单中使用数据属性选择
- r - R中的布林带指标
- python - 如何将python输出重定向到python控制台和Windows中的文本文件
- http - HTTP 静态文件处理程序不断显示目录列表
- html - 封面页高度设置为 100% 有一半