|
|
import tkinter as tk
|
|
|
import pyttsx3
|
|
|
import speech_recognition as sr
|
|
|
from tkinter import filedialog
|
|
|
import threading
|
|
|
|
|
|
engine = pyttsx3.init()
|
|
|
|
|
|
def center_window(window, width=200, height=150):
|
|
|
"""使窗口居中显示"""
|
|
|
window.update_idletasks() # 更新窗口,确保尺寸是最新的
|
|
|
screen_width = window.winfo_screenwidth()
|
|
|
screen_height = window.winfo_screenheight()
|
|
|
|
|
|
if width is None:
|
|
|
width = window.winfo_width()
|
|
|
if height is None:
|
|
|
height = window.winfo_height()
|
|
|
|
|
|
x_cordinate = int((screen_width / 2) - (width / 2))
|
|
|
y_cordinate = int((screen_height / 2) - (height / 2))
|
|
|
window.geometry(f"{width}x{height}+{x_cordinate}+{y_cordinate}")
|
|
|
|
|
|
def create_text_to_speech_window(parent):
|
|
|
global text_entry, result_label
|
|
|
|
|
|
window = tk.Toplevel(parent)
|
|
|
window.title("文本转语音")
|
|
|
|
|
|
speed_slider = tk.Scale(window, from_=50, to=200, orient=tk.HORIZONTAL,command=lambda value: set_speed(value, engine))
|
|
|
speed_slider.set(120) # 默认语速
|
|
|
speed_slider.pack()
|
|
|
|
|
|
label = tk.Label(window, text="请输入文字:")
|
|
|
label.pack(pady=10)
|
|
|
|
|
|
|
|
|
# 创建一个文本框,用于用户输入
|
|
|
text_entry = tk.Text(window, height=20)
|
|
|
text_entry.pack()
|
|
|
|
|
|
|
|
|
|
|
|
convert_button = tk.Button(window, text="转换语音", command=text_to_speech)
|
|
|
convert_button.pack(side=tk.LEFT, padx=250, pady=10)
|
|
|
|
|
|
|
|
|
result_label = tk.Label(window, text="", fg="green")
|
|
|
result_label.pack(pady=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def text_to_speech():
|
|
|
engine = pyttsx3.init()
|
|
|
text = text_entry.get("1.0", "end-1c") # 获取文本框中的文本
|
|
|
engine.say(text)
|
|
|
engine.runAndWait()
|
|
|
|
|
|
|
|
|
def set_speed(value, engine):
|
|
|
"""根据滑块的值设置语音合成的语速"""
|
|
|
engine.setProperty('rate', int(value))
|
|
|
|
|
|
def create_audio_recognition_window(window):
|
|
|
window = tk.Toplevel(window)
|
|
|
window.title("音频文件识别")
|
|
|
|
|
|
# 正确的写法
|
|
|
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()
|
|
|
|
|
|
|
|
|
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 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()
|
|
|
|
|
|
|
|
|
|
|
|
def recognize_audio_realtime(parent):
|
|
|
window = tk.Toplevel(parent)
|
|
|
window.title("实时转写")
|
|
|
|
|
|
center_window(window)
|
|
|
# 初始化识别器
|
|
|
r = sr.Recognizer()
|
|
|
# 使用麦克风作为源
|
|
|
mic = sr.Microphone()
|
|
|
|
|
|
# 调整能量阈值和监听时间以适应不同环境
|
|
|
with mic as source:
|
|
|
r.adjust_for_ambient_noise(source)
|
|
|
print("请开始说话...")
|
|
|
|
|
|
# 这里简化处理,实际实时转写可能需要循环监听并处理数据块
|
|
|
audio = r.listen(source, timeout=5) # 例如,监听5秒
|
|
|
|
|
|
try:
|
|
|
# 尝试识别
|
|
|
text = r.recognize_google(audio, language='zh-CN')
|
|
|
text_entry.delete(1.0, tk.END) # 清空文本框
|
|
|
text_entry.insert(tk.END, text) # 插入识别的文本
|
|
|
result_label.config(text="实时语音已转换为文本")
|
|
|
except sr.WaitTimeoutError:
|
|
|
result_label.config(text="未检测到语音输入")
|
|
|
except sr.UnknownValueError:
|
|
|
result_label.config(text="无法识别音频中的内容")
|
|
|
except sr.RequestError as e:
|
|
|
result_label.config(text=f"识别服务请求错误; {e}")
|
|
|
|
|
|
def main():
|
|
|
window = tk.Tk()
|
|
|
window.title("选择界面")
|
|
|
window.geometry("400x300")
|
|
|
window.resizable(True, True)
|
|
|
|
|
|
# 创建并配置跳转到文本转语音窗口的按钮
|
|
|
text_to_speech_button = tk.Button(window, text="文本转语音", command=lambda: create_text_to_speech_window(window))
|
|
|
text_to_speech_button.pack(padx=10,pady=10)
|
|
|
|
|
|
# 创建并配置跳转到音频识别窗口的按钮
|
|
|
audio_recognition_button = tk.Button(window, text="音频文件识别", command=lambda: create_audio_recognition_window(window))
|
|
|
audio_recognition_button.pack(padx=10,pady=10)
|
|
|
|
|
|
# 创建并配置跳转到实时语音识别窗口的按钮
|
|
|
audio_button = tk.Button(window, text="实时转写",command=lambda: recognize_audio_realtime(window))
|
|
|
audio_button.pack(padx=10,pady=10)
|
|
|
|
|
|
center_window(window)
|
|
|
|
|
|
window.mainloop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |