Naveen Manwani

@naveenmanwani

Face-Recognition Using OpenCV: A step-by-step guide to build a facial recognition system

Whenever you hear the term Face Recognition, you instantly think of surveillance in videos, and would could ever forget the famous Opening narration “ You are being watched. The government has a secret system, a machine that spies on you every hour of every day. I know because I built it” from Season One of the television show person of interest .I still get goosebumps whenever I hear it.

First thing first

Literature:

Over the last ten years or so, face recognition has become a popular area of research in computer vision and one of the most successful applications of image analysis and understanding. Because of the nature of the problem, not only computer science researchers are interested in it, but neuroscientists and psychologists also. It is the general opinion that advances in computer vision research will provide useful insights to neuroscientists and psychologists into how human brain works, and vice versa.

Face Recognition systems use computer algorithms to pick out specific, distinctive details about a person’s face. These details, such as distance between the eyes or shape of the chin, are then converted into a mathematical representation and compared to data on other faces collected in a face recognition database. The data about a particular face is often called a face template and is distinct from a photograph because it’s designed to only include certain details that can be used to distinguish one face from another.

Source: https://thefwa.com/cases/ava-sessions-t64

After explaining you in brief about what is face recognition and how it works it’s time to clear my intentions with regards to this article . In this article I’ll introduce you with very simple easy to follow python code to build a Face Recognition system that will run on a video of any person of your choice.

So, without stalling further let’s deep dive in, and I assure you by the end of this article you’ll be able to make your own face recognition over video pretty smoothly.

Installation

Requirements

  • Python 3.3+ or Python 2.7
  • macOS or Linux (Windows not officially supported, but might work)
  • OpenCV

Installation Options:

Installing on Mac or Linux

First, make sure you have dlib already installed with Python bindings:

Then, install this module from pypi using pip3 (or pip2 for Python 2):

pip3 install face_recognition

If you are having trouble with installation, you can also try out a pre-configured VM.

After installing all the dependency it’s time to get your hands dirty, which means to write the code which will make bring your Face Recognition system into existence.

# module and library required to build a Face Recognition System
import face_recognition
import cv2
# objective: this code will help you in running face recognition on a video file and saving the results to a new video file.
# Open the input movie file
# "VideoCapture" is a class for video capturing from video files, image sequences or cameras
input_video = cv2.VideoCapture("input.mp4")
#"CAP_PROP_FRAME_COUNT": it helps in finding number of frames in the video file.
length = int(input_video.get(cv2.CAP_PROP_FRAME_COUNT))
# Create an output movie file (make sure resolution/frame rate matches input video!)
#  So we capture a video, process it frame-by-frame and we want to save that video, it only possible by using "VideoWriter" object
# FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent.
fourcc = cv2.VideoWriter_fourcc('M','P','E','G')
# 25.07-  number of frames per second (fps)
#(1280,720)- frame size
output_video = cv2.VideoWriter('output.avi', fourcc, 25.07, (1280, 720))
# Load some sample pictures and learn how to recognize them.
female_image = face_recognition.load_image_file("warina.jpg")
female_face_encoding = face_recognition.face_encodings(female_image)[0]
#  "face_recognition.face_encodings": it's a face_recognition package which returns a list of 128-dimensional face encodings
male_image = face_recognition.load_image_file("aayush.jpeg")
male_face_encoding = face_recognition.face_encodings(male_image)[0]

known_faces = [
    female_face_encoding,
    male_face_encoding
]

The images I used are:

Bollywood Actress and Actor : Warina Hussain and Aayush sharma
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
frame_number = 0
while True:
    # Grab a single frame of video
    ret, frame = input_video.read()
    frame_number += 1
# Quit when the input video file ends
    if not ret:
        break
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_frame = frame[:, :, ::-1]
# Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
face_names = []
    for face_encoding in face_encodings:
        # See if the face is a match for the known face(s)
        match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)

        name = None
        if match[0]:
            name = "Warina"
        elif match[1]:
            name = "Aayush"
face_names.append(name)
# Label the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue
# Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
# Write the resulting image to the output video file
    print("Writing frame {} / {}".format(frame_number, length))
    output_video.write(frame)
# All done!
input_video.release()
cv2.destroyAllWindows()

And that’s it , it’s done if you follow along exactly the way it’s been shown here then you all too will achieve what I achieved.

The demo for the Face Recognition system which I built can be seen below.

REFERENCES:

  1. The GitHub repo can be found here.
  2. In order to know more about Face Recognition click here.
  3. In order to know more about the library dlib click here.
  4. In order to know more about OpenCV click here.

Thank you for your attention

You using your time to read my work means the world to me. I fully mean that.

If you liked this story, go crazy with the applaud( 👏) button! It will help other people to find my work.

Also, follow me on Medium, LinkedIn or Twitter if you want to! I would love that.

More by Naveen Manwani

Topics of interest

More Related Stories