Introduction
Hello Geeks! Welcome to the era of Machine Learning, Big Data, and Data Science. Image Processing is a very popular and interesting domain of Computer Science. Detecting objects was very difficult in the early days of Image processing. With the advancement in the field of Image Processing, things became easier and easier for the developers. Now a novice programmer with a basic understanding of python can write a code that can detect different objects in real-time and all this is possible only due to OpenCV, an image processing library for almost all the platforms and with different languages supports.
What will we do today?
Today I am going to show you how you can detect human faces using python and a cascade classifier.
What is the prerequisite for this?
Almost nothing is a prerequisite for this but for better understanding, you must have:
- Basic knowledge of Python
What is required to make this possible?
- Python installed (I am using 2.7 version)
- OpenCV installed
- Download Cascade Classifier file
How to use the OpenCV library in python?
Go to your OpenCV installation folder. Usually, it is C:\ drive. Go to C:\opencv\build\python\2.7
The directory structure might be different for you depending upon your default installation location. I am using C:\ drive as an installation location.
Once you move there, you see two folders namely "x86" and "x64". Depending upon your computer architecture go to the folder. I am using "x64" so I am going to "x64" folder. There you will find "cv2.pyd" file. Copy it.
Now move to your python installation location. I have in C:\ drive. So move to C:\Python27\Lib\site-packages\ and paste the file there.
Now open python terminal and type
- import cv2
How we will make it?
Detecting faces was not an easy job before the openCV and Cascade Classifier. There are billions of people in this world. And everyone's face is different from the other in structural patterns but contains the same features as one nose, two ears, two eyes and a pair of lips. With Cascade Classifier, you just downloaded is an XML file that already contains data of faces. This file is already trained on many different faces. So we do not need to train it again. We just provide this classifier and it will put boxes around the faces in the image.
We're getting ready for the dive into OpenCV and python journey in the Image Processing domain. Now the actual work starts. I will take the code bit by bit and explain to you what is actually happening behind the scenes.
- import cv2
- import sys
- image = sys.argv[1]
- faceclassifier = cv2.CascadeClassifier("haarcascade_frontalface.xml")
- actual_image = cv2.imread(image)
- gray = cv2.cvtColor(actual_image, cv2.COLOR_BGR2GRAY)
- faces = faceclassifier.detectMultiScale(
- gray,
- scaleFactor=1.1,
- minNeighbors=5,
- minSize=(30, 30),
- )
- grey: It is the grayscale image.
- scaleFactor: It scales or descales your image according to your algorithm requirement. Some faces are big and some are very small and they produce problems for the algorithm to smoothly detect them.
- minNeighbors: For the sake of understanding assume that detecting faces is not an easy task for computers. A classifier contains thousands of data of faces. Our given image is compared with the faces in the classifier. minNeighbors property is used to tell after how many equal images we can say we have found a face. If you set it to 5 as in our case it at a minimum requires 5 images that must match with our provided image to detect a face.
- minSize: minimum object size. If the object is smaller then this it will be ignored.
The above function returns a list of rectangles where our algorithm found the face.
- for (x, y, w, h) in faces:
- cv2.rectangle(actual_image, (x, y), (x+w, y+h), (0,0, 255), 2)
- x: location of the rectangle on the x-axis
- y: location of the rectangle on the y-axis
- w: width of the rectangle
- h: height of the rectangle
- cv2.imshow("Face Detected" ,actual_image)
- cv2.waitKey(0)
In the second line wait for the user key to terminate the program.
Hurrah we have made it. Now we can detect faces on any image using the little python scripts that we write here.
Now for running our script open command prompt and type:
python <path-to-your-script> <path-to-your-image>
In my case it is:
python C:\face_detection.py C:\image.jpg
And the output is.

We have successfully detected all the faces in the image.
In future articles, I will be using real-time detection using a webcam.
Stay tuned for further fun projects using python and OpenCV.

Nidhi SinghPosted Feb 6, 2018, 2:41 AM
I want only the biggest face to be detected. what is the code for that
Vignesh ManiPosted Sep 27, 2016, 6:20 PM
Nice