Simple Face detection using OpenCV (Python)

Nivedha
3 min readOct 19, 2022

--

Face detection / Facial detection is an AI-based technology to identify faces from digital images. In this article, we’ll see an easy way to develop a face detection program using Haar Cascade Algorithm in OpenCV (Python)

To begin with, we need OpenCV installed. To install OpenCV run the following command in the terminal.

pip install opencv-python

Next, we need to download the Haar Cascade trained classifier in our workspace to detect faces. This is a pre-trained classifier that is trained with a set of input data. You can find this trained classifier XML file (haarcascade_frontalface_default.xml) from the following link.

https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml

Now, with the OpenCV installed and trained classifier file in the workspace let us perform the face detection in images and also using real-time video.

Face detection in image

To detect faces from images, have the image downloaded in the workspace. Here I have a sample image downloaded in my workspace as ‘demo.jpg’. The coding part is as,

"""
Face detection using OpenCV
Author: Nivedha
"""
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('demo.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 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)

# Display the output
cv2.imshow('img', img)
cv2.waitKey()

Decoding the above code as,

  1. Import OpenCV
  2. Load the Haar Cascade trained classifier to our code
  3. Read image from the workspace
  4. Convert the color to grayscale image (face detection works with only grayscale image because by converting to grayscale the luminance and chrominance planes are separated which is important for distinguishing features in the image)
  5. Store the co-ordinates of the faces detected from the image in the variable ‘faces’
  6. Draw rectangle around the faces detected using the co-ordinates stored in the variable ‘faces’
  7. Display the output image with faces detected

Output,

Face detection in real-time video

Now let us perform face detection in videos. The working method is the same as above in the images except for we perform an infinite loop to loop through the video and detect faces from every frame (as we know that video is made up of frames which are basically images). The coding part is as,

"""
Face detection using realtime video
Author: Nivedha
"""
import cv2
#load the cascade
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#capture video from webcame
vid=cv2.VideoCapture(0)
#performing face detection for each frame
while(True):
#reading frame
__, img=vid.read()
#converting to grayscale
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#detect faces
faces=face_cascade.detectMultiScale(gray,1.1,4)
#draw rectangle around each face
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#display
cv2.imshow('img',img)
#to stop
x=cv2.waitKey(30) & 0xff
if x==27:
break
#release videocapture object
vid.release()

From the above code, we can infer that a while loop is performed that performs the face detection in each frame of the video and this is looped until the ESC key (key code = 27) is pressed.

You can find the complete code from the GitHub repo link below

Hope this article may have helped you with what you are looking for. Any improvements or suggestions for the article are welcome. Cheers :)

Socials: Linktree

--

--