Face Detection Using Python & Open CV

Image for post
Image for post

In this article I will show you how to create your very own image face detection program using the python programming language, and Open CV with pretrained classifiers. We will detect both the faces and eyes within an image.

Face detection is performed by using classifiers. A classifier is essentially an algorithm that decides whether a given image is positive(face) or negative(not a face). We will use the Haar classifier which was named after the Haar wavelets because of their similarity to detect the faces and eyes on an image. The Haar classifier employs a machine learning algorithm called Adaboost for visual object detection.

If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video below and the full code on my Github. It goes through everything in this article with a little more detail, and will help make it easy for you to start programming your own face detection program. Or you can use both as supplementary materials for learning about this topic !

Start Programming:

The first thing that I like to do before writing a single line of code is to put in a description in comments of what the code does. This way I can look back on my code and know exactly what it does.

# Description: This program detects faces, and eyes.

Now import the packages/libraries to make it easier to write the program.

import cv2

Next, I will use Open CV’s pretrained Haar classifers ( haarcascade_frontalface_default.xml & haarcascade_eye.xml) which are stored as .xml files, which will help me to detect faces and eyes in an image, and store them in variables. These models were already trained on thousands of images.

haarcascade_frontalface_default.xml : Model trained on many face images, and used to detect faces.

haarcascade_eye.xml: Model trained on many eye images and used to detect eyes.

#The Haar Classifiers face_cascade = cv2.CascadeClassifier('DataSets/Faces/haarcascade_frontalface_default.xml')eye_cascade = cv2.CascadeClassifier('DataSets/Faces/haarcascade_eye.xml')

Time to read in the image that has the faces I want to detect ! The image I will read in is dwayne_and_james.jpg. This image file contains the images of Dwayne Wade (left) and Lebron James (right).

Note: cv2.imread() stores the image as a numpy array.

img = cv2.imread('DataSets/Faces/dwayne_and_james.jpg')
Image for post
Image for post

Get the position (x,y) , width (w), and height(h) of the detected faces in the image, and store it in the variable called ‘faces’.

faces = face_cascade.detectMultiScale(img, 1.3, 5)

Get some data about the image. Print the number of faces found/detected. Print the image height, width, and channel (RGB visible wavelenghts) . Print the co-ordinates of each face detected within the image.

print('Faces found: ', len(faces))
print('The image height, width, and channel: ',img.shape)
print('The coordinates of each face detected: ', faces)
Image for post
Image for post
Data About The Image

Draw a green rectangle around the faces detected with a thickness of ‘2’. This can be achieved by looping through the number of faces and using the coordinates of the faces returned from the image.

for (x,y,w,h) in faces:    
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

Draw a blue rectangle around the eyes detected with a thickness of ‘2’. This can be achieved by looping through the number of eyes detected on the face and using the coordinates of the eyes returned from the face detected.

NOTE: eyes are located on the face, so we can get the coordinates of the face area that was detected to detect the eyes. So face will be the region of interest (ROI) to detect the eyes.

for (x,y,w,h) in faces:    
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
roi_face = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_face)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_face, (ex, ey), (ex + ew, ey + eh), (255, 0, 0), 2)

Show the new image with the rectangles highlighting the detected face and eyes.

Image for post
Image for post
The Detected Faces & Eyes Image Example

If you want to save the new image then type the following. This will create an image file of the new image with the detected faces called ‘final_image.png’.


That is it, you are done creating your face detection program! If you want to see the code, just go to my GitHub account and you can look at the code there. Again if you want, you can watch and listen to me explain all of the code on my YouTube video.

Thanks for reading this article I hope its helpful to you all ! If you enjoyed this article and found it helpful please leave some claps to show your appreciation. Keep up the learning, and if you like machine learning, mathematics, computer science, programming or algorithm analysis, please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 ).

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store