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

30 Comments

  1. Michael May 10, 2018 at 3:54 pm - Reply

    Amazing work guys!

    • Edgar Florez May 12, 2018 at 2:44 pm - Reply

      thanks so much Mr Michael

    • Edgar May 24, 2018 at 2:45 pm - Reply

      thanks so much Michael

  2. iraq2010 May 29, 2018 at 3:39 am - Reply

    thanks for tutorial and sharing your knowledge ….

  3. riza September 5, 2021 at 5:39 am - Reply

    how to use custom dataset???

  4. RIZA IRSYAD September 5, 2021 at 5:44 am - Reply

    what if I want to use a custom dataset, for example a dataset using masks

  5. Yash November 10, 2021 at 11:33 pm - Reply

    Can we add some other Objects in this. Code in Live Streaming . i need Help Plz Help me out

  6. Yash November 10, 2021 at 11:33 pm - Reply

    Can we add some other Objects in this. Code in Live Streaming . i need Help Plz Help me out .

  7. auto approve list November 23, 2021 at 7:37 am - Reply

    Touche. Outstanding arguments. Keep up the great effort.

  8. java best practics December 9, 2021 at 3:57 pm - Reply

    This is a good tip especially to those new to thee blogosphere.
    Short but very precise information… Many
    thanks for sharing this one. A must read post!

    php patterns

  9. auto approve list December 17, 2021 at 3:10 pm - Reply

    Heya! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no back
    up. Do you have any methods to protect against hackers?

  10. python tips and tricks December 31, 2021 at 6:26 am - Reply

    Wow, this article is good, my siister is analyzing these kinds off things, therefore I am going to convey her.

    php patterns

  11. Dallas January 10, 2022 at 11:44 pm - Reply

    It’s remarkable to go to see this web page and reading
    the views of all colleagues about this piece of writing,
    while I am also zealous of getting know-how.

  12. Salamatikhabari.ir February 22, 2022 at 4:05 pm - Reply

    I am regular visitor, how are you everybody? This paragraph posted
    at this web site is really pleasant.

  13. علمی خبری February 23, 2022 at 10:51 pm - Reply

    Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between user friendliness and visual appeal.
    I must say that you’ve done a fantastic job with this.

    In addition, the blog loads super quick for me on Opera. Superb Blog!

  14. تولید محتوای سایت February 28, 2022 at 8:11 am - Reply

    Oh my goodness! Impressive article dude! Thanks, However I am experiencing issues with your RSS.
    I don’t understand why I can’t subscribe to it. Is there anybody
    else having identical RSS problems? Anybody who knows
    the solution can you kindly respond? Thanks!!

  15. fintech February 28, 2022 at 2:03 pm - Reply

    Hello! I’ve been following your weblog for a long time now and finally got the bravery to go ahead and give you a shout out from
    Lubbock Tx! Just wanted to say keep up the excellent work!

  16. jahankhabari.ir March 1, 2022 at 7:51 am - Reply

    Greetings! I’ve been reading your website for a long
    time now and finally got the courage to go ahead and give you a shout out from Dallas Texas!
    Just wanted to say keep up the good job!

  17. بهترین سایت March 2, 2022 at 4:42 am - Reply

    whoah this blog is magnificent i like studying your posts.

    Keep up the great work! You understand, a lot of people are searching around for this
    info, you can help them greatly.

  18. bizzone March 2, 2022 at 6:12 pm - Reply

    excellent post, very informative. I’m wondering why
    the other experts of this sector don’t realize this.
    You must proceed your writing. I’m confident, you have a great
    readers’ base already!

  19. بهترین سایت March 2, 2022 at 7:19 pm - Reply

    Hi there I am so glad I found your webpage, I really found you by error,
    while I was searching on Aol for something else, Anyhow I am here now and would just like
    to say kudos for a tremendous post and a all round entertaining blog (I also love
    the theme/design), I don’t have time to browse it all at the
    minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back
    to read more, Please do keep up the superb job.

  20. بهترین سایت March 4, 2022 at 8:07 am - Reply

    An impressive share! I’ve just forwarded this onto a friend who was
    doing a little research on this. And he in fact ordered me dinner due to the
    fact that I discovered it for him… lol. So allow me to reword this….
    Thanks for the meal!! But yeah, thanx for spending the time to discuss this subject here on your web page.

  21. بهترین سایت March 4, 2022 at 6:50 pm - Reply

    Wow, incredible blog layout! How long have you been blogging for?

    you made blogging look easy. The overall look of your site is excellent, let alone the content!

  22. bizzone.ir March 6, 2022 at 7:32 am - Reply

    Aw, this was an exceptionally nice post. Finding the time and actual
    effort to make a good article… but what can I say…
    I put things off a lot and don’t seem to get nearly anything done.

  23. bizzone.ir March 8, 2022 at 3:13 pm - Reply

    Really when someone doesn’t be aware of after that its up to other
    viewers that they will help, so here it takes place.

  24. بهترین سایت March 9, 2022 at 3:09 pm - Reply

    Good day! I know this is kinda off topic but I’d figured I’d ask.

    Would you be interested in exchanging links or maybe guest authoring a
    blog post or vice-versa? My site covers
    a lot of the same topics as yours and I think we could greatly benefit from each other.
    If you are interested feel free to send me an email. I look forward to hearing from you!
    Great blog by the way!

  25. بهترین سایت March 10, 2022 at 2:31 am - Reply

    Hi there everyone, it’s my first pay a visit at this
    site, and piece of writing is really fruitful in support of me, keep up posting
    these content.

  26. بهترین سایت March 12, 2022 at 7:04 am - Reply

    Thanks for sharing your thoughts about bizzone.
    Regards

  27. بهترین سایت March 12, 2022 at 4:34 pm - Reply

    each time i used to read smaller content that also clear their motive, and that is also happening with this article which I am reading at this time.

Leave A Comment