Let's Build Facial Recognition System - part 1


Over the last decade or so, face recognition has become a popular topic for computer vision researcher and one of the most successful applications of image analysis and understanding. Day by day, they always try to build the better libs which help us to create face recognition system with ease.

Now, let's define what face recognition system is:

A face recognition system is a computer application capable of identifying or verifying a person from a digital image or a video frame from a video source. Source Wikipedia

The process of facial recognition system is quite simple where we can split it into two simple steps:

  1. Find faces: identify the face from the given image or video.
  2. Recognize faces: search the faces where we found in step 1 with our faces database in the system.

However in this post, we're going to talk about first step which is finding the face from our computer webcam.

What we need

Decade ago, to build this system you need to do everything on your own from scratch. Now, we can harvest the help from some libs which help us speed up our development. Libraries which we'll use in the code in this post are:

  • Python3.6: programming language use in this post
  • OpenCV: the most popular library for computer version and machine learning. How to set it up here
  • dlib: is a cross-platform package for threading, networking, numerical operations, machine learning, computer vision, and compression, placing a strong emphasis on extremely high-quality and portable code. How to set it up here
  • imutils: A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV. Checkout its official link here

Let's code

By using opencv library cv2 and dlib, we can build a face detection with just few lines of code and here they are:

# load the needed package
from imutils.video import VideoStream
from imutils import face_utils
import imutils
import time
import cv2
import dlib

print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()

print("[INFO] camera sensor warming up...")
vs = VideoStream().start()

# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream, resize it to
    # have a maximum width of 600 pixels, and convert it to
    # grayscale
    frame = vs.read()
    frame = imutils.resize(frame, width=600)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     # detect faces in the grayayscale frame
    rects = detector(gray, 0)

    # loopop over the face detections
    # and draw rectangle around face
    for rect in rects:
        (x,y,w,h) = face_utils.rect_to_bb(rect)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255), 1)

    # show the frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

     # if the `q` key was pressed, break from the loop
    if key == ord("q"):
# do a bit of cleanup

It's time to run our code and here the result

Awesome, it's working.


What next

By using additional library like dlib, opencv we can easily write the code to find the faces from the given images, videos. Next, we are going to search the found faces in our faces database. Then we will build a small web application for real time face recognition. So stay tuned for my next post.

All Rights Reserved

Let's register a Viblo Account to get more interesting posts.