With this article I want to show you how easy it is to do a simple practice with Python to detect faces in real time on a webcam. We will use the powerful Python OpenCV library and an already trained algorithm called haarcascade. If after this your curiosity continues to pique, we will continue expanding the tutorials on opencv !.

OpenCV (Open Source Computer Vision Library) is a free software library created for computer vision and learning. OpenCV was created to provide a common infrastructure for vision applications and to accelerate the use of machine perception in commercial products.

And once you know what OpenCV is, open your favorite code editor and we start !!!

First of all, make sure you have OpenCV installed. You can install it using pip:

pip install opencv-python

Face detection using Haar cascades is a machine learning based approach in which a cascading function is trained with an input dataset. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc. Today we will use the face classifier. You can also experiment with other classifiers.

You must download the classifier XML file called (haarcascade_frontalface_default.xml), I have left it here for you to download easily. Once downloaded, place it in your workspace where you are programming this Python script

Download haarcascade

Some things to keep in mind:


Detection only works on grayscale images. Therefore, it is important to convert the color image to grayscale. (line 8)

gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)


The detectMultiScale function is used to detect faces. 3 arguments are needed:

faces = face_cascade.detectMultiScale (gray, 1.1, 4)

  • Input image
  • ScaleFactor
  • MinNeighbors

. LET'S EXPLAIN THEM

ScaleFactor specifies how much the image is reduced in size with each scale.

MinNeighbors specifies how many neighbors each candidate rectangle must have to retain it.

The faces contains a list of coordinates for the rectangular regions where they were found. We use these coordinates to draw the rectangles on our image.

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

Now that we are clear about how faces can be detected in static images, we go to Videos, detect faces in real time. As you know, videos are basically made up of frames, which are still images. Then we perform face detection for each frame in a video.

The only difference here is that we use an infinite loop to cycle through each frame in the video. We use cap.read () to read each frame.

while True:
# Read the frame
_, img = cap.read ()
# Convert to grayscale
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale (gray, 1.1, 4)
# Draw the 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)
# Stop if escape key is pressed
if cv2.waitKey (10) == ord ("q"):
break

The first return value is an indicator that indicates whether the frame was read correctly or not. The second value returned is the fixed box in which we will perform the detection.

Well now that we have everything explained we are going to put the code together and detect faces with Python

import cv2
face_cascade = cv2.CascadeClassifier ("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture (0)
while True:
# Read the frame
_, img = cap.read ()
# Convert to grayscale
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale (gray, 1.1, 4)
# Draw the 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)
# Stop if escape key is pressed
if cv2.waitKey (10) == ord ("q"):
break
cap.release ()

Once it has worked for you .. you are encouraged to detect the face and EYES !!!. Remember to use another haarcascade algorithm.