yunyin 6 months ago
parent c48cb6da38
commit 07008602bb

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

Loading…
Cancel
Save