From 07008602bb7c884be33274dc6988401e2d9998d5 Mon Sep 17 00:00:00 2001 From: yunyin <2977138976@qq.com> Date: Sun, 2 Jun 2024 22:47:03 +0800 Subject: [PATCH] 0602 --- yuyin.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/yuyin.py b/yuyin.py index 735862b..e6c801d 100644 --- a/yuyin.py +++ b/yuyin.py @@ -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()