如果我没说什么,语音识别就停止了,如何保持它的听力

2024-06-09 05:55:44 发布

您现在位置:Python中文网/ 问答频道 /正文

想创建两个按钮,一个启动语音识别,一直到我点击按钮完成。问题是,当我单击“开始”时,没有说它停止的内容:

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()

Tags: textfieldascolumnwindowwidth按钮tk
1条回答
网友
1楼 · 发布于 2024-06-09 05:55:44

根据文档,listen方法有一个timeout参数,这个参数可能就是您要查找的

audio = r.listen(source, timeout=60)

在你放弃之前给你一分钟时间。你知道吗

相关问题 更多 >