From 17d09fd152ecec730946460badc28337fac02d55 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 14 Apr 2025 11:51:30 +0800 Subject: [PATCH] =?UTF-8?q?feature=EF=BC=9A=E5=8A=A0=E5=85=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E5=B0=8F=E6=9C=8B=E5=8F=8B=E4=BA=BA=E8=84=B8?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E4=B8=8E=E5=88=86=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/kid_photo_extractor/main.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/kid_photo_extractor/main.py b/plugins/kid_photo_extractor/main.py index 4f2f36c..a1619ce 100644 --- a/plugins/kid_photo_extractor/main.py +++ b/plugins/kid_photo_extractor/main.py @@ -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'],