qwerlol123? 6 months ago
parent 50c50504e2
commit 5caac9d3a0

@ -3,14 +3,17 @@ import tkinter as tk
import threading import threading
import speech_recognition as sr import speech_recognition as sr
# 初始化语音播报对象 # Initialize speech synthesis object
talker = pyttsx3.init() talker = pyttsx3.init()
#登陆界面
# 用户账号和密码 # Initialize speech recognition object
recognizer = sr.Recognizer()
# Login credentials
userName = "admin" userName = "admin"
userPass = "123" userPass = "123"
# 创建对话页面 # Create chat window
def chat_window(): def chat_window():
chat_window = tk.Tk() chat_window = tk.Tk()
chat_window.title("对话") chat_window.title("对话")
@ -35,6 +38,28 @@ def chat_window():
talker.say(answer) talker.say(answer)
talker.runAndWait() talker.runAndWait()
def voice_recognition():
with sr.Microphone() as source:
print("请说话...")
audio = recognizer.listen(source)
try:
print("识别中...")
question = recognizer.recognize_google(audio, language="zh-CN")
print("你说: " + question)
answer = question.strip("吗??" + "!")
if question == "不聊了拜拜":
talker.say("好的,拜拜就拜拜!")
talker.runAndWait()
chat_window.destroy()
else:
chat_text.insert(tk.END, "你: " + question + "\n")
chat_text.insert(tk.END, "机器人的回答:" + answer + "\n")
talker_thread = threading.Thread(target=speak, args=(answer,))
talker_thread.start()
except sr.UnknownValueError:
print("抱歉, 未能识别,请重试")
chat_text = tk.Text(chat_window) chat_text = tk.Text(chat_window)
chat_text.pack() chat_text.pack()
@ -44,31 +69,10 @@ def chat_window():
send_button = tk.Button(chat_window, text="发送", command=chat) send_button = tk.Button(chat_window, text="发送", command=chat)
send_button.pack() send_button.pack()
voice_button = tk.Button(chat_window, text="语音识别", command=voice_recognition)
voice_button.pack()
chat_window.mainloop() chat_window.mainloop()
#语音识别功能(使用百度云语音识别)
def voice_recognition():
with sr.Microphone() as source:
print("请说话...")
audio = recognizer.listen(source)
try:
print("识别中...")
question = recognizer.recognize_baidu(audio, app_id='73927317', api_key='3jGcj5fLma64CtB3tTEuLcei', secret_key='qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG')
print("你说: " + question)
answer = question.strip("吗??" + "!")
if question == "不聊了拜拜":
talker.say("好的,拜拜就拜拜!")
talker.runAndWait()
chat_window.destroy()
else:
chat_text.insert(tk.END, "你: " + question + "\n")
chat_text.insert(tk.END, "机器人的回答:" + answer + "\n")
talker_thread = threading.Thread(target=speak, args=(answer,))
talker_thread.start()
except sr.UnknownValueError:
print("抱歉, 未能识别,请重试")
except sr.RequestError:
print("抱歉, 无法连接到服务器")
# 创建登录窗口 # 创建登录窗口
def login_window(): def login_window():
login_window = tk.Tk() login_window = tk.Tk()

Loading…
Cancel
Save