Supported Models
The following classification models are commonly used:- ResNet - Deep residual networks with skip connections
- MobileNet - Lightweight models optimized for mobile devices
- GoogLeNet - Inception architecture from Google
- SqueezeNet - Compact model with high accuracy
- VGG - Very deep convolutional networks
Python Implementation
1
Import Libraries
2
Load the Model
You can use
DNN_BACKEND_CUDA and DNN_TARGET_CUDA for GPU acceleration if available.3
Load Class Names
4
Prepare Input Image
The
blobFromImage function performs:- Mean subtraction
- Scaling
- Optional channel swapping (BGR to RGB)
- Resizing to target dimensions
5
Run Inference
6
Visualize Results
C++ Implementation
- Basic Usage
- Video Processing
Model Download and Configuration
GoogLeNet (Caffe)
SqueezeNet (Caffe)
Preprocessing Parameters
Different models require different preprocessing parameters:
Backend and Target Options
Available Backends
Available Targets
Complete Example
Here’s a complete classification example that processes video frames:Source Code
The complete source code for classification examples can be found in the OpenCV repository:- Python:
samples/dnn/classification.py - C++:
samples/dnn/classification.cpp
