|
|
@ -4,6 +4,7 @@ import speech_recognition as sr
|
|
|
|
from tkinter import filedialog
|
|
|
|
from tkinter import filedialog
|
|
|
|
import threading
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
engine = pyttsx3.init()
|
|
|
|
engine = pyttsx3.init()
|
|
|
|
|
|
|
|
|
|
|
|
def center_window(window, width=200, height=150):
|
|
|
|
def center_window(window, width=200, height=150):
|
|
|
@ -100,10 +101,9 @@ def recognize_audio_from_file():
|
|
|
|
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav")])
|
|
|
|
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.wav")])
|
|
|
|
if file_path:
|
|
|
|
if file_path:
|
|
|
|
r = sr.Recognizer()
|
|
|
|
r = sr.Recognizer()
|
|
|
|
r.language = 'zh-CN'
|
|
|
|
|
|
|
|
with sr.AudioFile(file_path) as source:
|
|
|
|
with sr.AudioFile(file_path) as source:
|
|
|
|
audio_data = r.record(source)
|
|
|
|
audio_data = r.record(source)
|
|
|
|
text = r.recognize_sphinx(audio_data)
|
|
|
|
text = r.recognize_sphinx(audio_data,language='zh-CN')
|
|
|
|
window.update_idletasks() # 更新窗口以避免同步问题
|
|
|
|
window.update_idletasks() # 更新窗口以避免同步问题
|
|
|
|
text_entry.delete(1.0, tk.END) # 清空文本框
|
|
|
|
text_entry.delete(1.0, tk.END) # 清空文本框
|
|
|
|
text_entry.insert(tk.END, f"{text}")
|
|
|
|
text_entry.insert(tk.END, f"{text}")
|
|
|
@ -117,37 +117,6 @@ def recognize_audio_from_file():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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():
|
|
|
|
def main():
|
|
|
|
window = tk.Tk()
|
|
|
|
window = tk.Tk()
|
|
|
|
window.title("选择界面")
|
|
|
|
window.title("选择界面")
|
|
|
@ -162,13 +131,11 @@ def main():
|
|
|
|
audio_recognition_button = tk.Button(window, text="音频文件识别", command=lambda: create_audio_recognition_window(window))
|
|
|
|
audio_recognition_button = tk.Button(window, text="音频文件识别", command=lambda: create_audio_recognition_window(window))
|
|
|
|
audio_recognition_button.pack(padx=10,pady=10)
|
|
|
|
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)
|
|
|
|
center_window(window)
|
|
|
|
|
|
|
|
|
|
|
|
window.mainloop()
|
|
|
|
window.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
main()
|