Face Detection using Haar cascade xml files in python

Malini Anbazhagan
3 min readFeb 18, 2021

This is my first medium post and I am writing about a small code I created to detect faces.

Note that the face detection and face recognition is different as the name suggests. Face detection can be used to plot/detect face in an image or video while face recognition can be used to plot/detect a face with a name.

There are various ways to detect faces in an image. For example, build and train a ML model or use a trained model for your database to detect a face in an image.

Haar cascade is an effective object detection method proposed by Paul Viola and Michael Jones in their paper. It’s a machine learning based approach where a cascade function is trained from a lot of positive and negative images. Haar cascade uses a adaboost classifier.

5 steps to Face Detection

  1. Install and Import OpenCV and PIL package
  • Type the below listed commands in your terminal where your python package is located. you can use pip/pip3 depending on the python you are using. Also, If you are using Anaconda, you can also use “conda” command instead of pip
pip install opencv-python
pip install Pillow
  • There are two options to download. Either clone the entire opencv project or use “DownGit” or “GitZip”. There are various online software that can be used to download the files from Github without downloading the whole project.
  • Here is the link https://github.com/opencv/opencv/tree/master/data/haarcascades
  • The image below shows the interface if you are using Downgit
  • Haarcascade_frontalface_default.xml is the only file needed for this piece of code
  • Let’s jump to the code now.
import cv2
import PIL.Image

2. Read the Cascade file and the input image

  • Use OpenCV to load the xml file. Provide full path to the xml file and input image. Also use image formats supported by OpenCV such as Windows bitmap (bmp), Portable image formats (pbm, pgm, ppm), Sun raster (sr, ras),JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2), TIFF files (tiff, tif), Portable network graphics (png)
  • I love BTS K-pop group so I am using their poster as input image.
# Load the Haar cascade xml cascadeface_cascade=cv2.CascadeClassifier('.../haarcascade_frontalface_default.xml')# Read the input 
imageimg = cv2.imread('..your_path_to_image_file../bts.jpg')
*Images may be subject to copyright

3. Detect faces and Draw a rectangle around the faces

# Detect faces
faces = face_cascade.detectMultiScale(img, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

4. Display the resultant image

  • Here, I have used OpenCV to write the image to a location. you can use cv2.imshow() to show the result directly.
# Display the outputcv2.imwrite('..your_path_to_store_image../face_deteted.jpg', img) Real=PIL.Image.open("your_path_to_stored_image/face_deteted.jpg")

Result

Here is the full code.

--

--

Malini Anbazhagan

I am an AI Enthusiast. I love to read about new libraries & models and implement them.