yunyin 6 months ago
parent f687795616
commit c48cb6da38

@ -2,6 +2,7 @@ import tkinter as tk
import pyttsx3 import pyttsx3
import speech_recognition as sr import speech_recognition as sr
from tkinter import filedialog from tkinter import filedialog
import threading
engine = pyttsx3.init() engine = pyttsx3.init()
@ -61,36 +62,59 @@ def set_speed(value, engine):
"""根据滑块的值设置语音合成的语速""" """根据滑块的值设置语音合成的语速"""
engine.setProperty('rate', int(value)) engine.setProperty('rate', int(value))
def create_audio_recognition_window(parent): def create_audio_recognition_window(window):
window = tk.Toplevel(parent) window = tk.Toplevel(window)
window.title("音频文件识别") 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) 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(): def recognize_audio_from_file():
# 打开文件对话框选择音频文件 # 打开文件对话框选择音频文件
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")]) def worker():
if file_path: file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")])
r = sr.Recognizer() if file_path:
with sr.AudioFile(file_path) as source: r = sr.Recognizer()
audio_data = r.record(source) with sr.AudioFile(file_path) as source:
try: audio_data = r.record(source)
text = r.recognize_google(audio_data, language='zh-CN') try:
text_entry.delete(1.0, tk.END) # 清空文本框 text = r.recognize_google(audio_data, language='zh-CN')
text_entry.insert(tk.END, text) # 将识别的文本插入文本框 text_entry.delete(1.0, tk.END)
except sr.UnknownValueError: text_entry.insert(tk.END, text)
result_label.config(text="无法识别音频中的内容") except sr.UnknownValueError:
except sr.RequestError as e: result_label.config(text="无法识别音频中的内容")
result_label.config(text=f"识别服务请求错误; {e}") except sr.RequestError as e:
else: result_label.config(text=f"识别服务请求错误; {e}")
result_label.config(text="音频已成功转换为文本") else:
result_label.config(text="音频已成功转换为文本")
thread = threading.Thread(target=worker)
thread.start()

Loading…
Cancel
Save