From eabf1e5d2c1db7e116c5ea5fc8e8b200c87a1421 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 14 Apr 2025 12:16:09 +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 | 43 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/plugins/kid_photo_extractor/main.py b/plugins/kid_photo_extractor/main.py index 2a0b79b..d7f6f08 100644 --- a/plugins/kid_photo_extractor/main.py +++ b/plugins/kid_photo_extractor/main.py @@ -74,18 +74,19 @@ class FaceAnalyzer: def analyze_face(self, image_path, face_area=None): """分析人脸,获取特征向量""" + temp_path = None try: # 检查文件是否存在 if not os.path.exists(image_path): self.logger.error(f"图片文件不存在: {image_path}") - return None + return None, None # 检查文件是否可读 try: img = cv2.imread(image_path) if img is None: self.logger.error(f"无法读取图片: {image_path}") - return None + return None, None # 如果指定了人脸区域,裁剪图片 if face_area: @@ -98,7 +99,7 @@ class FaceAnalyzer: except Exception as e: self.logger.error(f"读取图片失败: {image_path}, 错误: {e}") - return None + return None, None # 提取人脸特征向量用于后续比对 embedding_result = DeepFace.represent( @@ -127,26 +128,20 @@ class FaceAnalyzer: embedding = [float(x) for x in embedding] except (TypeError, ValueError): self.logger.error(f"无法将嵌入向量转换为浮点数列表: {image_path}") - return None + return None, temp_path self.logger.info(f"成功提取人脸特征向量: {image_path}") - # 如果使用了临时文件,删除它 - 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 { 'embedding': embedding, 'is_kid': True # 默认所有人脸都处理 - } + }, temp_path + except Exception as e: self.logger.error(f"人脸分析失败: {image_path}, 错误: {e}") self.logger.error(traceback.format_exc()) # 打印完整的错误堆栈 - return None + return None, temp_path def is_kid(self, face_info): """判断是否为小朋友 - 现在总是返回True""" @@ -529,6 +524,8 @@ class KidPhotoExtractorPlugin(MessagePluginInterface): def _run_analysis_task(self, group_key, source_dir, output_dir, wcf, target, sender): """在后台运行分析任务""" start_time = time.time() + temp_files = [] # 用于跟踪所有创建的临时文件 + try: is_full = self.analysis_tasks[group_key].get("is_full", False) self.LOG.info(f"开始{'全量' if is_full else '增量'}分析任务: {source_dir}") @@ -623,9 +620,13 @@ class KidPhotoExtractorPlugin(MessagePluginInterface): for face in faces: # 分析人脸 face_region = face.get('facial_area', None) - face_info = self.face_analyzer.analyze_face(image_path, face_region) + face_info, temp_file = self.face_analyzer.analyze_face(image_path, face_region) + + # 如果创建了临时文件,添加到跟踪列表 + if temp_file: + temp_files.append(temp_file) - if face_info: # 不再判断是否为小朋友 + if face_info: # 保存人脸特征 all_faces.append(face_info['embedding']) face_images.append(image_path) @@ -751,6 +752,16 @@ class KidPhotoExtractorPlugin(MessagePluginInterface): # 强制垃圾回收 import gc gc.collect() + + def _cleanup_temp_files(self, temp_files): + """清理所有临时文件""" + for temp_file in temp_files: + try: + if os.path.exists(temp_file): + os.remove(temp_file) + self.LOG.info(f"已删除临时文件: {temp_file}") + except Exception as e: + self.LOG.error(f"删除临时文件失败: {temp_file}, 错误: {e}") def _save_analysis_result(self, group_key, result): """保存分析结果到Redis"""