13 Deep Learning in OpenCV
Satya Mallick edited this page 2026-09-21 10:40:06 +05:30

OpenCV's DNN module runs trained neural networks from C++ and Python. It brings model inference into the same application that reads images, processes video, and uses the predictions. Training and model export usually happen in a separate framework; the deployed application can run supported models without that training framework.

This page focuses on OpenCV 5. API details were checked against the 5.x development branch on September 21, 2026. Where the released 5.0.0 API differs, the difference is called out below. For OpenCV 4, see the version-specific resources.

Start here

The OpenCV DNN Module tutorial includes complete Python and C++ examples for:

  • Object detection with YOLO26: export the model, prepare the input, run inference, and draw bounding boxes.
  • Instance segmentation with RF-DETR: run inference and decode a separate mask for each detected object.

The examples were tested with OpenCV 5.0.0, and the tutorial explains the engine-name change in the current development API. These are two examples of a much broader range of applications, not a list of the only supported models.

For more tasks, explore the official DNN samples and OpenCV 5 DNN benchmarks.

Models and formats

ONNX is the recommended starting point for new deployments. Export a trained model to ONNX, load it with OpenCV, and reproduce the model's preprocessing and output decoding in your application. OpenCV 5's DNN engine expands support for modern ONNX graphs, including dynamic shapes and subgraphs.

The range of applications extends beyond classification, detection, and segmentation:

Application Examples and starting points
Image classification and visual features ResNet, MobileNet, DINOv2/DINOv3, and SigLIP models appear in the benchmark tables. See also the classification sample.
Object detection YOLO26 and RF-DETR appear in the benchmarks; the tutorial above provides a complete YOLO26 workflow.
Segmentation SegFormer and SAM components appear in the benchmarks; the tutorial provides a tested RF-DETR instance-segmentation workflow.
Depth and motion Depth Anything and RAFT appear in the benchmarks. See also the optical-flow sample.
Image restoration Real-ESRGAN and SwinIR appear in the benchmarks. See also the super-resolution sample.
Vision-language models The PaliGemma 2 sample combines a vision encoder, a language model, tokenization, and generation.

A model-family name is not a guarantee that every checkpoint or export will work. Compatibility depends on the exported graph, operators, opset, tensor shapes, selected engine, and OpenCV build. Some benchmark entries report errors, and some test individual model components rather than a complete application. Check the exact export you intend to deploy.

The current 5.x API also exposes TensorFlow and TensorFlow Lite importers. Caffe and Darknet importers have been removed, and the legacy Torch importer is no longer part of the current API. PyTorch models can be exported to ONNX; this is separate from importing legacy Torch files. An older framework-specific model list should not be used as an OpenCV 5 compatibility list. See the current import API and format dispatch implementation.

Choosing the DNN engine

The engine API changed after OpenCV 5.0.0:

API Current 5.x development branch OpenCV 5.0.0 release
Built-in OpenCV engine ENGINE_OPENCV ENGINE_NEW
Classic engine Removed ENGINE_CLASSIC was available
Automatic selection ENGINE_AUTO currently resolves to ENGINE_OPENCV Used the new/classic selection path
Optional ONNX Runtime integration ENGINE_ORT ENGINE_ORT

Use named constants from the version you build against. Do not copy numeric engine values between versions: the enum values changed. Also unset OPENCV_FORCE_DNN_ENGINE when following examples that select an engine in code, because that environment variable can override the requested engine.

These short snippets show model loading with the current 5.x API. Replace model.onnx with your exported model. With OpenCV 5.0.0, use ENGINE_NEW in place of ENGINE_OPENCV; the complete tutorial includes compatibility handling for both versions.

Python:

import cv2

net = cv2.dnn.readNetFromONNX("model.onnx", cv2.dnn.ENGINE_OPENCV)

C++:

#include <opencv2/dnn.hpp>

int main()
{
    cv::dnn::Net net = cv::dnn::readNetFromONNX(
        "model.onnx", cv::dnn::ENGINE_OPENCV);
    return 0;
}

Loading is only the first step. Prepare the input with the model's required color order, layout, size, normalization, and resize or padding policy; pass it to setInput(); then call forward() and decode the outputs. Models with multiple inputs or outputs need the corresponding names and tensors. There is no single preprocessing or decoding routine that is correct for every network.

The linked tutorial uses the built-in engine for CPU inference. GPU availability depends on the OpenCV version, build, engine, and runtime provider; an OpenCV 4 backend configuration should not be assumed to work unchanged in OpenCV 5.

ENGINE_ORT selects the optional ONNX Runtime integration for ONNX models and requires an OpenCV build with WITH_ONNXRUNTIME=ON. GPU use additionally requires the appropriate runtime provider and dependencies. Installing the Python onnxruntime package alone does not enable this feature in an existing OpenCV build.

For exact API definitions, compare the current header with the 5.0.0 header.

Performance and validation

The OpenCV 5 DNN benchmark page reports CPU inference times across many models and compares OpenCV with ONNX Runtime. Check the available hardware and configuration details, as well as error entries, alongside the timings. These measurements do not establish a universal speedup, and a missing result is not evidence of support.

Before deploying a model, compare its outputs against the original framework or another validated runtime using representative inputs. Measure the whole application as well as network inference: image preparation, data transfer, output decoding, and drawing can all contribute to latency. The DNN samples and model configuration files provide task-specific starting points.

Using OpenCV 4

If your application uses OpenCV 4, consult the OpenCV 4 DNN documentation, 4.x samples, and OpenCV 4 DNN tutorial. Its importer and backend support differs from OpenCV 5.

The previous version of this wiki page preserves the historical framework-specific model catalog. Treat it as a historical reference, not a current compatibility guarantee.