From c48cb6da38a60a40f46c0d97fc3c35f6f938b767 Mon Sep 17 00:00:00 2001 From: yunyin <2977138976@qq.com> Date: Sat, 1 Jun 2024 22:17:17 +0800 Subject: [PATCH] 0601 --- yuyin.py | 68 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/yuyin.py b/yuyin.py index afbc258..735862b 100644 --- a/yuyin.py +++ b/yuyin.py @@ -2,6 +2,7 @@ import tkinter as tk import pyttsx3 import speech_recognition as sr from tkinter import filedialog +import threading engine = pyttsx3.init() @@ -61,36 +62,59 @@ def set_speed(value, engine): """根据滑块的值设置语音合成的语速""" engine.setProperty('rate', int(value)) -def create_audio_recognition_window(parent): - window = tk.Toplevel(parent) +def create_audio_recognition_window(window): + window = tk.Toplevel(window) window.title("音频文件识别") - recognize_button = tk.Button(window, text="识别文本", command=recognize_audio_from_file()) - recognize_button.pack(side=tk.RIGHT, padx=120, pady=10) + # 正确的写法 + recognize_button = tk.Button(window, text="音频识别",command=lambda win=window: audio_recognition_window()) + + recognize_button.pack(side=tk.RIGHT, padx=70, pady=50) + + # 创建结果标签 + result_label = tk.Label(window, text="") + result_label.pack() - text_entry = tk.Text(window, height=20) - text_entry.pack() center_window(window) +def audio_recognition_window(): + global text_entry, result_label + 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) + + result_label = tk.Label(window, text="", fg="green") + result_label.pack(pady=5) + # 运行Tkinter事件循环 + window.mainloop() + def recognize_audio_from_file(): # 打开文件对话框选择音频文件 - file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")]) - if file_path: - r = sr.Recognizer() - 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="音频已成功转换为文本") - + def worker(): + file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")]) + if file_path: + r = sr.Recognizer() + 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="音频已成功转换为文本") + + thread = threading.Thread(target=worker) + thread.start()