Facial detection using OpenCV

Puja Chaudhury
3 min readDec 8, 2019

--

Object detection and recognition have been the talk of the town for the most part of the 2010s. Be it in medical imaging, security surveillance or the facial recognition system built in your smartphone, object recognition has found its place in almost all aspects of modern society. Before models of deep learning became more mainstream, object detection and recognition was mainly done using Computer Vision and is still widely used.

In this tutorial, we will learn how to make a basic facial recognition model using Computer vision. The module we are going to be using today is called OpenCV. It includes a library of statistical machine learning algorithms like Boosting, Decision tree learning, Gradient boosting trees, etc and was first launched by intel in 1999.

This tutorial will be divided into two parts. The first part will focus on facial detection and the second part will focus on facial recognition. This is going to be the first part, where we will learn to detect the presence of faces and eyes.

Facial detection

Step 1. Install the OpenCV library: pip install opencv-python

Try pip install opencv-contrib-python if you encounter an error while running your program.

Step 2. Now that you have installed OpenCV, locate the .xml files for various haarcascade features by running the command below in your command shell. Move the data folder to the directory you are correctly working in.

Step 3. Lets code!

We Import our libraries and load in the face and eye cascades. We can add any number of cascades and experiment with different objects.

import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:/Users/_ _/Documents/cv/data/haarcascade_frontalface_alt2.xml')eye_cascade = cv2.CascadeClassifier('C:/Users/_ _/Documents/cv/data//haarcascade_eye.xml')cap = cv2.VideoCapture(0)

Then we begin our loop to detect the face. We need to convert our image to gray because that is just how OpenCV works and scale it accordingly. For more information on OpenCV syntax visit their official documentation.

After detection of the face, we save it in the region of interest or roi variable and create rectangles around it. We do the same with the eyes, but we put the loop for eyes, inside the loop for faces because it would be very odd to find a pair of eyes outside of a face!

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),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]

eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

Then we finish up the code with a delay and cap release.

cv2.imshow('img',img)    
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()

And we are done! The results should look something like this.

You can find the full code here.

Part two of the project will be up very soon where we will see how to recognize a person in real-time after training our model with custom images!

Thank you for your time!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Puja Chaudhury
Puja Chaudhury

Written by Puja Chaudhury

Masters' student in Robotics focusing on machine learning. Experienced in AI, NLP, and CV through internships, research projects, & published papers.

No responses yet

Write a response