完成查询犯人带出次数
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -273,3 +273,43 @@ class SurAlertRecordService:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to delete alert record: {e}")
|
||||
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
|
||||
Reference in New Issue
Block a user