# 边检CV算法接口服务 ### 技术栈 - **后端框架**: FastAPI - **数据库**: SQLite (SQLAlchemy ORM) - **AI模型**: YOLOv11n - **进程管理**: Supervisor - **开发语言**: Python 3.8+ ### 项目结构 ``` server/ ├── app.py # FastAPI主应用 ├── start.py # 启动脚本 ├── requirements.txt # Python依赖 ├── env.example # 环境变量示例 ├── init_data.py # 示例数据初始化 ├── core/ # 核心配置 │ └── database.py # 数据库配置 ├── models/ # 数据模型 │ ├── __init__.py │ ├── base.py # 基础模型 │ ├── algorithm.py # 算法模型 │ ├── device.py # 设备模型 │ └── event.py # 事件模型 ├── schemas/ # Pydantic模型 │ ├── __init__.py │ ├── algorithm.py # 算法相关模型 │ ├── device.py # 设备相关模型 │ └── event.py # 事件相关模型 └── routers/ # API路由 ├── __init__.py ├── algorithms.py # 算法管理接口 ├── devices.py # 设备管理接口 └── events.py # 事件管理接口 ``` ### 功能模块 #### 1. 算法管理模块 - 算法的增删改查 - 算法状态管理(启用/禁用) - 算法版本管理 - 算法性能指标(准确率、推理时间等) #### 2. 设备管理模块 - 设备信息管理(摄像头、传感器等) - 设备状态监控 - 设备类型管理 - 设备地理位置信息 #### 3. 事件管理模块 - 事件记录和查询 - 事件状态管理 - 事件统计分析 - 告警管理 ### API接口 #### 算法管理接口 - `POST /api/algorithms/` - 创建算法 - `GET /api/algorithms/` - 获取算法列表 - `GET /api/algorithms/{id}` - 获取算法详情 - `PUT /api/algorithms/{id}` - 更新算法 - `DELETE /api/algorithms/{id}` - 删除算法 - `PATCH /api/algorithms/{id}/status` - 更新算法状态 - `PATCH /api/algorithms/{id}/enable` - 启用/禁用算法 #### 设备管理接口 - `POST /api/devices/` - 创建设备 - `GET /api/devices/` - 获取设备列表 - `GET /api/devices/{id}` - 获取设备详情 - `PUT /api/devices/{id}` - 更新设备 - `DELETE /api/devices/{id}` - 删除设备 - `PATCH /api/devices/{id}/status` - 更新设备状态 - `PATCH /api/devices/{id}/enable` - 启用/禁用设备 - `GET /api/devices/types/list` - 获取设备类型列表 - `GET /api/devices/status/stats` - 获取设备状态统计 #### 事件管理接口 - `POST /api/events/` - 创建事件 - `GET /api/events/` - 获取事件列表 - `GET /api/events/{id}` - 获取事件详情 - `PUT /api/events/{id}` - 更新事件 - `DELETE /api/events/{id}` - 删除事件 - `PATCH /api/events/{id}/status` - 更新事件状态 - `GET /api/events/types/list` - 获取事件类型列表 - `GET /api/events/stats/summary` - 获取事件统计摘要 - `GET /api/events/stats/by-type` - 按类型统计事件 ### 安装和运行 #### 1. 环境准备 ```bash # 创建虚拟环境 conda create -n border_inspection python=3.8 conda activate border_inspection # 安装依赖 pip install -r requirements.txt ``` #### 2. 初始化数据库 ```bash # 启动服务(会自动创建数据库表) python app.py ``` #### 3. 初始化示例数据 ```bash python init_data.py ``` #### 4. 启动服务 ```bash python app.py ``` 服务将在 `http://localhost:8000` 启动 ### API文档 启动服务后,可以访问以下地址查看API文档: - Swagger UI: `http://localhost:8000/docs` - ReDoc: `http://localhost:8000/redoc` ### 数据库设计 #### 算法表 (algorithms) - 基本信息:名称、描述、版本 - 模型信息:模型路径、配置文件路径 - 性能指标:准确率、推理时间、输入尺寸 - 状态管理:启用状态、算法状态 - 分类信息:检测类别、标签 #### 设备表 (devices) - 基本信息:名称、类型、位置 - 连接信息:IP地址、端口、用户名、密码 - 视频流:RTSP地址、分辨率、帧率 - 状态信息:在线状态、最后心跳时间 - 地理位置:经纬度坐标 #### 事件表 (events) - 事件信息:类型、设备ID、算法ID - 检测结果:置信度、边界框、检测对象 - 状态管理:事件状态、严重程度 - 告警信息:是否告警、告警发送状态 - 处理信息:处理人员、处理备注、解决时间 ### 开发说明 1. **添加新模型**: 在 `models/` 目录下创建新的模型文件 2. **添加新接口**: 在 `routers/` 目录下创建新的路由文件 3. **添加新Schema**: 在 `schemas/` 目录下创建新的Pydantic模型 4. **数据库迁移**: 修改模型后重启服务,数据库表会自动更新 ### 部署说明 #### 使用Supervisor管理进程 ```ini [program:border_inspection] command=python /path/to/server/start.py directory=/path/to/server user=www-data autostart=true autorestart=true stderr_logfile=/var/log/border_inspection.err.log stdout_logfile=/var/log/border_inspection.out.log ``` #### 环境变量配置 复制 `env.example` 为 `.env` 并修改相应配置: ```bash cp env.example .env # 编辑 .env 文件 ```