如何解决在 tkinter 中语音到文本
我有这个代码,我希望它开始记录用户所说的内容并将其转换为文本并记录花费的时间和识别的文本。
我的代码看起来像这样。
root = Tk()
root.title('Amplio Home Task')
root.iconbitmap('images/Ampliologo.ico')
root.geometry("1000x600")
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio_text = r.listen(source)
try:
txt = (r.recognize_google(audio_text))
print(txt)
except Exception as e:
print(e)
break
def fnc1to2():
welcome_frame.pack_forget()
target_text_frame.pack()
label.grid(row=0,column=0)
button3.grid(row=5,column=0)
button2.grid(row=6,column=0)
def fnc2to3():
target_text_frame.pack_forget()
word_meaning_frame.pack()
labe2.pack()
button4.pack()
# crate the frame
welcome_frame = Frame(root)
button1 = Button(welcome_frame,text='Start',command = fnc1to2)
button1.pack()
target_text_frame = Frame(root)
label = Label(target_text_frame,text='this will be some text for the user to read...",font="helvetica 28",wraplength=800,justify="center")
button2 = Button(target_text_frame,text='Done',command = fnc2to3)
button3 = Button(target_text_frame,command = rec)
word_meaning_frame = Frame(root)
labe2 = Label(word_meaning_frame,text='word meaning frame',justify="center")
button4 = Button(word_meaning_frame,command = fnc2to3)
welcome_frame.pack()
root.mainloop()
我无法用它来记录文本,而且我不确定如何让时间生效。 当我不使用 tkinter(如下)时,我在一个函数中完成了它,但是在 tkinter 中的某些东西对我来说并不那么顺利。
def intro():
start = time.time()
print ("Some long text for the user to read...")
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk\n"
'Press ENTER when you are done ')
start = time.time()
audio_text = r.listen(source)
if not input(' '):
print("Great job,thanks")
end = time.time()
readtime = (end - start)
try:
# using google speech recognition
text = (r.recognize_google(audio_text))
return text,readtime
except:
print("Sorry,I did not get that")
所以我想做的是:
当我点击统计按钮时-
- 开始录制
- 开始花时间
当我点击完成按钮时-
- 完成录制
- 运行语音识别
- 将识别的文本和记录到全局变量所花费的时间返回以供以后记录
- 移到下一帧
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。