fix: MediaPipe 版本兼容性问题 - 移除初始化时的 mp.solutions 调用

This commit is contained in:
2026-04-17 10:56:40 +08:00
parent 79979b2ffb
commit 2a7ee40cf1

View File

@@ -108,38 +108,26 @@ class PersonManager:
def _init_detectors(self):
"""初始化检测器"""
# MediaPipe 人脸检测(方案1: 参数调整
if self.has_mediapipe:
try:
mp_face_detection = mp.solutions.face_detection
self.face_detector = mp_face_detection.FaceDetection(
model_selection=self.config['mediapipe_model_selection'], # 远距离模型
min_detection_confidence=self.config['mediapipe_min_confidence'] # 降低阈值
)
self.mp_face_detection = mp_face_detection
print(f"[PersonManager] MediaPipe initialized (model={self.config['mediapipe_model_selection']}, conf={self.config['mediapipe_min_confidence']})")
except Exception as e:
print(f"[PersonManager] MediaPipe init failed: {e}")
self.face_detector = None
self.has_mediapipe = False
# MediaPipe 人脸检测(目前不使用,由 YOLO 负责
self.face_detector = None
self.mp_face_detection = None
# OpenCV 人脸检测(备用)
self.cv_face_detector = None
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 Haar Cascade initialized (backup)")
except Exception as e:
self.cv_face_detector = None
print(f"[PersonManager] OpenCV detector init failed: {e}")
# 方案3: YOLO 检测(更准确
# YOLO 检测(主要检测器
self.yolo_detector = None
try:
from ultralytics import YOLO
# 使用轻量级 nano 模型
self.yolo_detector = YOLO('yolov8n.pt') # nano 模型,快速
print("[PersonManager] YOLOv8nano initialized (most accurate)")
self.yolo_detector = YOLO('yolov8n.pt')
print("[PersonManager] YOLOv8nano initialized (primary detector)")
except ImportError:
print("[PersonManager] YOLO not installed. Install with: pip install ultralytics")
except Exception as e:
@@ -229,11 +217,12 @@ class PersonManager:
# 方法2: MediaPipe 人脸关键点
if encoding is None and self.has_mediapipe:
try:
mp_face_mesh = mp.solutions.face_mesh
import mediapipe as mp_local
mp_face_mesh = mp_local.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
min_detection_confidence=self.config.get('mediapipe_min_confidence', 0.3)
min_detection_confidence=self.config.get('yolo_min_confidence', 0.3)
)
rgb_face = cv2.cvtColor(face_region, cv2.COLOR_BGR2RGB)
@@ -330,7 +319,8 @@ class PersonManager:
# 方法2使用 MediaPipe 人脸关键点(推荐)
if self.has_mediapipe:
try:
mp_face_mesh = mp.solutions.face_mesh
import mediapipe as mp_local
mp_face_mesh = mp_local.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)
rgb_face = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
@@ -346,7 +336,8 @@ class PersonManager:
face_mesh.close()
return np.array(features)
except:
except Exception as e:
print(f"[PersonManager] MediaPipe face_mesh failed: {e}")
pass
# 方法3使用颜色直方图最简单备用