From b8ceaf5c1cb0ab1a56c19dd3f6695327ab1b62c5 Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Wed, 20 May 2026 08:30:13 +0700 Subject: [PATCH] edit src/hik_predict_trt.cpp --- .gitignore | 1 + README.md | 275 +++++++++++++++++++++++++++++++++++----- src/hik_predict_trt.cpp | 2 +- 3 files changed, 244 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index fff9750..5b32f2b 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ runs/ # Vendored/external repositories third_party/ agent_skills_codex/ +docs/ # Python caches and virtual environments __pycache__/ diff --git a/README.md b/README.md index 654e9f7..cc38244 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,247 @@ # Quartz Inspection Core -C++ TensorRT realtime inspection app for the Hikvision camera pipeline.https://marketplace.cursorapi.com/open-vsx-mirror/vscode/asset/openai/chatgpt/26.5506.31421/Microsoft.VisualStudio.Services.Icons.Default?targetPlatform=linux-x64 +C++ TensorRT realtime inspection app for the Hikvision camera pipeline. This folder is intentionally separate from the Python training and prediction code. It contains its own copied `runs/` model artifacts while adding a faster runtime path based on `Geekgineer/YOLOs-CPP-TensorRT`. -## Layout +## What This Builds + +The CMake project builds one executable: + +- `build/hik_predict_trt`: Hikvision MVS camera capture + YOLO26 segmentation inference through TensorRT. + +The runtime uses: + +- `src/hik_predict_trt.cpp` for camera capture, preprocessing fallbacks, inference, drawing, and capture saving. +- `third_party/YOLOs-CPP-TensorRT/include/yolos/tasks/segmentation.hpp` for TensorRT segmentation inference. +- `third_party/YOLOs-CPP-TensorRT/include/yolos/core/cuda_preprocessing.cu`, compiled by this repository as `yolos_cuda_preprocessing`. +- `models/yolo26_tile_seg_v3_fp16.trt` as the default TensorRT engine. +- `config/labels.names` as the default class-name file. + +## Repository Layout - `src/hik_predict_trt.cpp`: Hikvision camera + YOLO26 segmentation TensorRT runtime. - `config/labels.names`: class names used by the C++ runtime. -- `scripts/export_yolo26_seg_onnx.py`: exports `runs/segment/yolo26_tile_seg_v3/weights/best.pt` to ONNX. +- `runs/segment/yolo26_tile_seg_v3/weights/best.pt`: default PyTorch checkpoint used for ONNX export. +- `runs/segment/yolo26_tile_seg_v3/weights/best.onnx`: copied by the export script when present. +- `models/yolo26_tile_seg_v3.onnx`: default ONNX input for TensorRT engine generation. +- `models/yolo26_tile_seg_v3_fp16.trt`: default TensorRT engine path. +- `scripts/export_yolo26_seg_onnx.py`: exports or copies the YOLO26 segmentation model to ONNX. - `scripts/build_engine.sh`: builds an FP16 TensorRT engine with `trtexec`. -- `third_party/YOLOs-CPP-TensorRT`: cloned dependency. +- `third_party/YOLOs-CPP-TensorRT`: required C++ TensorRT dependency. ## Prerequisites -- NVIDIA driver working (`nvidia-smi` must succeed). -- CUDA and TensorRT compatible with `YOLOs-CPP-TensorRT`. -- OpenCV C++ development files. -- Hikvision MVS SDK at `/opt/MVS`. +Install and verify these before configuring CMake: -Current machine note: CUDA 12.2, TensorRT 10.1.0, OpenCV 4.2, and Hikvision MVS SDK were detected. The FP16 TensorRT engine has been built at `models/yolo26_tile_seg_v3_fp16.trt`. +- NVIDIA driver working on the target machine. +- CUDA Toolkit. The bundled `YOLOs-CPP-TensorRT` project documents CUDA `>= 12.0`. +- TensorRT `>= 10.0`, including headers, libraries, and preferably `trtexec`. +- OpenCV C++ development files. The upstream dependency recommends OpenCV `>= 4.5`; this machine has also been configured with OpenCV 4.2. +- CMake `>= 3.16`. +- C++17 compiler and build tools. +- Hikvision MVS SDK, default path `/opt/MVS`. +- Python environment with `ultralytics` only if you need to export `best.pt` to ONNX. If `runs/.../weights/best.onnx` already exists, the script copies it and does not load Ultralytics. -## Build +Basic Ubuntu packages for the non-NVIDIA pieces are typically: + +```bash +sudo apt update +sudo apt install -y build-essential cmake git libopencv-dev +``` + +Install CUDA, TensorRT, and Hikvision MVS SDK using the versions and installers required by your deployment machine. TensorRT engines are tied to the GPU architecture and TensorRT version, so rebuild the `.trt` file on the target machine. + +Quick checks: + +```bash +nvidia-smi +nvcc --version +trtexec --help +pkg-config --modversion opencv4 +test -f /opt/MVS/include/MvCameraControl.h +test -d /opt/MVS/lib/64 +``` + +If TensorRT or MVS are installed in custom locations, use `TENSORRT_DIR`, `TRTEXEC`, and `MVS_DIR` as shown below. + +## Download The Repository + +Clone the project and enter the repository: + +```bash +git clone https://github.com/trung09122002/quartz_inspection_core.git +cd quartz_inspection_core +``` + +If you use the company remote instead: + +```bash +git clone https://git.pnkr.asia/TrungLQ/quartz_inspection_core.git +cd quartz_inspection_core +``` + +## Set Up `third_party` + +This repository expects `YOLOs-CPP-TensorRT` at `third_party/YOLOs-CPP-TensorRT`. It is not used as an installed system library; this CMake project includes its headers and compiles the CUDA preprocessing source directly. + +From the repository root: + +```bash +mkdir -p third_party +git clone --depth 1 https://github.com/Geekgineer/YOLOs-CPP-TensorRT.git third_party/YOLOs-CPP-TensorRT +``` + +Verify the files CMake needs: + +```bash +test -f third_party/YOLOs-CPP-TensorRT/include/yolos/tasks/segmentation.hpp +test -f third_party/YOLOs-CPP-TensorRT/include/yolos/core/cuda_preprocessing.cu +``` + +You do not need to run `third_party/YOLOs-CPP-TensorRT/build.sh` for this app. Build that project separately only if you want its examples or tests. + +If you keep the dependency somewhere else, pass the path to CMake: + +```bash +cmake -S . -B build -DYOLOS_TRT_DIR=/path/to/YOLOs-CPP-TensorRT +``` + +## Prepare The Model + +The default model pipeline is: + +```text +runs/segment/yolo26_tile_seg_v3/weights/best.pt + -> models/yolo26_tile_seg_v3.onnx + -> models/yolo26_tile_seg_v3_fp16.trt +``` + +Export or copy the ONNX model: ```bash -cd /home/quangtrung/quartz_inspection_core python3 scripts/export_yolo26_seg_onnx.py -scripts/build_engine.sh -cmake -S . -B build \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc -cmake --build build -j ``` -If TensorRT is installed outside common paths, point both engine generation and CMake at it: +Behavior of the export script: + +- If `runs/segment/yolo26_tile_seg_v3/weights/best.onnx` exists, it copies that file to `models/yolo26_tile_seg_v3.onnx`. +- Otherwise it loads `runs/segment/yolo26_tile_seg_v3/weights/best.pt` with Ultralytics and exports ONNX with `imgsz=864`, `opset=17`, `simplify=True`, `dynamic=False`, and `half=False`. +- The checkpoint uses a YOLO26 `Segment26` head. If your active Ultralytics install does not provide that head, run the script inside the same YOLO26-capable Python environment used for training. + +Use a custom checkpoint by setting `YOLO26_MODEL`: ```bash -TENSORRT_DIR=/path/to/TensorRT scripts/build_engine.sh -cmake -S . -B build \ - -DCMAKE_BUILD_TYPE=Release \ - -DTENSORRT_DIR=/path/to/TensorRT \ - -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc +YOLO26_MODEL=/path/to/best.pt python3 scripts/export_yolo26_seg_onnx.py ``` -If `trtexec` is elsewhere, use: +Build the TensorRT engine: + +```bash +scripts/build_engine.sh +``` + +The script defaults to: + +- ONNX input: `models/yolo26_tile_seg_v3.onnx` +- engine output: `models/yolo26_tile_seg_v3_fp16.trt` +- TensorRT flags: `--fp16 --useCudaGraph --noDataTransfers` + +To use explicit input/output paths: + +```bash +scripts/build_engine.sh /path/to/model.onnx /path/to/model_fp16.trt +``` + +If `trtexec` is not on `PATH`: ```bash TRTEXEC=/path/to/trtexec scripts/build_engine.sh ``` +If TensorRT is unpacked under a custom directory and contains `bin/trtexec`: + +```bash +TENSORRT_DIR=/path/to/TensorRT scripts/build_engine.sh +``` + +## Configure And Build + +Default build: + +```bash +cmake -S . -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc + +cmake --build build -j"$(nproc)" +``` + +If CUDA is already discoverable, `CMAKE_CUDA_COMPILER` can be omitted: + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build -j"$(nproc)" +``` + +Custom TensorRT path: + +```bash +cmake -S . -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DTENSORRT_DIR=/path/to/TensorRT \ + -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc +cmake --build build -j"$(nproc)" +``` + +Custom Hikvision MVS SDK path: + +```bash +cmake -S . -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DMVS_DIR=/path/to/MVS +cmake --build build -j"$(nproc)" +``` + +Custom CUDA architectures, for example Ada Lovelace compute capability 8.9: + +```bash +cmake -S . -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CUDA_ARCHITECTURES=89 +cmake --build build -j"$(nproc)" +``` + +By default, this repository uses `CMAKE_CUDA_ARCHITECTURES="75;80;86;89;90"` when the value is not supplied. + +## Full Build From A Clean Checkout + +```bash +git clone https://github.com/trung09122002/quartz_inspection_core.git +cd quartz_inspection_core + +mkdir -p third_party +git clone --depth 1 https://github.com/Geekgineer/YOLOs-CPP-TensorRT.git third_party/YOLOs-CPP-TensorRT + +python3 scripts/export_yolo26_seg_onnx.py +scripts/build_engine.sh + +cmake -S . -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc +cmake --build build -j"$(nproc)" +``` + ## Run +Run with default paths: + +```bash +./build/hik_predict_trt +``` + +Equivalent explicit command: + ```bash ./build/hik_predict_trt \ --engine models/yolo26_tile_seg_v3_fp16.trt \ @@ -59,16 +250,20 @@ TRTEXEC=/path/to/trtexec scripts/build_engine.sh Useful options: -- `--pixel-format mono8|bgr8|bayer_rg8` -- `--frame-skip 1` -- `--imgsz 864` -- `--display-scale 0.3` -- `--save-dir captures` +- `--engine PATH`: TensorRT engine path. +- `--labels PATH`: labels file path. +- `--device-index N`: Hikvision camera index, default `0`. +- `--pixel-format mono8|bgr8|bayer_rg8`: camera pixel format, default `bgr8`. +- `--frame-skip N`: run detection every N frames, default `2`. +- `--imgsz N`: max side used for pre-inference resize, default `864`. +- `--display-scale X`: OpenCV display scale, default `0.3`. +- `--save-dir PATH`: image capture output directory, default `captures`. +- `--no-resize-before-infer`: disable Python-compatible pre-inference resize. -Keys: +Keys while running: - `ESC` or `q`: exit. -- `s`: save current original/result frame. +- `s`: save current original/result frame pair. ## Behavior Matched From `hik_predict.py` @@ -83,12 +278,26 @@ Keys: - if multiple defect classes remain, keeps only the class with the highest max confidence - Mask cleanup and overlay drawing similar to `ResultVisualizer`. +## Troubleshooting + +- `YOLOs-CPP-TensorRT was not found`: clone the dependency into `third_party/YOLOs-CPP-TensorRT` or pass `-DYOLOS_TRT_DIR=/path/to/YOLOs-CPP-TensorRT`. +- `TensorRT not found`: install TensorRT headers and libraries, or configure with `-DTENSORRT_DIR=/path/to/TensorRT`. +- `trtexec was not found`: put `trtexec` on `PATH`, set `TRTEXEC=/path/to/trtexec`, or set `TENSORRT_DIR=/path/to/TensorRT` if `trtexec` is under `TENSORRT_DIR/bin`. +- `Hikvision MVS SDK not found`: install MVS under `/opt/MVS` or configure with `-DMVS_DIR=/path/to/MVS`. CMake checks for `MvCameraControl.h` and `libMvCameraControl`. +- `This checkpoint uses the YOLO26 Segment26 head`: use the same YOLO26-capable Python environment used for training, or provide an existing `best.onnx` at `runs/segment/yolo26_tile_seg_v3/weights/best.onnx`. +- Engine loads on one machine but fails on another: rebuild `models/yolo26_tile_seg_v3_fp16.trt` on the target GPU with the target TensorRT version. + ## Class Mapping Note -`config/labels.names` follows the current `predict.py` mapping: +`config/labels.names` follows the current runtime mapping: ```text -crack, glue_strip, good, gray_stroke, oil, rough +crack +glue_strip +good +gray_stroke +oil +rough ``` -`tile_yolo26/data.yaml` currently lists 5 classes. If the trained model really has 5 outputs, update `labels.names` and the class IDs in `src/hik_predict_trt.cpp` to match the exported model before production use. +`src/hik_predict_trt.cpp` also hardcodes the same class order for filtering and drawing. If the trained model changes class count or class order, update both `config/labels.names` and the class IDs in `src/hik_predict_trt.cpp` before production use. diff --git a/src/hik_predict_trt.cpp b/src/hik_predict_trt.cpp index af29f8f..5c437db 100644 --- a/src/hik_predict_trt.cpp +++ b/src/hik_predict_trt.cpp @@ -241,7 +241,7 @@ public: if (ret != MV_OK) return {}; cv::Mat image; - try { + try { const int h = static_cast(frame.stFrameInfo.nHeight); const int w = static_cast(frame.stFrameInfo.nWidth); const unsigned char* data = frame.pBufAddr;