How to process images in Python

How to process images in Python


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


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

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 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 : 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 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 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


This is image title


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.


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

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.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

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 Programming: A Beginner’s Guide

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?

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[]( "")**, our full-stack Python development services...

Python any: How to Check If Element is Iterable or Not

Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().