yunyin 6 months ago
parent f687795616
commit c48cb6da38

@ -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()

Loading…
Cancel
Save