How to process images in Python

Introduction

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.

Prerequisites

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.

First thing first, What be, OpenCV and Haar Cascade?

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.

Steps

Download the Code

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

Install OpenCV on your machine by following the instructions listed on following OpenCV page.

Selecting a test image

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.

Looking at the code

The below listed code was sourced from OpenCV Documentation publicly available here

Open pedestrian.py from root of the work space in your favorite editor.

pedestrian.py

Let’s look at some of the important sections of the code one by one :

  • Reading the image : The below listed code would read into the image passed as an argument to the program and convert it to grayscale. Also we would initialize the HaaR cascade model for pedestrian detect by using the HaaR cascade xml file as well.

Reading the image

  • Detecting people in image : Using the HaaR cascade model initialized in last step, we run an detection on the input image and out the number of objects detected.

Detecting the objects

  • Creating bounding box : Finally we shall create a bounding box around the objects detected in previous steps and write a new output image by the name of peds_saved.jpg

Execution and Output

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 peds.jpeg


$ python pedestrian.py peds.jpeg
Found 31 pedestrian!
Image written to file-system :  True
[ INFO:0] Initialize OpenCL runtime...

Moment of Truth

Let’s now visually validate the before and after results of our prediction

Before

This is image title

After

This is image title

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.

#python

How to process images in Python
1 Likes39.10 GEEK