|
|
@ -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,20 +62,41 @@ 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():
|
|
|
|
# 打开文件对话框选择音频文件
|
|
|
|
# 打开文件对话框选择音频文件
|
|
|
|
|
|
|
|
def worker():
|
|
|
|
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")])
|
|
|
|
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav *.mp3")])
|
|
|
|
if file_path:
|
|
|
|
if file_path:
|
|
|
|
r = sr.Recognizer()
|
|
|
|
r = sr.Recognizer()
|
|
|
@ -82,8 +104,8 @@ def recognize_audio_from_file():
|
|
|
|
audio_data = r.record(source)
|
|
|
|
audio_data = r.record(source)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
text = r.recognize_google(audio_data, language='zh-CN')
|
|
|
|
text = r.recognize_google(audio_data, language='zh-CN')
|
|
|
|
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, text)
|
|
|
|
except sr.UnknownValueError:
|
|
|
|
except sr.UnknownValueError:
|
|
|
|
result_label.config(text="无法识别音频中的内容")
|
|
|
|
result_label.config(text="无法识别音频中的内容")
|
|
|
|
except sr.RequestError as e:
|
|
|
|
except sr.RequestError as e:
|
|
|
@ -91,6 +113,8 @@ def recognize_audio_from_file():
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
result_label.config(text="音频已成功转换为文本")
|
|
|
|
result_label.config(text="音频已成功转换为文本")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread = threading.Thread(target=worker)
|
|
|
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|