# 安保异常行为识别系统 ## 项目概述 基于 YOLO + TensorRT 的安保异常行为识别系统,支持离岗检测和周界入侵检测。 ## 快速开始 ### 1. 安装依赖 ```bash pip install -r requirements.txt ``` ### 2. 生成 TensorRT 引擎 ```bash python scripts/build_engine.py --model models/yolo11n.pt --fp16 ``` ### 3. 配置数据库 编辑 `config.yaml` 配置数据库连接。 ### 4. 启动服务 ```bash python main.py ``` ### 5. 访问前端 打开浏览器访问 `http://localhost:3000` ## 目录结构 ``` project_root/ ├── main.py # FastAPI 入口 ├── config.yaml # 配置文件 ├── requirements.txt # Python 依赖 ├── inference/ │ ├── engine.py # TensorRT 引擎封装 │ ├── stream.py # RTSP 流处理 │ ├── pipeline.py # 推理主流程 │ ├── roi/ │ │ └── roi_filter.py # ROI 过滤 │ └── rules/ │ └── algorithms.py # 规则算法 ├── db/ │ ├── models.py # SQLAlchemy ORM 模型 │ └── crud.py # 数据库操作 ├── api/ │ ├── camera.py # 摄像头管理接口 │ ├── roi.py # ROI 管理接口 │ └── alarm.py # 告警管理接口 ├── utils/ │ ├── logger.py # 日志工具 │ ├── helpers.py # 辅助函数 │ └── metrics.py # Prometheus 监控 ├── frontend/ # React 前端 ├── scripts/ │ └── build_engine.py # TensorRT 引擎构建脚本 ├── tests/ # 单元测试 └── Dockerfile # Docker 配置 ``` ## API 接口 ### 摄像头管理 - `GET /api/cameras` - 获取摄像头列表 - `POST /api/cameras` - 添加摄像头 - `PUT /api/cameras/{id}` - 更新摄像头 - `DELETE /api/cameras/{id}` - 删除摄像头 ### ROI 管理 - `GET /api/camera/{id}/rois` - 获取摄像头 ROI 列表 - `POST /api/camera/{id}/roi` - 添加 ROI - `PUT /api/camera/{id}/roi/{roi_id}` - 更新 ROI - `DELETE /api/camera/{id}/roi/{roi_id}` - 删除 ROI ### 告警管理 - `GET /api/alarms` - 获取告警列表 - `GET /api/alarms/stats` - 获取告警统计 - `PUT /api/alarms/{id}` - 更新告警状态 - `POST /api/alarms/{id}/llm-check` - 触发大模型检查 ### 其他接口 - `GET /api/camera/{id}/snapshot` - 获取实时截图 - `GET /api/camera/{id}/detect` - 获取带检测框的截图 - `GET /api/pipeline/status` - 获取 Pipeline 状态 - `GET /health` - 健康检查 ## 配置说明 ### config.yaml ```yaml database: dialect: "sqlite" # sqlite 或 mysql name: "security_monitor" model: engine_path: "models/yolo11n_fp16_480.engine" imgsz: [480, 480] batch_size: 8 half: true stream: buffer_size: 2 reconnect_delay: 3.0 alert: snapshot_path: "data/alerts" cooldown_sec: 300 ``` ## Docker 部署 ```bash docker-compose up -d ``` ## 监控指标 系统暴露 Prometheus 格式的监控指标: - `camera_count` - 活跃摄像头数量 - `camera_fps{camera_id="*"}` - 各摄像头 FPS - `inference_latency_seconds{camera_id="*"}` - 推理延迟 - `alert_total{camera_id="*", event_type="*"}` - 告警总数 - `event_queue_size` - 事件队列大小