MobileNet SSD Object Detection using OpenCV 3.4.1 DNN module

Published On: May 8th, 2018
Edgar Florez Ostos
SHARE

MobileNet SSD  object detection using OpenCV 3.4.1 DNN module

This post demonstrates how to use the OpenCV 3.4.1 deep learning module with the MobileNet-SSD network for object discovery.

As part of Opencv 3.4. + The deep neural network (DNN) module was officially included. The DNN module allows loading pre-trained models of most popular deep learning frameworks, including Tensorflow, Caffe, Darknet, Torch. Besides MobileNet-SDD, other architectures are compatible with OpenCV 3.4.1:

  • GoogleLeNet
  • YOLO
  • SqueezeNet
  • R-CNN faster
  • ResNet
  • This API is compatible with C ++ and Python. : -)

Descripton code

In this section, we will create the Python script for object detection and explain, how to load our deep neural network with OpenCV 3.4? How to pass the image to the neural network? and How to make a prediction with MobileNet or dnn module in OpenCV ?.

We use a pre-trained MobileNet taken from https://github.com/chuanqi305/MobileNet-SSD/ that was trained on the Caffe-SSD framework. This model can detect 2 classes.

Load and predict with the deep neural network module

First, create a new Python file mobilenet_ssd_python.py we put the following code, here we import the libraries:

Copy to Clipboard

The previous line sets the following arguments:

  • Video: video path file.
  • Prototxt: network file is .prototxt
  • Weights: the network weights file is .caffemodel
  • Thr: confidence threshold.

Next, we define the labels for the classes on our MobileNet-SSD network.

Copy to Clipboard

Next, open the video file or capture device depending what we choose, also load the model Caffe model.

Copy to Clipboard

On line 36, you pass the prototxt and weights arguments to the function, then we correctly load the network.

We then read the video frame by frame and pass it on the frame to the web for detections. With the DNN module it is easy to use our deep learning network in OpenCV and make predictions.

Copy to Clipboard

On line 40-41, read the video frame and resize to 300× 300 because it is the image input size defined for the MobileNet-SSD model.

Copy to Clipboard

After the previous lines, we get the network prediction, just doing it in three basic steps:

  • Upload an image
  • Preprocess the image
  • Set the image as the network input and get the prediction result.

The use of the DNN module is essentially the same for the other networks and architectures, so we can replicate this for our own trained models.

Visualize Object Detection and Prediction Confidence

In conclusion, after the above steps, new questions arise, How to get the location of the object with MobileNet? How to know the predicted object class? How to get confidence in the prediction? Go!

We must read the detection matrix to get the prediction data from the neural network, the following code does this:

Copy to Clipboard

We make a loop (line 62) to read the values. Then on line 63 we get the confidence of the prediction and the next line filter with threshold value. On line 65, get the label. On lines 68 through 71, get the corners of the object.

With all the information about the predicted object, the last step is to display the results. The following code drawing object detected and shows its label and trust in the frame.

Copy to Clipboard

Last lines, display the normal frame image and resize to screen.

Downloads

MobileNet-trained code and model can be downloaded from:

https://github.com/djmv/MobilNet_SSD_opencv

Custom Artificial Intelligence Solutions

We provide outsourcing services for custom AI and Computer Vision development solutions.

Artificial Intelligence solutions

Leave A Comment