Face Detection with 15 lines of python code using OpenCV

Akash Bhiwgade
Analytics Vidhya
Published in
3 min readSep 20, 2019

Hello and welcome to the exciting world of computer vision where fun never ends.

Today, we will see how to detect faces in a video feed. Here we will use the primary webcam for learning purposes which can be surely replaced with any video file.

So, let’s get the ball rolling!

Pre-requisites:

1 — OpenCV
2 — Haarcascade XML files

Steps:

1 — Let us first turn on the webcam, read the video feed and display the same in a window.

2 — Add a condition which helps us to escape the video feed and close the display window used in step 1.

3 — Detect face and draw a rectangle surrounding the face.

Use these steps and we are done, period.

import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('Video feed', img)
key = cv2.waitKey(30) & 0xff
if key == 27:
break
cap.release()
cv2.destroyAllWindows()

The above code informs to import cv2 (opencv) for further processes. In the next line, we turn on the primary webcam by passing 0 as the parameter. If you have a secondary camera you can change 0 to 1.

The next line has an infinite loop which says that till the break condition is not encountered keep the video feed in display window.

Inside this loop we read whatever is received by the VideoCapture and by using imshow we display it on a window named as ‘Video Feed’.

Lastly, we add a break condition as if ‘ESC’ key is pressed then break the loop which brings it to the final lines to release the webcam and destroy the display window.

4 — Now the final manoeuvre, lets detect faces. For this step, we will load a Haarcascade ‘haarcascade_frontalface_default.xml’ using the below line of code.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

5 — To avoid any disturbances, we will conver the image read from webcam to grayscale usingthe below line of code.

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

6 — We will use the grayscale image for detecting the face using the below line of code.

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

The faces variable will have the length, breadth, height and width.

7 — We will use these as co-ordinates to plot a rectangle as seen in below for loop.

8 — The parameters passed while drawing rectangle are the actual image, start co-ordinates, end co-ordinates, color to use, width of border.

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

This is it, we have successfully written a face detector program using python. Congratulations!! See the full code below.

import cv2face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)cap = cv2.VideoCapture(0)while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 2)
cv2.imshow(‘image window’, img) k = cv2.waitKey(30) & 0xff if k == 27:
break
cap.release()
cv2.destroyAllWindows()

Thanks for visiting this article, I hope you enjoyed. Do share your feedback in the comments section below. Till the next time.

Best Regards,
Akash Bhiwgade

--

--