examples/cpp/OpenCV-DNN/README.md
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
--taskfor grid pose/obb).
--task for grid pose/obb).--cuda (requires a CUDA-enabled OpenCV).| Dependency | Version | Description |
|---|---|---|
| OpenCV | >=4.7.0 | DNN module for inference, plus image I/O, drawing, and NMS. |
| C++ | >=17 | Modern C++ compiler. |
| CMake | >=3.5 | Build system. |
| CUDA | optional | Only for the OpenCV CUDA DNN backend (--cuda). |
The OpenCV DNN module runs grid models only. YOLOv8 and YOLO11 export to a grid by default:
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:
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)
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.
# 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
| Argument | Default | Description |
|---|---|---|
--model | yolo26n.onnx | Path to the exported ONNX model (grid output). |
--source | bus.jpg | Input image. |
--conf | 0.25 | Confidence threshold. |
--iou | 0.45 | NMS IoU threshold. |
--imgsz | 640 | Square input size of the exported model. |
--task | auto | Override the task (detect/segment/pose/obb/classify/semantic); needed for grid pose/obb. |
--cuda | off | Use the OpenCV CUDA DNN backend (requires a CUDA-enabled OpenCV). |
--out | result.jpg | Output image path. |
--show | off | Also open a display window. |
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.