diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..dbfb081 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,29 @@ +.git +__pycache__ +*.pyc +*.pyo +.idea +.vscode +.env +.env.* +!.env.example + +# 模型和数据通过卷挂载 +models/ +data/ +logs/ + +# 测试文件 +tests/ +test_*.py +pytest.ini + +# 文档 +docs/ +*.md +!CLAUDE.md + +# 临时文件 +*.engine +*.onnx +*.pt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6ee940f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,51 @@ +# ============================================================ +# 基础镜像:NVIDIA TensorRT 23.08 +# 内含:CUDA 12.1.1 | cuDNN 8.9.3 | TensorRT 8.6.1.6 | Python 3.10 +# ============================================================ +FROM nvcr.io/nvidia/tensorrt:23.08-py3 + +LABEL maintainer="AI Edge Architecture Team" +LABEL description="Edge AI Inference Service - YOLOv11n + TensorRT" + +# 设置时区 +ENV TZ=Asia/Shanghai +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +# 系统依赖(视频解码、OpenCV 运行时) +RUN apt-get update && apt-get install -y --no-install-recommends \ + ffmpeg \ + libsm6 \ + libxext6 \ + libgl1-mesa-glx \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +# 先复制依赖文件,利用 Docker 层缓存 +COPY requirements.txt . + +# 安装 PyTorch(CUDA 12.1 版本)+ 其余依赖 +RUN pip install --no-cache-dir \ + torch==2.1.2 torchvision==0.16.2 \ + --index-url https://download.pytorch.org/whl/cu121 \ + && pip install --no-cache-dir -r requirements.txt + +# 复制项目代码 +COPY __init__.py . +COPY main.py . +COPY algorithms.py . +COPY build_engine.py . +COPY config/ ./config/ +COPY core/ ./core/ +COPY utils/ ./utils/ + +# 模型和数据通过卷挂载,不打入镜像 +# -v /path/to/models:/app/models +# -v /path/to/data:/app/data + +# 日志目录 +RUN mkdir -p /app/logs /app/data + +EXPOSE 9001 + +CMD ["python", "main.py"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9a1f969 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,46 @@ +version: "3.8" + +services: + edge-inference: + build: . + image: edge-inference:latest + container_name: edge-inference + restart: always + + # GPU 访问 + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + + # 环境变量 + env_file: + - .env + + # 卷挂载 + volumes: + - ./models:/app/models # TensorRT 引擎文件 + - ./data:/app/data # SQLite + 截图缓存 + - ./logs:/app/logs # 运行日志 + - ./.env:/app/.env # 环境配置 + + # 网络(需要访问摄像头 RTSP + 云端 API + Redis) + network_mode: host + + # 健康检查 + healthcheck: + test: ["CMD", "python", "-c", "import os; assert os.path.exists('/app/main.py')"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + + # 日志限制 + logging: + driver: json-file + options: + max-size: "50m" + max-file: "5" diff --git a/requirements.txt b/requirements.txt index f081142..0e42c09 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,64 +1,78 @@ # Edge_Inference_Service 依赖清单 # 安装命令: pip install -r requirements.txt -# 备注:所有版本均选择最稳定版本,经过大量验证 +# 环境要求: Python 3.10 | CUDA 12.1 | cuDNN 8.9 | TensorRT 8.6.1 +# Docker 基础镜像: nvcr.io/nvidia/tensorrt:23.08-py3 # ============================================================ -# 核心依赖(必需) +# GPU 推理依赖(TensorRT 8.6 + CUDA 12.1) # ============================================================ -# 视频处理 - OpenCV 4.8.0,最稳定的4.x版本 -opencv-python==4.8.0.74 +# PyTorch - CUDA 12.1 下最稳定版本 +# 安装命令: pip install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/cu121 +--extra-index-url https://download.pytorch.org/whl/cu121 +torch==2.1.2 +torchvision==0.16.2 -# 数值计算 - NumPy 1.24.0,Python 3.8-3.11完美兼容 -numpy==1.24.0 +# TensorRT Python 绑定(NGC 镜像已预装,裸机需手动安装) +tensorrt==8.6.1.6 +pycuda==2023.1.1 + +# YOLO11 目标检测框架 +ultralytics==8.3.5 + +# ONNX 模型转换与优化 +onnx==1.16.0 +onnxsim==0.4.36 +onnxruntime-gpu==1.17.1 + +# ============================================================ +# 核心依赖 +# ============================================================ + +# 视频处理 +opencv-python==4.8.0.76 + +# 数值计算(锁定 1.x,避开 NumPy 2.0 破坏性变更) +numpy==1.26.4 + +# 图像处理 +Pillow==10.2.0 # ============================================================ # 数据库依赖 # ============================================================ -# ORM框架 - SQLAlchemy 2.0.23,长期支持稳定版 +# ORM 框架 sqlalchemy==2.0.23 -# MySQL驱动 - PyMySQL 1.1.0,成熟稳定版本 +# MySQL 驱动 pymysql==1.1.0 # ============================================================ # 消息队列与缓存 # ============================================================ -# MQTT客户端 - Paho-MQTT 1.6.1,1.x最终稳定版 +# MQTT 客户端(1.x 最终稳定版) paho-mqtt==1.6.1 -# Redis客户端 - Redis 4.6.0,4.x最终稳定版 +# Redis 客户端 redis==4.6.0 -# 腾讯云COS SDK - 用于截图上传 +# 腾讯云 COS SDK(截图上传) cos-python-sdk-v5>=1.9.30 # ============================================================ # 工具库 # ============================================================ -# YAML解析 - PyYAML 6.0.1,安全稳定版 pyyaml==6.0.1 +requests==2.31.0 +psutil==5.9.8 +python-dotenv==1.0.1 # ============================================================ -# 测试框架 +# 测试依赖 # ============================================================ -# 单元测试 - PyTest 7.4.4,7.x最终稳定版 pytest==7.4.4 - -# 覆盖率报告 - PyTest-Cov 4.1.0,成熟稳定版 pytest-cov==4.1.0 - -# ============================================================ -# 可选依赖(按需安装) -# ============================================================ - -# GPU推理框架(需要CUDA 12.1环境) -# tensorrt==8.6.1.6 -# pycuda==2023.1.1 - -# YOLOv8目标检测(按需安装) -# ultralytics==8.0.228