MobileNet SSD Object Detection using OpenCV 3.4.1 DNN module
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:
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.
Next, open the video file or capture device depending what we choose, also load the model Caffe model.
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.
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.
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:
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.
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
Amazing work guys!
thanks so much Mr Michael
thanks so much Michael
thanks for tutorial and sharing your knowledge ….
how to use custom dataset???
what if I want to use a custom dataset, for example a dataset using masks
Can we add some other Objects in this. Code in Live Streaming . i need Help Plz Help me out
Can we add some other Objects in this. Code in Live Streaming . i need Help Plz Help me out .
Touche. Outstanding arguments. Keep up the great effort.
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
Well, you’re welcome.
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?
Wow, this article is good, my siister is analyzing these kinds off things, therefore I am going to convey her.
php patterns
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.
I am regular visitor, how are you everybody? This paragraph posted
at this web site is really pleasant.
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!
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!!
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!
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!
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.
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!
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.
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.
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!
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.
Really when someone doesn’t be aware of after that its up to other
viewers that they will help, so here it takes place.
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!
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.
Thanks for sharing your thoughts about bizzone.
Regards
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.