OpenCV
4.5.1
Open Source Computer Vision
|
Next Tutorial: How to enable Halide backend for improve efficiency
Original author | Vitaliy Lyudvichenko |
Compatibility | OpenCV >= 3.3 |
In this tutorial you will learn how to use opencv_dnn module for image classification by using GoogLeNet trained network from Caffe model zoo.
We will demonstrate results of this example on the following picture.
We will be using snippets from the example application, that can be downloaded here.
Firstly, download GoogLeNet model files: bvlc_googlenet.prototxt and bvlc_googlenet.caffemodel
Also you need file with names of ILSVRC2012 classes: classification_classes_ILSVRC2012.txt.
Put these files into working dir of this program example.
framework
if one of the files model
or config
has an extension .caffemodel
or .prototxt
. This way function cv::dnn::readNet can automatically detects a model's format.Read input image and convert to the blob, acceptable by GoogleNet
cv::VideoCapture can load both images and videos.
We convert the image to a 4-dimensional blob (so-called batch) with 1x3x224x224
shape after applying necessary pre-processing like resizing and mean subtraction (-104, -117, -123)
for each blue, green and red channels correspondingly using cv::dnn::blobFromImage function.
prob
blob. And find the index of element with maximal value in this one. This index corresponds to the class of the image.space shuttle
with more than 99% sureness.