You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.4 KiB

6 months ago
import tkinter as tk
6 months ago
import pyttsx3
6 months ago
import speech_recognition as sr
6 months ago
from tkinter import filedialog
6 months ago
import threading
6 months ago
6 months ago
engine = pyttsx3.init()
6 months ago
6 months ago
def center_window(window, width=200, height=150):
"""使窗口居中显示"""
window.update_idletasks() # 更新窗口,确保尺寸是最新的
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
6 months ago
6 months ago
if width is None:
width = window.winfo_width()
if height is None:
height = window.winfo_height()
6 months ago
6 months ago
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}")
6 months ago
6 months ago
def create_text_to_speech_window(parent):
global text_entry, result_label
6 months ago
6 months ago
window = tk.Toplevel(parent)
window.title("文本转语音")
6 months ago
6 months ago
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))
6 months ago
def create_audio_recognition_window(window):
window = tk.Toplevel(window)
6 months ago
window.title("音频文件识别")
6 months ago
# 正确的写法
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()
6 months ago
center_window(window)
6 months ago
def audio_recognition_window():
global text_entry, result_label
window = tk.Toplevel()
window.title("音频")
6 months ago
select_button = tk.Button(window, text="选择音频文件",command=lambda win=window: recognize_audio_from_file())
select_button.pack(pady=50)
6 months ago
result_label = tk.Label(window, text="", fg="green")
result_label.pack(pady=5)
6 months ago
center_window(window)
6 months ago
# 运行Tkinter事件循环
window.mainloop()
6 months ago
def recognize_audio_from_file():
6 months ago
window = tk.Toplevel()
window.title("文本")
6 months ago
# 打开文件对话框选择音频文件
6 months ago
def worker():
6 months ago
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav")])
6 months ago
if file_path:
r = sr.Recognizer()
6 months ago
r.language = 'zh-CN'
6 months ago
with sr.AudioFile(file_path) as source:
audio_data = r.record(source)
6 months ago
text = r.recognize_sphinx(audio_data)
window.update_idletasks() # 更新窗口以避免同步问题
text_entry.delete(1.0, tk.END) # 清空文本框
text_entry.insert(tk.END, f"{text}")
text_entry = tk.Text(window, height=10, width=50)
text_entry.pack(pady=10)
6 months ago
thread = threading.Thread(target=worker)
thread.start()
6 months ago
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)
6 months ago
6 months ago
center_window(window)
6 months ago
6 months ago
window.mainloop()
6 months ago
6 months ago
if __name__ == "__main__":
main()