|
|
|
@ -1,92 +0,0 @@
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
from tkinter import ttk
|
|
|
|
|
import speech_recognition as sr
|
|
|
|
|
import pyttsx3
|
|
|
|
|
|
|
|
|
|
# 初始化文本转语音引擎
|
|
|
|
|
engine = pyttsx3.init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def text_to_speech():
|
|
|
|
|
text = text_input.get("1.0", "end-1c")
|
|
|
|
|
engine.say(text)
|
|
|
|
|
engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def speech_to_text():
|
|
|
|
|
# 初始化识别器
|
|
|
|
|
r = sr.Recognizer()
|
|
|
|
|
|
|
|
|
|
with sr.Microphone() as source:
|
|
|
|
|
print("请说话:")
|
|
|
|
|
audio = r.listen(source)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
text = r.recognize_google(audio, language='zh-CN')
|
|
|
|
|
text_output.delete("1.0", tk.END)
|
|
|
|
|
text_output.insert(tk.END, text)
|
|
|
|
|
except sr.UnknownValueError:
|
|
|
|
|
print("Google Speech Recognition无法理解音频")
|
|
|
|
|
except sr.RequestError as e:
|
|
|
|
|
print(f"无法从Google Speech Recognition服务请求结果; {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def real_time_transcription():
|
|
|
|
|
# 这里简化处理,实际应用中需要一个循环监听并实时转写
|
|
|
|
|
|
|
|
|
|
print("实时转写功能尚未实现,请手动调用speech_to_text进行单次转写")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def switch_frame(frame):
|
|
|
|
|
frame.tkraise()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建主窗口
|
|
|
|
|
root = tk.Tk()
|
|
|
|
|
root.title("语音识别与转换系统")
|
|
|
|
|
|
|
|
|
|
# 创建一个容器来放置不同的Frame
|
|
|
|
|
container = ttk.Frame(root)
|
|
|
|
|
container.pack(side="top", fill="both", expand=True)
|
|
|
|
|
|
|
|
|
|
# 创建不同的Frame对应不同功能
|
|
|
|
|
frame_text_to_speech = ttk.Frame(container)
|
|
|
|
|
frame_speech_to_text = ttk.Frame(container)
|
|
|
|
|
frame_real_time = ttk.Frame(container)
|
|
|
|
|
|
|
|
|
|
# 在每个Frame中添加对应的功能组件
|
|
|
|
|
# 文本转语音Frame
|
|
|
|
|
ttk.Label(frame_text_to_speech, text="请输入要转换为语音的文本:").pack()
|
|
|
|
|
text_input = tk.Text(frame_text_to_speech, height=20)
|
|
|
|
|
text_input.pack()
|
|
|
|
|
ttk.Button(frame_text_to_speech, text="转换", command=text_to_speech).pack()
|
|
|
|
|
|
|
|
|
|
# 语音转文本Frame
|
|
|
|
|
ttk.Label(frame_speech_to_text, text="请点击下方按钮开始语音识别:").pack()
|
|
|
|
|
ttk.Button(frame_speech_to_text, text="开始识别", command=speech_to_text).pack()
|
|
|
|
|
text_output = tk.Text(frame_speech_to_text, height=20)
|
|
|
|
|
text_output.pack()
|
|
|
|
|
|
|
|
|
|
# 实时转写Frame(简化示意)
|
|
|
|
|
ttk.Label(frame_real_time, text="实时转写功能界面(待实现)").pack()
|
|
|
|
|
ttk.Button(frame_real_time, text="返回", command=lambda: switch_frame(container)).pack()
|
|
|
|
|
|
|
|
|
|
# 将所有Frame添加到容器中
|
|
|
|
|
for frame in (frame_text_to_speech, frame_speech_to_text, frame_real_time):
|
|
|
|
|
frame.grid(row=0, column=0, sticky="nsew")
|
|
|
|
|
|
|
|
|
|
switch_frame(frame_text_to_speech) # 默认显示文本转语音界面
|
|
|
|
|
|
|
|
|
|
# 创建顶部菜单进行功能切换
|
|
|
|
|
menu = tk.Menu(root)
|
|
|
|
|
|
|
|
|
|
root.config(menu=menu)
|
|
|
|
|
|
|
|
|
|
sub_menu = tk.Menu(menu, tearoff=0)
|
|
|
|
|
|
|
|
|
|
menu.add_cascade(label="功能选择", menu=sub_menu)
|
|
|
|
|
sub_menu.add_command(label="文本转语音", command=lambda: switch_frame(frame_text_to_speech))
|
|
|
|
|
sub_menu.add_command(label="语音转文本", command=lambda: switch_frame(frame_speech_to_text))
|
|
|
|
|
sub_menu.add_command(label="实时转写", command=lambda: switch_frame(frame_real_time))
|
|
|
|
|
|
|
|
|
|
root.mainloop()
|