Skip to main content
Object detection identifies and localizes multiple objects in an image, providing both class labels and bounding box coordinates. OpenCV’s DNN module supports popular detection models like YOLO, SSD, and Faster R-CNN.

Supported Models

YOLO (You Only Look Once) - Real-time object detection
  • YOLOv3, YOLOv4 (Darknet)
  • YOLOv5 (PyTorch/ONNX)
  • YOLOv8, YOLOv9, YOLOv10 (Ultralytics)
  • YOLOX, YOLO-NAS
YOLO models are single-stage detectors optimized for speed.

YOLO Object Detection (Python)

1

Import Libraries

2

Load the Model

3

Prepare Input

4

Run Detection

5

Post-process Results

6

Draw Detections

YOLO Object Detection (C++)

SSD Object Detection

MobileNet-SSD (Caffe)

Model Configurations

YOLOv8 (ONNX)

Download: https://github.com/ultralytics/ultralytics

YOLOv4 (Darknet)

Download: https://github.com/AlexeyAB/darknet/releases

MobileNet-SSD (Caffe)

Faster R-CNN (TensorFlow)

Download: http://download.tensorflow.org/models/object_detection/

Non-Maximum Suppression (NMS)

NMS removes duplicate detections by suppressing boxes with high overlap.

Performance Tips

1

Use GPU Acceleration

2

Use FP16 for Faster Inference

3

Choose Appropriate Model Size

  • YOLOv8n (nano): Fastest, lowest accuracy
  • YOLOv8s (small): Balanced
  • YOLOv8m (medium): Higher accuracy
  • YOLOv8l/x (large/xlarge): Best accuracy, slowest
Different YOLO versions (v3, v4, v5, v8) have different output formats and require different post-processing.

Source Code

Complete source code for object detection:
  • Python: samples/dnn/object_detection.py
  • C++ (YOLO): samples/dnn/yolo_detector.cpp
  • C++ (Generic): samples/dnn/object_detection.cpp