Image Processing and Machine Learning, the two hot cakes of tech world. Did you know that we are the most documented generation in history of humanity. Every minute a whooping 1.78 million GB data gets produced online !!!. That’s a lot of data and a big chunk that of data is images and videos. This is where automated image processing and machine learning comes in. There never has been an more awesome time to be a geek. Plethora of avenues are opening up for those with skills in Machine learning in general and image processing in particular.
After we are done with the tutorial, you would be able to pass an input image to our program and our program should be able to count the number of peoples appearing in that image. Additionally we would also be creating an bounding box around each of the detected person.
This post of mine is an humble effort to get people interested in this area and by using a simple example, demonstrate how easy is it to get started. All we need would be a working knowledge of Python and a little background of OpenCV.
Python — Although there are multiple tutorials available online, personally, I found dataquest.io to be an wonderful python learning platform, for beginners and experienced alike.
OpenCV — Same as python, OpenCV also has a lot of online tutorials. One site that I find myself referring to again and again is the official documentation.
HaaR Cascades — OpenCV exposes special methods to train our own custom algorithms to detect any object of interest in an input image. HaaR cascade are those files that contain that trained model
I realize that a lot of folks might already have a good knowledge of python but might still be just starting out with OpenCV. Hence for the sake of completeness, I have included the next section with a little background on OpenCV and HaaR Cascade files. Please feel free to skip it, if needed.
OpenCV or Open Source Computer Vision Library is an open source computer vision and machine learning library. Although most popular with python, it interfaces quite well with C++, Java and MATLAB. OpenCV is native written in C++ and is widely used with computer vision related applications running on variety of systems like Windows, Linux, Android, Mac OS etc. This multi language/OS support makes is specially useful in variety of IoT applications.
OpenCV uses HaaR cascade files for object detection. HaaR cascade files are nothing but models trained by OpenCV to detect an object of interest in input images. You can create a HaaR cascade file for any real world object. All you would need is a lot of positive samples images containing the object and a lot of negative sample images, containing everything but the object. Next using using methods exposed by OpenCV, we can run training cycle to generate our own HaaR classifier or an HaaR Cascade file. For this tutorial however, we would be using one of the classifiers publicly available from OpenCV. This classifier specializes in detecting a person in an image. In future tutorials we will touch briefly on how to create your own classifier and then run object detection using that.
I created a sample project for the purpose of this demo. Please clone this Github Link in your work space to get started.
git clone https://github.com/akshaysin/pedestrian_detect
Install OpenCV on your machine by following the instructions listed on following OpenCV page.
Although the root of the cloned project already contains a test image named
peds.jpeg, I would recommend for you to download your own image from internet, just to make it more interesting . Be sure to choose an image with bunch of people in it because that’s what we are looking for. And save it in the root of the directory.
The below listed code was sourced from OpenCV Documentation publicly available here
pedestrian.py from root of the work space in your favorite editor.
Let’s look at some of the important sections of the code one by one :
Reading the image
Detecting the objects
Now lets execute the script and see how it fair with our input image. Execute following command from root of the directory
python pedestrian.py peds.jpeg
Please make sure to replace
peds.jpeg with the name of the image that you downloaded in previous step
It takes a few seconds for it to run, but you should expect to see following output if running against
$ python pedestrian.py peds.jpeg Found 31 pedestrian! Image written to file-system : True [ INFO:0] Initialize OpenCL runtime...
Let’s now visually validate the before and after results of our prediction
As can be seen that our script detected around 31 pedestrians in the input image. Which ain’t great but by training our own custom classifiers, we can make predictions much better.
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?
Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...
Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().