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