feature:加入功能,小朋友人脸识别与分类。
This commit is contained in:
@@ -57,7 +57,6 @@ class FaceAnalyzer:
|
||||
self.logger.error(f"人脸检测失败: {image_path}, 错误: {e}")
|
||||
return []
|
||||
|
||||
# 在 FaceAnalyzer 类的 analyze_face 方法中,需要增强错误处理
|
||||
def analyze_face(self, image_path, face_area=None):
|
||||
"""分析人脸,获取年龄和特征向量"""
|
||||
try:
|
||||
@@ -72,21 +71,36 @@ class FaceAnalyzer:
|
||||
if img is None:
|
||||
self.logger.error(f"无法读取图片: {image_path}")
|
||||
return None
|
||||
|
||||
# 如果指定了人脸区域,裁剪图片
|
||||
if face_area:
|
||||
x, y, w, h = face_area['x'], face_area['y'], face_area['w'], face_area['h']
|
||||
img = img[y:y+h, x:x+w]
|
||||
# 保存临时裁剪图片
|
||||
temp_path = f"{image_path}.temp.jpg"
|
||||
cv2.imwrite(temp_path, img)
|
||||
image_path = temp_path
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"读取图片失败: {image_path}, 错误: {e}")
|
||||
return None
|
||||
|
||||
# 分析人脸属性
|
||||
analysis = DeepFace.analyze(img_path=image_path,
|
||||
actions=['age', 'gender'],
|
||||
enforce_detection=False,
|
||||
region=face_area)
|
||||
actions=['age', 'gender'],
|
||||
enforce_detection=False)
|
||||
|
||||
# 提取人脸特征向量用于后续比对
|
||||
embedding = DeepFace.represent(img_path=image_path,
|
||||
model_name='Facenet',
|
||||
enforce_detection=False,
|
||||
region=face_area)
|
||||
model_name='Facenet',
|
||||
enforce_detection=False)
|
||||
|
||||
# 如果使用了临时文件,删除它
|
||||
if face_area and os.path.exists(f"{image_path}.temp.jpg"):
|
||||
try:
|
||||
os.remove(f"{image_path}.temp.jpg")
|
||||
except Exception as e:
|
||||
self.logger.warning(f"删除临时文件失败: {e}")
|
||||
|
||||
return {
|
||||
'age': analysis[0]['age'],
|
||||
|
||||
Reference in New Issue
Block a user