vietanhlee/Trafic-Monitoring
GitHub: vietanhlee/Trafic-Monitoring
基于 C++ 与 ONNX Runtime 的高性能交通监控方案,实现车辆检测、品牌分类及车牌 OCR 一体化处理。
Stars: 1 | Forks: 0
# 交通监控车牌 OCR (C++ / ONNX Runtime / OpenCV)
基于 C++ 与 ONNX Runtime + OpenCV 的车辆识别、品牌分类及车牌 OCR 项目。
## 1) Pipeline 概述
支持输入:`--image`、`--folder`、`--video`。
每帧/图片的处理流程:
1. 在原始图像上进行 **Vehicle detection (YOLO)**。
2. 从 vehicle bbox 中 **Crop vehicle ROI**。
3. **并行运行 2 个分支**:
- **Brand branch**:对 `car` 进行批量 brand 分类。
- **Plate branch**:
- 对每个 vehicle 进行 plate detect(multi-thread)。
- 将 plate bbox 映射回原始图像。
- crop + preprocess plate(multi-thread)。
- OCR(batch;如果 OCR model 固定 `batch=1`,则按每张图 multi-thread 运行)。
4. **合并结果**:vehicle + plate + OCR text/conf。
5. **绘制叠加层** 并输出图片/视频。
流程图:
```
flowchart TD
A[Input image/folder/video] --> B[Vehicle Detection YOLO]
B --> C[Crop vehicle ROIs]
C --> D[Brand branch - batch]
C --> E[Plate branch - MT detect/map/preprocess + OCR]
D --> F[Merge]
E --> F
F --> G[Draw overlay + Save/Show]
```
ONNX Runtime 已包含在 `third_party/onnxruntime` 中。
## 2) 系统要求
- Linux(推荐 Ubuntu 22.04)
- `apt`
- CMake + 支持 C++23 的编译器
`setup.sh` 已预装:
- `build-essential`
- `cmake`
- `pkg-config`
- `libopencv-dev`
## 3) 快速开始
在项目根目录下执行:
```
./setup.sh
./build.sh
cd build
../run.sh --image ../img/1.jpeg
```
如果脚本尚未具有执行权限:
```
chmod +x setup.sh build.sh run.sh
```
## 4) 构建
默认构建 2 个目标:`main` 和 `benchmark`。
```
./build.sh
```
选项:
- `--build-type `(默认 `Release`)
- `--jobs `
- `--clean`
- `--target `(可重复使用)
示例:
```
./build.sh --build-type Debug --jobs 8
./build.sh --clean --target benchmark
```
输出:
- `out/build/bin/main`
- `out/build/bin/benchmark`
## 5) 运行应用
### Main 模式(默认)
- `--image <图片路径>`
- `--folder <图片文件夹路径>`
- `--video <视频路径>`
选项:
- `--show` / `--no-show`
- `--nosave`(仅适用于 `--image`、`--video`)
示例:
```
../run.sh --image ../img/1.jpeg
../run.sh --image ../img/1.jpeg --nosave
../run.sh --folder ../img
../run.sh --video ../video.mp4 --show --nosave
```
视频说明:
- 按 `app_config::kVideoInferEveryNFrames` 周期(默认为 5)进行 infer,周期中间的帧复用最近的 overlay。
### Benchmark 模式
从 `build` 目录运行:
```
../run.sh --benchmark --image ../img/10.jpeg --warmup 3 --runs 10
```
如果尚未构建,`run.sh` 会提示先运行 `./build.sh`。
## 6) Benchmark 指标含义
- `vehicle detect`:在原始图像上 detect vehicle 的时间。
- `vehicle crop`:准备 crop vehicle 的时间。
- `brand branch`:brand 分支的总时间。
- `brand classify`:纯 brand 模型 infer 时间。
- `plate branch`:plate 分支的总时间。
- `plate detect`:在 vehicle crops 上 detect plate 的时间。
- `plate map`:将 plate bbox 映射回原始图像。
- `plate crop/pre`:为 OCR 进行 crop + preprocess plate。
- `plate ocr`:OCR 时间。
- `merge`:合并最终结果。
- `total pipeline`:端到端 infer 一次的时间。
由于 brand/plate 并行运行:
`total` 接近 `vehicle + crop + max(brand_branch, plate_branch) + merge + overhead`。
## 7) 配置
在 `include/app_config.h` 中设置:
- `kVehicleModelPath`
- `kPlateModelPath`
- `kBrandCarModelPath`
- `kOcrModelPath`
- `kVehicleConfThresh`
- `kPlateConfThresh`
- `kNmsIouThresh`
- `kOcrConfAvgThresh`
## 8) Docker
```
docker build -t traffic-monitoring .
docker run --rm -v "$PWD/img:/app/img" traffic-monitoring --image /app/img/1.jpeg
docker run --rm -v "$PWD/img:/app/img" --entrypoint /app/benchmark traffic-monitoring --image /app/img/1.jpeg --warmup 5 --runs 10
```
## 9) 代码结构
- `src/main.cpp`:CLI + image/folder/video 流程。
- `src/frame_annotator.cpp`:infer overlay + draw。
- `src/benchmark.cpp`:分阶段 benchmark。
- `src/yolo_detector.cpp`:YOLO infer(batch + single)。
- `src/ocr_batch.cpp`:OCR batch,当 model 固定 `batch=1` 时 fallback 到 multi-thread。
- `src/brand_classifier.cpp`:brand 分类。
- `src/utils/plate_parallel.cpp`:并行 plate detect/map/preprocess 工具。
- `src/utils/parallel_utils.cpp`:共享 worker 数量计算工具。
标签:Bash脚本, C++, CMake, ITS, OCR, ONNX Runtime, OpenCV, YOLO, 交通监控, 品牌分类, 图像处理, 数据擦除, 智慧交通, 深度学习推理, 目标检测, 视频分析, 计算机视觉, 请求拦截, 车牌检测, 车牌识别, 车辆检测