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 read class names or the task from the ONNX metadata, so class names fall back to the 80 COCO names and the task is inferred from the output shapes (use
--taskfor grid pose/obb). This example targets grid models (YOLOv8 / YOLO11, or a default YOLO26 export); NMS-free (nms=False) YOLO26 exports need OpenCV 4.11 or newer for their TopK operator.
nms=None) Ultralytics YOLO26 exports. NMS-free (nms=False) YOLO26 exports need OpenCV 4.11 or newer for their TopK operator.--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 and, with OpenCV 4.11 or newer, NMS-free YOLO26 exports. 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 default exports (nms=None) emit the same grid [1, 84, 8400] output, so the command above works with yolo26n.pt as well. NMS-free exports (nms=False) emit a [1, 300, 6] tensor that also runs here, but only on OpenCV 4.11 or newer, which added the TopK operator they use.
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.