Prerequisites: Python OpenCV
Gun Detection using Object Detection is a helpful tool to have in your repository. It forms the backbone of many fantastic industrial applications. OpenCV(Open Source Computer Vision Library) is a highly optimized library with focus on Real-Time Applications.
1) Creation of Haarcascade file of Guns: Refer to Creation of own haarcascade
From here, you will learn about how to create your own Haarcascade file. With your single positive image, you can use the opencv_createsamples command to actually create a bunch of positive examples, using your negative images. Your positive image will be superimposed on these negatives, and it will be angled and all sorts of things. It actually can work pretty well, especially if you are really just looking for one specific object. If you are looking to identify all guns, however, you will want to have thousands of unique images of guns, rather than using the opencv_createsamples to generate samples for you. We’ll keep it simple and just use one positive image, and then create a bunch of samples with our negatives.
Note: For The Gun haar cascade created – click here.
2) Detection of Guns using OpenCV
OpenCV comes with a trainer as well as a detector. If you want to train your own classifier for any object like car, planes, etc. you can use OpenCV to create one.
Here we deal with the detection of Gun. First we need to load the required XML classifiers. Then load our input image (or video) in grayscale mode. Now we find the guns in the image. If guns are found, it returns the positions of detected guns as
Rect(x, y, w, h). Once we get these locations, we can create a ROI(Region of Interest) for the gun.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course