yunyin 6 months ago
parent d16eb297bc
commit f687795616

@ -1,52 +1,151 @@
import tkinter as tk
from tkinter import messagebox
import pyttsx3
import speech_recognition as sr
from tkinter import filedialog
engine = pyttsx3.init()
class VoiceRecognitionApp:
def __init__(self, master):
self.master = master
master.title("语音识别工具")
def center_window(window, width=200, height=150):
"""使窗口居中显示"""
window.update_idletasks() # 更新窗口,确保尺寸是最新的
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# 创建开始录音的按钮
self.record_button = tk.Button(master, text="开始录音", command=self.start_listening)
self.record_button.pack(pady=10)
if width is None:
width = window.winfo_width()
if height is None:
height = window.winfo_height()
# 创建完成按钮,初始时禁用
self.finish_button = tk.Button(master, text="完成并识别", state=tk.DISABLED, command=self.recognize_voice)
self.finish_button.pack(pady=10)
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}")
self.recognizer = sr.Recognizer()
self.audio_data = None
def create_text_to_speech_window(parent):
global text_entry, result_label
def start_listening(self):
# 启动录音
self.record_button.config(state=tk.DISABLED) # 禁用开始录音按钮
self.finish_button.config(state=tk.NORMAL) # 启用完成按钮
with sr.Microphone() as source:
print("请说话...")
self.audio_data = self.recognizer.listen(source)
window = tk.Toplevel(parent)
window.title("文本转语音")
def recognize_voice(self):
if self.audio_data is not None:
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(parent):
window = tk.Toplevel(parent)
window.title("音频文件识别")
recognize_button = tk.Button(window, text="识别文本", command=recognize_audio_from_file())
recognize_button.pack(side=tk.RIGHT, padx=120, pady=10)
text_entry = tk.Text(window, height=20)
text_entry.pack()
center_window(window)
def recognize_audio_from_file():
# 打开文件对话框选择音频文件
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:
recognized_text = self.recognizer.recognize_google(self.audio_data, language='zh-CN')
messagebox.showinfo("识别结果", f"你说的是: {recognized_text}")
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:
messagebox.showerror("错误", "无法识别你的语音")
result_label.config(text="无法识别音频中的内容")
except sr.RequestError as e:
messagebox.showerror("错误", f"服务不可用; {e}")
finally:
self.reset_ui() # 识别完成后重置UI状态
result_label.config(text=f"识别服务请求错误; {e}")
else:
result_label.config(text="音频已成功转换为文本")
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)
def reset_ui(self):
# 重置按钮状态和音频数据
self.record_button.config(state=tk.NORMAL)
self.finish_button.config(state=tk.DISABLED)
self.audio_data = None
center_window(window)
window.mainloop()
# 创建主窗口并启动应用
app = tk.Tk()
VoiceRecognitionApp(app)
app.mainloop()
if __name__ == "__main__":
main()
Loading…
Cancel
Save