将Human Detection with Face Recognition这段代码写入以下代码import cv2 # OpenCV库,用于图像处理
import numpy as np # 数值计算库
from collections import defaultdict # 提供带默认值的字典
import shutil # 文件操作库
import pyttsx3 # 本地语音合成库
from datetime import datetime # 日期时间处理
import logging # 日志处理库
import threading # 线程库
import queue # 队列库
import json # JSON处理库
from pathlib import Path # 路径处理库
from typing import List # 类型提示
import time # 时间处理库
import os # 操作系统相关操作
import sys # 系统相关操作
from flask import Flask, render_template # Flask框架及模板渲染
import geocoder # 地理位置获取模块
from .weather.weather_service import WeatherService # 导入天气服务模块
from .weather.config import * # 如有需要可导入配置
from .weather.api_client import * # 如有需要可导入API客户端
from .weather.exception import * # 如有需要可导入异常定义
from .utils import read_text_baidu, user_speech_recognition, record_audio_until_silence, audio_to_text, text_to_speech_chinese, load_known_faces_from_folder # 导入通用工具函数
from .voice_feature import VoiceAssistant # 导入语音助手模块
from .face_recognition import FaceRecognition # 导入人脸识别系统
# 初始化Flask应用
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True # 设置模板自动重载
# 全局运行标志,控制主循环0
running = True # 控制主循环是否继续运行
face_detected = False # 标记是否检测到人脸
wake_words = ["小", "小朋友", "朋友"] # 唤醒词列表
WAKE_WORD_THRESHOLD = 0.7 # 唤醒词识别阈值
assistant = None # 全局语音助手对象
face_detection_running = False # 标记人脸检测线程是否运行中
face_detection_success = False # 标记人脸检测是否成功
# 定义Flask路由,主页
@app.route('/')
def index():
"""渲染主界面页面"""
return render_template('index.html')
# 定义Flask路由,测试接口
@app.route('/test')
def test():
"""测试接口,验证Flask服务是否正常"""
return "Flask server is working!"
preloaded_face_data = None # 全局变量,存储预加载的人脸数据
def preload_face_data():
"""预加载人脸数据到内存,提升识别速度"""
face_system = FaceRecognition() # 创建人脸识别系统对象
image_paths_by_person = load_known_faces_from_folder("known_faces") # 加载已知人脸图片路径
for person_name, image_paths in image_paths_by_person.items(): # 遍历每个人
face_system.add_new_person(person_name, image_paths) # 添加人脸特征
return face_system # 返回人脸识别系统对象
def detect_face(timeout=60) -> bool:
"""使用预加载的人脸数据检测人脸,超时返回False"""
global preloaded_face_data
if preloaded_face_data is None: # 检查是否已预加载
print("Error: Face data not preloaded.")
return False
start_time = time.time() # 记录检测开始时间
while time.time() - start_time < timeout and running: # 在超时时间内循环检测
try:
recognized = preloaded_face_data.start_recognition() # 调用识别方法
if recognized: # 如果识别到人脸
print("Face detected")
return True
else:
print("No face detected (during activation)")
time.sleep(0.5) # 未识别到,等待0.5秒再试
except Exception as e:
print(f"Error during recognition: {e}") # 捕获异常并打印
time.sleep(0.5)
return False # 超时未识别到人脸
def get_user_location():
"""通过geocoder获取用户地理位置"""
try:
return geocoder.ip("me") # 获取本机IP对应的地理位置
except Exception as e:
print(f"Error getting location: {e}")
return None
def assistant_mode():
"""语音助手主循环,处理用户语音指令"""
global running, face_detected, assistant
if assistant is None: # 检查语音助手是否初始化
print("Error: Assistant not initialized.")
return
location = get_user_location() # 获取用户地理位置
weather = WeatherService() # 创建天气服务对象
longitude = location.lng if location else None # 获取经度
latitude = location.lat if location else None # 获取纬度
response = weather.get_weather_info(longitude, latitude) if longitude and latitude else {} # 获取天气信息
listening_duration = 60 # 监听超时时间(秒)
last_interaction_time = time.time() # 上次交互时间
read_text_baidu("你好!有什么我可以帮您?") # 语音播报欢迎语
while running and face_detected: # 只要系统运行且检测到人脸
if time.time() - last_interaction_time > listening_duration: # 超时未交互
read_text_baidu("等待唤醒...")
face_detected = False
break
text = user_speech_recognition() # 获取用户语音输入
if text: # 如果识别到内容
print(f"User said: {text}")
last_interaction_time = time.time() # 重置交互时间
if '天气' in text and response: # 天气查询
print("Weather query detected")
print("Response: " + response.get('weather_condition', ''))
read_text_baidu(f"今天的天气状况如下, 位置:{location.city if location else ''}")
read_text_baidu(f"天气:{response.get('weather_condition', '')}")
read_text_baidu(f"温度:{response.get('temperature', '')}")
read_text_baidu(f"体感温度:{response.get('feels_like', '')}")
read_text_baidu(f"湿度:{response.get('humidity', '')}")
read_text_baidu(f"风向:{response.get('wind_direction', '')}")
read_text_baidu(f"风速:{response.get('wind_speed', '')}")
read_text_baidu(f"气压:{response.get('pressure', '')}")
read_text_baidu(f"能见度:{response.get('visibility', '')}")
read_text_baidu(f"云量:{response.get('cloud_coverage', '')}")
continue
elif '几点' in text: # 时间查询
print("Time query detected")
read_text_baidu(f"现在是 {time.strftime('%H:%M')}")
continue
elif '空调' in text: # 空调控制
print("AC query detected")
read_text_baidu("好的,正在处理空调指令。")
continue
elif '拜拜' in text or '再见' in text: # 结束对话
read_text_baidu("拜拜,下次再见!")
face_detected = False
continue
else: # 其他内容交给大模型处理
print("Deepseek request")
print(f"Heard: {text}, processing with DeepSeek...")
response = assistant.chat(text) # 调用大模型
print(f"DeepSeek response: {response}")
read_text_baidu(response)
continue
else:
print("Listening for command...") # 未识别到内容,继续监听
time.sleep(1)
if face_detected: # 如果还在检测状态,提示等待唤醒
read_text_baidu("等待唤醒...")
face_detected = False
def run_face_detection():
"""运行人脸检测线程,检测成功则标记,否则语音提示失败"""
global face_detection_running, face_detection_success
face_detection_running = True # 标记检测线程已启动
if detect_face(timeout=60): # 检测人脸
face_detection_success = True # 检测成功
else:
text_to_speech_chinese("我无法识别您的面部。如果需要我,请随时叫我。") # 检测失败语音提示
face_detection_running = False # 检测线程结束
def wake_word_detection_loop():
"""唤醒词检测主循环,检测到唤醒词后启动人脸检测和助手"""
global running, face_detected, assistant
global face_detection_running, face_detection_success
if assistant is None: # 检查语音助手是否初始化
print("Error: Assistant not initialized.")
return
while running: # 主循环
if face_detected: # 如果已检测到人脸,等待
time.sleep(2)
continue
if face_detection_success: # 如果人脸检测成功,启动助手线程
face_detected = True
face_detection_success = False # 重置标志
assistant_thread = threading.Thread(target=assistant_mode)
assistant_thread.start()
continue
if not face_detection_running: # 如果没有人脸检测线程在运行
print("Listening for wake word...") # 等待唤醒词
script = audio_to_text() # 语音转文本
if script:
for wake_word in wake_words: # 检查是否包含唤醒词
if wake_word in script:
text_to_speech_chinese("唤醒成功,请靠近并扫描您的面部以继续互动。这是为了您的安全。")
threading.Thread(target=run_face_detection).start() # 启动人脸检测线程
break
else:
print("Wake word not detected.") # 未检测到唤醒词
else:
print("No speech detected.") # 未检测到语音
time.sleep(1) # 防止CPU占用过高
def launch_gui():
"""启动图形界面,优先PyQt5,失败则尝试Kivy,再失败则控制台模式"""
try:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
import sys
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("http://localhost:8080"))
web.show()
sys.exit(app.exec_())
except ImportError:
try:
from kivy.app import App
from kivy.uix.label import Label
class SimpleApp(App):
def build(self):
return Label(text='Display Error, your Smart Mirror is Running\nhttp://localhost:8080')
SimpleApp().run()
except ImportError:
print("GUI frameworks not available - running in console mode")
print("Access the mirror at http://localhost:8080")
import time
while True:
time.sleep(1)
def main() -> None:
"""主入口,初始化各模块并启动主线程"""
global running, assistant, preloaded_face_data
print("Smart Mirror started.")
assistant = VoiceAssistant(
baidu_app_id=os.getenv("BAIDU_APP_ID"),
baidu_api_key=os.getenv("BAIDU_API_KEY"),
baidu_secret_key=os.getenv("BAIDU_SECRET_KEY"),
deepseek_api_key=os.getenv("DEEPSEEK_API_KEY"),
)
preloaded_face_data = preload_face_data()
gui_thread = threading.Thread(target=launch_gui) # 启动GUI线程
voice_thread = threading.Thread(target=wake_word_detection_loop, daemon=True)
voice_thread.start()
gui_thread.start()
try:
while True: # 主线程保持运行
time.sleep(1)
except KeyboardInterrupt:
print("Exiting...")
running = False # 终止主循环
pir_thread.join()
voice_thread.join()
GPIO.cleanup() # 清理GPIO设置
if __name__ == "__main__":
main() # 运行主入口中实现语音识别到关键词和人体靠近都可以实现开启人脸识别功能,整体逻辑和框架不要动,尽量以加入的方式少量修改来完成功能的实现和时间效率的提高