From ec96e161bc77216abc51d35d4d17985650404381 Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Fri, 9 Jan 2026 17:33:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=9F=A5=E8=AF=A2=E7=8A=AF?= =?UTF-8?q?=E4=BA=BA=E5=B8=A6=E5=87=BA=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/video_face_prison_biz.py | 20 ++++++++++--- services/sur_alert_record_service.py | 42 +++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/biz/video_face_prison_biz.py b/biz/video_face_prison_biz.py index 88a13f6..212f3bb 100644 --- a/biz/video_face_prison_biz.py +++ b/biz/video_face_prison_biz.py @@ -264,19 +264,30 @@ class VideoFacePrisonBiz(BaseFaceBiz): self.person_tracking[best_name] = [] self.person_tracking[best_name].append(current_time) - if has_passed: - # 插入数据库告警记录 + # 查询历史报警记录数 + historical_alert_count = 0 + if has_passed and passed_person_id: 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']}") + + # 查询近escort_window_hours小时内的PRISONER_OUT记录数 + historical_alert_count = alert_service.get_alert_count_by_person_and_time( + person_id=int(passed_person_id), + alert_type=AlertType.PRISONER_OUT, + hours=self.escort_window_hours + ) + + logger.info(f"[INFO] 告警记录已插入数据库: person_id={passed_person_id}, 历史记录数: {historical_alert_count}") except Exception as e: - logger.error(f"[ERROR] 插入告警记录失败: {e}") + logger.error(f"[ERROR] 数据库操作失败: {e}") result = { 'bbox': face.bbox.astype(int).tolist(), @@ -285,6 +296,7 @@ class VideoFacePrisonBiz(BaseFaceBiz): 'is_match': is_match, 'has_passed': has_passed, # 新增:是否已经通过 'passed_person_id': passed_person_id, # 新增:通过的person_id + 'historical_alert_count': historical_alert_count, # 新增:历史报警记录数 'det_score': float(face.det_score), 'quality_metrics': quality_metrics, 'is_acceptable': is_acceptable diff --git a/services/sur_alert_record_service.py b/services/sur_alert_record_service.py index 08765e0..3aad542 100644 --- a/services/sur_alert_record_service.py +++ b/services/sur_alert_record_service.py @@ -272,4 +272,44 @@ class SurAlertRecordService: except Exception as e: self.db.rollback() logger.error(f"Failed to delete alert record: {e}") - return False \ No newline at end of file + return False + + def get_alert_count_by_person_and_time( + self, + person_id: int, + alert_type: int, + hours: int + ) -> int: + """ + 查询指定人员和时间范围内的告警记录数 + + Args: + person_id: 人员ID + alert_type: 告警类型 + hours: 小时数(从当前时间往前推算) + + Returns: + 告警记录数 + """ + from datetime import datetime, timedelta + + # 计算时间范围 + end_time = datetime.now() + start_time = end_time - timedelta(hours=hours) + + try: + count = ( + self.db.query(SurAlertRecord) + .filter( + SurAlertRecord.person_id == person_id, + SurAlertRecord.alert_type == alert_type, + SurAlertRecord.created_time >= start_time, + SurAlertRecord.created_time <= end_time + ) + .count() + ) + logger.debug(f"查询到人员 {person_id} 在最近 {hours} 小时内类型 {alert_type} 的告警记录数: {count}") + return count + except Exception as e: + logger.error(f"查询告警记录数失败: {e}") + return 0 \ No newline at end of file