yunyin 6 months ago
parent c48cb6da38
commit 07008602bb

@ -83,35 +83,34 @@ def audio_recognition_window():
window = tk.Toplevel()
window.title("音频")
text_entry = tk.Text(window, height=10, width=50)
text_entry.pack(pady=10)
select_button = tk.Button(window, text="选择并识别",command=lambda win=window: recognize_audio_from_file())
select_button.pack(pady=10)
select_button = tk.Button(window, text="选择音频文件",command=lambda win=window: recognize_audio_from_file())
select_button.pack(pady=50)
result_label = tk.Label(window, text="", fg="green")
result_label.pack(pady=5)
center_window(window)
# 运行Tkinter事件循环
window.mainloop()
def recognize_audio_from_file():
window = tk.Toplevel()
window.title("文本")
# 打开文件对话框选择音频文件
def worker():
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")])
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav")])
if file_path:
r = sr.Recognizer()
r.language = 'zh-CN'
with sr.AudioFile(file_path) as source:
audio_data = r.record(source)
try:
text = r.recognize_google(audio_data, language='zh-CN')
text_entry.delete(1.0, tk.END)
text_entry.insert(tk.END, text)
except sr.UnknownValueError:
result_label.config(text="无法识别音频中的内容")
except sr.RequestError as e:
result_label.config(text=f"识别服务请求错误; {e}")
else:
result_label.config(text="音频已成功转换为文本")
text = r.recognize_sphinx(audio_data)
window.update_idletasks() # 更新窗口以避免同步问题
text_entry.delete(1.0, tk.END) # 清空文本框
text_entry.insert(tk.END, f"{text}")
text_entry = tk.Text(window, height=10, width=50)
text_entry.pack(pady=10)
thread = threading.Thread(target=worker)
thread.start()

Loading…
Cancel
Save