From cb089ca3be8a1549ccb3478b17e5d37930e96fed Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Fri, 9 Jan 2026 16:12:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AD=A6=E5=91=8A=E6=8F=92=E5=85=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=A7=BB=E5=85=A5biz=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/video_face_prison_biz.py | 39 +++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/biz/video_face_prison_biz.py b/biz/video_face_prison_biz.py index 325a115..41e7c4e 100644 --- a/biz/video_face_prison_biz.py +++ b/biz/video_face_prison_biz.py @@ -10,12 +10,27 @@ import time from insightface.app import FaceAnalysis from biz.base_face_biz import BaseFaceBiz +from utils.logger import setup_logger + +logger = setup_logger(__name__) + +# 导入数据库相关模块 +try: + from services.sur_alert_record_service import SurAlertRecordService + from database.connection import db_manager + from models.sur_alert_record import AlertType + logger.debug("[INFO] 成功导入数据库模块") +except Exception as e: + logger.error(f"[WARN] 无法导入数据库模块: {e}") + class VideoFacePrisonBiz(BaseFaceBiz): """ 视频检查业务类 - RTSP专用 专门处理RTSP视频流中的人脸识别和检测 """ + #todo: 目前的设计是只有一个摄像头的情况,需要修改成支持多个摄像头,应该是要修改实例化的地方。放到map里,cam_id为key + def __init__(self, face_analysis: FaceAnalysis): """ 初始化视频检查业务类 @@ -165,11 +180,11 @@ class VideoFacePrisonBiz(BaseFaceBiz): # 检查是否在冷却期内 if person_id in self.person_cooldown: if current_time <= self.person_cooldown[person_id]: - print(f"{person_id} in cooldown") + logger.debug(f"{person_id} in cooldown") # 还在冷却期内,忽略此人 return False, None else: - print(f"{person_id} cooldown expired, remove") + logger.debug(f"{person_id} cooldown expired, remove") # 冷却期结束,删除记录 del self.person_cooldown[person_id] @@ -185,10 +200,10 @@ class VideoFacePrisonBiz(BaseFaceBiz): self.person_cooldown[person_id] = current_time + self.cooldown_seconds # 清空该人员的匹配记录 del self.person_tracking[person_id] - print(f"{person_id} passed") + logger.debug(f"{person_id} passed") return True, person_id else: - print(f"{person_id} not enough matches, count: {len(recent_matches)}") + logger.debug(f"{person_id} not enough matches, count: {len(recent_matches)}") return False, None @@ -215,7 +230,7 @@ class VideoFacePrisonBiz(BaseFaceBiz): # 查找最佳匹配 best_name, similarity = self.find_best_match(face.embedding) - # print(f"best_name: {best_name}, similarity: {similarity}") + # logger.debug(f"best_name: {best_name}, similarity: {similarity}") is_match = best_name is not None and similarity >= self.similarity_threshold # 新增:判断是否已经通过 @@ -230,6 +245,20 @@ class VideoFacePrisonBiz(BaseFaceBiz): self.person_tracking[best_name] = [] self.person_tracking[best_name].append(current_time) + if has_passed: + # 插入数据库告警记录 + try: + with db_manager.get_session() as db: + alert_service = SurAlertRecordService(db) + alert_service.create_alert_record( + alert_type=AlertType.PRISONER_OUT, + person_id=int(passed_person_id), + # camera_id=cam_id #todo:设置cam_id,如果每个摄像头创建一个实例,则可以将cam_id放到成员变量里 + ) + # logger.debug(f"[INFO] 告警记录已插入数据库: person_id={result['passed_person_id']}") + except Exception as e: + logger.error(f"[ERROR] 插入告警记录失败: {e}") + result = { 'bbox': face.bbox.astype(int).tolist(), 'similarity': similarity,