fix: MediaPipe 版本兼容性问题 - 移除初始化时的 mp.solutions 调用
This commit is contained in:
+14
-23
@@ -108,38 +108,26 @@ class PersonManager:
|
|||||||
|
|
||||||
def _init_detectors(self):
|
def _init_detectors(self):
|
||||||
"""初始化检测器"""
|
"""初始化检测器"""
|
||||||
# MediaPipe 人脸检测(方案1: 参数调整)
|
# MediaPipe 人脸检测(目前不使用,由 YOLO 负责)
|
||||||
if self.has_mediapipe:
|
self.face_detector = None
|
||||||
try:
|
self.mp_face_detection = None
|
||||||
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
|
|
||||||
|
|
||||||
# OpenCV 人脸检测(备用)
|
# OpenCV 人脸检测(备用)
|
||||||
|
self.cv_face_detector = None
|
||||||
try:
|
try:
|
||||||
model_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
model_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
||||||
if Path(model_path).exists():
|
if Path(model_path).exists():
|
||||||
self.cv_face_detector = cv2.CascadeClassifier(model_path)
|
self.cv_face_detector = cv2.CascadeClassifier(model_path)
|
||||||
print("[PersonManager] OpenCV Haar Cascade initialized (backup)")
|
print("[PersonManager] OpenCV Haar Cascade initialized (backup)")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.cv_face_detector = None
|
|
||||||
print(f"[PersonManager] OpenCV detector init failed: {e}")
|
print(f"[PersonManager] OpenCV detector init failed: {e}")
|
||||||
|
|
||||||
# 方案3: YOLO 检测(更准确)
|
# YOLO 检测(主要检测器)
|
||||||
self.yolo_detector = None
|
self.yolo_detector = None
|
||||||
try:
|
try:
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
# 使用轻量级 nano 模型
|
self.yolo_detector = YOLO('yolov8n.pt')
|
||||||
self.yolo_detector = YOLO('yolov8n.pt') # nano 模型,快速
|
print("[PersonManager] YOLOv8nano initialized (primary detector)")
|
||||||
print("[PersonManager] YOLOv8nano initialized (most accurate)")
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("[PersonManager] YOLO not installed. Install with: pip install ultralytics")
|
print("[PersonManager] YOLO not installed. Install with: pip install ultralytics")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -229,11 +217,12 @@ class PersonManager:
|
|||||||
# 方法2: MediaPipe 人脸关键点
|
# 方法2: MediaPipe 人脸关键点
|
||||||
if encoding is None and self.has_mediapipe:
|
if encoding is None and self.has_mediapipe:
|
||||||
try:
|
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(
|
face_mesh = mp_face_mesh.FaceMesh(
|
||||||
static_image_mode=True,
|
static_image_mode=True,
|
||||||
max_num_faces=1,
|
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)
|
rgb_face = cv2.cvtColor(face_region, cv2.COLOR_BGR2RGB)
|
||||||
@@ -330,7 +319,8 @@ class PersonManager:
|
|||||||
# 方法2:使用 MediaPipe 人脸关键点(推荐)
|
# 方法2:使用 MediaPipe 人脸关键点(推荐)
|
||||||
if self.has_mediapipe:
|
if self.has_mediapipe:
|
||||||
try:
|
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)
|
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)
|
||||||
|
|
||||||
rgb_face = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
|
rgb_face = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
|
||||||
@@ -346,7 +336,8 @@ class PersonManager:
|
|||||||
|
|
||||||
face_mesh.close()
|
face_mesh.close()
|
||||||
return np.array(features)
|
return np.array(features)
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(f"[PersonManager] MediaPipe face_mesh failed: {e}")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 方法3:使用颜色直方图(最简单,备用)
|
# 方法3:使用颜色直方图(最简单,备用)
|
||||||
|
|||||||
Reference in New Issue
Block a user