Files
quartz_inspection_core/README.md
2026-05-20 08:30:13 +07:00

10 KiB

Quartz Inspection Core

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.

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.
  • 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: required C++ TensorRT dependency.

Prerequisites

Install and verify these before configuring CMake:

  • 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.

Basic Ubuntu packages for the non-NVIDIA pieces are typically:

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:

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:

git clone https://github.com/trung09122002/quartz_inspection_core.git
cd quartz_inspection_core

If you use the company remote instead:

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:

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:

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:

cmake -S . -B build -DYOLOS_TRT_DIR=/path/to/YOLOs-CPP-TensorRT

Prepare The Model

The default model pipeline is:

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:

python3 scripts/export_yolo26_seg_onnx.py

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:

YOLO26_MODEL=/path/to/best.pt python3 scripts/export_yolo26_seg_onnx.py

Build the TensorRT engine:

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:

scripts/build_engine.sh /path/to/model.onnx /path/to/model_fp16.trt

If trtexec is not on PATH:

TRTEXEC=/path/to/trtexec scripts/build_engine.sh

If TensorRT is unpacked under a custom directory and contains bin/trtexec:

TENSORRT_DIR=/path/to/TensorRT scripts/build_engine.sh

Configure And Build

Default build:

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:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j"$(nproc)"

Custom TensorRT path:

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:

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:

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

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:

./build/hik_predict_trt

Equivalent explicit command:

./build/hik_predict_trt \
  --engine models/yolo26_tile_seg_v3_fp16.trt \
  --labels config/labels.names

Useful options:

  • --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 while running:

  • ESC or q: exit.
  • s: save current original/result frame pair.

Behavior Matched From hik_predict.py

  • Resize camera frame before inference when the max side is larger than imgsz.
  • 4-pass fallback:
    1. original frame at conf=0.175
    2. light CLAHE at conf=0.30
    3. gamma correction for dark/bright frames at conf=0.30
    4. strong CLAHE at conf=0.08, retaining only crack
  • Single-class filter:
    • drops class good
    • 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 runtime mapping:

crack
glue_strip
good
gray_stroke
oil
rough

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.