diff --git a/person_manager.py b/person_manager.py index 50a1a95..8f2a7f7 100644 --- a/person_manager.py +++ b/person_manager.py @@ -42,6 +42,12 @@ class PersonManager: # 加载人员库 self.persons = self._load_persons_db() + # 检测器状态 + self.face_detector = None + self.mp_face_detection = None + self.cv_face_detector = None + self.has_mediapipe = HAS_MEDIAPIPE + # 初始化检测器 self._init_detectors() @@ -75,22 +81,30 @@ class PersonManager: def _init_detectors(self): """初始化检测器""" # MediaPipe 人脸检测 - if HAS_MEDIAPIPE: - self.mp_face_detection = mp.solutions.face_detection - self.face_detector = self.mp_face_detection.FaceDetection( - model_selection=0, # 0: 短距离,1: 远距离 - min_detection_confidence=0.5 - ) - print("[PersonManager] MediaPipe face detector initialized") - else: - # OpenCV DNN 人脸检测 + if self.has_mediapipe: try: - model_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' + # 使用更安全的导入方式 + mp_face_detection = mp.solutions.face_detection + self.face_detector = mp_face_detection.FaceDetection( + model_selection=0, # 0: 短距离,1: 远距离 + min_detection_confidence=0.5 + ) + self.mp_face_detection = mp_face_detection + print("[PersonManager] MediaPipe face detector initialized") + except Exception as e: + print(f"[PersonManager] MediaPipe init failed: {e}") + self.face_detector = None + self.has_mediapipe = False + + # OpenCV 人脸检测(备用) + try: + model_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' + if Path(model_path).exists(): self.cv_face_detector = cv2.CascadeClassifier(model_path) - print("[PersonManager] OpenCV face detector initialized") - except: - self.cv_face_detector = None - print("[PersonManager] No face detector available") + print("[PersonManager] OpenCV face detector initialized (backup)") + except Exception as e: + self.cv_face_detector = None + print(f"[PersonManager] OpenCV face detector init failed: {e}") def detect_faces(self, image): """检测人脸 @@ -110,7 +124,7 @@ class PersonManager: faces = [] # MediaPipe 检测 - if HAS_MEDIAPIPE and hasattr(self, 'face_detector'): + if self.has_mediapipe and self.face_detector is not None: rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = self.face_detector.process(rgb_image) @@ -190,7 +204,7 @@ class PersonManager: pass # 方法2:使用 MediaPipe 人脸关键点(推荐) - if HAS_MEDIAPIPE: + if self.has_mediapipe: try: mp_face_mesh = mp.solutions.face_mesh face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1) diff --git a/start.bat b/start.bat index 9116e2a..7f64aaf 100644 --- a/start.bat +++ b/start.bat @@ -2,29 +2,29 @@ chcp 65001 >nul echo ================================ -echo 视觉记录系统启动 (Windows) +echo Vision Record System (Windows) echo ================================ cd /d %~dp0 -echo 检查 Python 环境... +echo Checking Python... python --version if errorlevel 1 ( - echo 错误: 未找到 Python,请先安装 Python 3.10+ + echo ERROR: Python not found pause exit /b 1 ) -echo 检查依赖... +echo Checking dependencies... pip show opencv-python >nul 2>&1 if errorlevel 1 ( - echo 安装依赖... + echo Installing dependencies... pip install -r requirements.txt ) -echo 启动服务... -echo 注意:此窗口请勿点击或拖动,否则会暂停 -echo 如需交互请另开命令行窗口 +echo Starting service... +echo WARNING: Do NOT click or drag this window +echo Open another terminal if you need to interact echo ================================ python main.py \ No newline at end of file