Face recognition with python (pt1)


Introduction

In this tutorial series we will go through step by step process of developing a face recognition system using python programming language. Lets go!

         watch this tutorial on YouTube 

Prerequisites

This tutorial assumes you already have basic knowledge in python programming and you already have your favorite text editor installed. Additionally the following tools should be installed.

  1. OpenCV (pip install opencv-python)
  2. OpenCV Contrib (pip install opencv-contrib-python)
  3. Pillow (pip install Pillow)

Steps

To get our system to start recognizing  faces, there are a few steps that we should follow:

  1. Detect faces
  2. Create a face data-set
  3. Training
  4. Face recognition
In this part of the tutorial we will cover step 1 & 2.

Detect Faces and Create a face data-set

Face Detection is not Face recognition! Before you can start  to recognize whose face this is, you need to first find which one is a human face within a frame of a video, that is face detection. We are going to first detect and extract faces (from different angles the better) of a single person, we want to later recognize,  and store his/her faces in a folder we call a data-set. The faces will be used for training our system (We will cover this in part 2) so that we can recognize the same person contained in our data-set later. Our data-set will contain 30 images for each person (the more the faces the more intelligent our system will be.)
Enough of talking lets start coding!

Start by creating a folder and name it Face Recognition. Inside that folder create 2 other folders and name them creators and data respectively. The creators folder will contain files for detecting and extracting faces whilst the data folder, you guessed it, will contain our face data-set. 

Create a file creators.py inside the creators folder and start typing some code. 

 import cv2  
       faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")  
       video_capture = cv2.VideoCapture(0)  
      
Whats going on here?
Well we start by importing opencv so that we can use it in our code. The second line we load a pre trained classifier called haarcascade_frontalface_default using cv2. This file is contained in opencv, download it here and save it in creators folder.

line 3: We start the camera. Note that you use zero (0) to refer to machine webcam device. For other connected cameras devices try 1, 2  or 3...

Now that we have started the camera! Write the following.

 while True:  
       # Capture frame-by-frame  
       ret, frame = video_capture.read()  
       gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  
       faces = faceCascade.detectMultiScale(  
       gray,  
       1.3,  
       5  
       )  
      
Some complex code? Don't worry, I will explain line by line.


 ret, frame = video_capture.read()  
        

What you need to know is that face recognition can only be done on an image not a video. From the video being taken, this line captures a frame/image into the variable 'frame'. Videos may have up to 60 frames per second, this means our while loop will process 60 different images per second! 

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

Here, before we try to detect any faces on our video, we first convert the frame from BGR to a gray image. This simply reduces complexity in the frame for better results.

  
 faces = faceCascade.detectMultiScale(  
         gray,  
         1.3,  
         )  
        
Don't worry this is a single line of code arranged into 5 lines for readability. Remember faceCascade that we got from classifier haarcascade_frontalface_default.xml, just call that and pass the gray image and boom! All faces from that image are in faces. Easy.
1.3 and 5 are minimum and maximum object size respectively. Objects not within this range are ignored (Documentation).


Now that we have detected faces in the frame, whats left is extracting those faces, save them in the data folder and draw a rectangular shape around the face on the original frame. We are going to do all of that in just 10 lines of code!

 # Draw a rectangle around the faces  
   for (x, y, w, h) in faces:  
     num = num + 1  
     cv2.imwrite("User." + str(id) + "." + str(num) + ".jpg", gray[y:y + h, x:x + w])  
     cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)  
     cv2.waitKey(100)  
   # Display the resulting frame  
   cv2.imshow('Camera', frame)  
   cv2.waitKey(1)  
   if(num > 30):  
     break  
Important: Note that though we can obtain multiple faces in a single image, you should make sure only a sing face is on video because we want to name our faces for a particular person on a single shoot. But just in case, we will loop around faces.


The num variable will keel count of the number of faces we are saving for a single person in the data-set and when it get to 30 we break the loop and exit the video.

 cv2.imwrite("User." + str(id) + "." + str(num) + ".jpg", gray[y:y + h, x:x + w])  
          cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)  
        
Our code just ugly? These two line simple save a face to data folder and draw a rectangle on the original image.

The images in the data-set will have the structure:

"User." + str(id) + "." + str(num) + ".jpg"

For id we are going to prompt an input from the user which should be unique for each person.

The whole code.

 import cv2  
 faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")  
 video_capture = cv2.VideoCapture(0)  
 id = input("enter id: ")  
 num = 0  
 while True:  
   # Capture frame-by-frame  
   ret, frame = video_capture.read()  
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  
   faces = faceCascade.detectMultiScale(  
     gray,  
     1.3,  
     5  
   )  
   # Draw a rectangle around the faces  
   for (x, y, w, h) in faces:  
     num = num + 1  
     cv2.imwrite("User." + str(id) + "." + str(num) + ".jpg", gray[y:y + h, x:x + w])  
     cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)  
     cv2.waitKey(100)  
   # Display the resulting frame  
   cv2.imshow('Camera', frame)  
   cv2.waitKey(1)  
   if(num > 30):  
     break  
 # When everything is done, release the capture  
 video_capture.release()  
 cv2.destroyAllWindows()  


3. Training
,

If you have any questions or comments you can comment below and I will be glade to reply you.

Follow me on social networks

FacebookYouTube Channel

Comments