From 5caac9d3a09092c13d76fae0e776f3852d48b742 Mon Sep 17 00:00:00 2001 From: qwerlol123? <2082437969@qq.com> Date: Tue, 28 May 2024 21:57:09 +0800 Subject: [PATCH] 11 --- AI聊天机器人.py | 60 +++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/AI聊天机器人.py b/AI聊天机器人.py index 4e52a10..932ae06 100644 --- a/AI聊天机器人.py +++ b/AI聊天机器人.py @@ -3,14 +3,17 @@ import tkinter as tk import threading import speech_recognition as sr -# 初始化语音播报对象 +# Initialize speech synthesis object talker = pyttsx3.init() -#登陆界面 -# 用户账号和密码 + +# Initialize speech recognition object +recognizer = sr.Recognizer() + +# Login credentials userName = "admin" userPass = "123" -# 创建对话页面 +# Create chat window def chat_window(): chat_window = tk.Tk() chat_window.title("对话") @@ -35,6 +38,28 @@ def chat_window(): talker.say(answer) 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.pack() @@ -44,31 +69,10 @@ def chat_window(): send_button = tk.Button(chat_window, text="发送", command=chat) send_button.pack() + voice_button = tk.Button(chat_window, text="语音识别", command=voice_recognition) + voice_button.pack() + 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(): login_window = tk.Tk()