Back to Ultralytics

Ultralytics YOLO OpenCV DNN Inference in C++

examples/cpp/OpenCV-DNN/README.md

8.4.716.3 KB
Original Source

Ultralytics YOLO OpenCV DNN Inference in C++

A C++ application that runs Ultralytics YOLO ONNX models with the OpenCV DNN module. It supports detect, segment, pose, OBB, classify, and semantic segmentation, sharing its post-processing with the other examples in ../common.

[!IMPORTANT] The OpenCV DNN module cannot run the NMS-in-graph operators used by YOLO26 end-to-end exports, and it cannot read class names or the task from the ONNX metadata. So this example targets grid models (YOLOv8 / YOLO11, or YOLO26 with its end-to-end head disabled), class names fall back to the 80 COCO names, and the task is inferred from the output shapes (use --task for grid pose/obb).

✨ Features

  • All tasks (grid models): detect, segment, pose, OBB, classify, and semantic segmentation.
  • All generations (grid): YOLOv8, YOLO11, and YOLO26 with its end-to-end head disabled. The OpenCV DNN module cannot run the YOLO26 end-to-end (NMS-in-graph) operators.
  • Zero configuration: OpenCV exposes no model metadata, so the task is inferred from the output shapes and class names fall back to COCO (pass --task for grid pose/obb).
  • CPU or CUDA: runs on the OpenCV DNN CPU backend, or the CUDA backend with --cuda (requires a CUDA-enabled OpenCV).

📋 Dependencies

DependencyVersionDescription
OpenCV>=4.7.0DNN module for inference, plus image I/O, drawing, and NMS.
C++>=17Modern C++ compiler.
CMake>=3.5Build system.
CUDAoptionalOnly for the OpenCV CUDA DNN backend (--cuda).

📦 Exporting a Model

The OpenCV DNN module runs grid models only. YOLOv8 and YOLO11 export to a grid by default:

bash
yolo export model=yolo11n.pt format=onnx opset=12 imgsz=640 # detect (also -seg / -pose / -obb / -cls / -sem)

YOLO26 is end-to-end (NMS-free) by architecture, so a normal export emits a [1, 300, 6] tensor that the OpenCV DNN module cannot run. Disable the end-to-end head first to get a grid [1, 84, 8400] output:

python
from ultralytics import YOLO

model = YOLO("yolo26n.pt")
model.model.model[-1].end2end = False  # grid output the OpenCV DNN module can run
model.export(format="onnx", opset=12, imgsz=640)

🛠️ Build

bash
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics/examples/cpp/OpenCV-DNN
mkdir build && cd build
cmake .. && cmake --build . --config Release

OpenCV is found with find_package(OpenCV) and the shared helpers in ../common are added automatically. For GPU inference build OpenCV with the CUDA DNN backend and pass --cuda.

🚀 Usage

bash
# Defaults: --model yolo26n.onnx --source bus.jpg --conf 0.25 --iou 0.45 --imgsz 640 --out result.jpg
./yolo_opencv_dnn --model yolo26n.onnx --source bus.jpg
./yolo_opencv_dnn --model yolo26n-seg.onnx --source bus.jpg --out seg.jpg
./yolo_opencv_dnn --model yolo26n-pose.onnx --source bus.jpg --task pose --show
ArgumentDefaultDescription
--modelyolo26n.onnxPath to the exported ONNX model (grid output).
--sourcebus.jpgInput image.
--conf0.25Confidence threshold.
--iou0.45NMS IoU threshold.
--imgsz640Square input size of the exported model.
--taskautoOverride the task (detect/segment/pose/obb/classify/semantic); needed for grid pose/obb.
--cudaoffUse the OpenCV CUDA DNN backend (requires a CUDA-enabled OpenCV).
--outresult.jpgOutput image path.
--showoffAlso open a display window.

🤝 Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the main Ultralytics repository.