Face Recognition System using Deep Learning/Convolutional Neural Network.  You have to first collect data, then train your model and the final step is to make a prediction.

What will you learn?

  1. Generate a dataset of authorized users
  2. Create a label using a generated dataset
  3. Create data: Train-Test split
  4. Create the model
  5. Visualize the data and make a prediction

View Complete Documentation

1. Generate a dataset

First of all, I must create the dataset. That implies I'll have to gather pictures. I can use the OpenCV package for this task.

To install this package, open any terminal and type:

pip install opencv-python

I am using the haarcascade_frontalface_default XML file to get the coordinates of my face so that I can crop it and use it to make a dataset and for future prediction.

I need to convert my RGB (Red, Green, Blue) images to grayscale. An RGB image has three channels, whereas a grayscale image has only one.
Turning an image into a grayscale makes things much simpler. Then, we can use a face classifier and a technique called detectMultiScale to find the coordinates we need. We must pass the scaling factor as well as the minimal neighbors. The scale factor is the factor that determines how much to modify from the original image. The number of minimum neighbors indicates how many faces to detect. A greater value yields less detection but of higher quality. We must experiment with this value. We can alter these parameters depending on our results. Then we simply collect the image and terminate the execution when it reaches 200 images or when we press the Enter key on the keyboard.

import cv2
def generate_dataset():
    face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    def face_cropped(img):
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_classifier.detectMultiScale(gray, 1.3, 5)
        
        if faces is ():
            return None
        for (x,y,w,h) in faces:
            cropped_face = img[y:y+h,x:x+w]
        return cropped_face
    
    cap = cv2.VideoCapture(1)
    img_id = 0
    
    while True:
        ret, frame = cap.read()
        if face_cropped(frame) is not None:
            img_id+=1
            face = cv2.resize(face_cropped(frame), (200,200))
            face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
            #file_name_path = "data/"+"Ishwar."+str(img_id)+".jpg"
            file_name_path = "Images for visualization/"+str(img_id)+'.jpg'
            cv2.imwrite(file_name_path, face)
            cv2.putText(face, str(img_id), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2 )
            
            cv2.imshow("Cropped_Face", face)
            if cv2.waitKey(1)==13 or int(img_id)==20:
                break
                
    cap.release()
    cv2.destroyAllWindows()
    print("Collecting samples is completed !!!")
#generate_dataset()

2. Create a label

A numpy module can be used to create a label. In this part, we just need to convert the user name into a numpy array.

import numpy as np # pip install numpy
def my_label(image_name):
    name = image_name.split('.')[-3] 
    #suppose your dataset contains two person
#     if name=="Ishwar":
#         return np.array([1,0])
#     elif name=="Manish":
#         return np.array([0,1])
    
    
    # suppose your dataset contains three person
    if name=="Ishwar":
        return np.array([1,0,0])
    elif name=="Manish":
        return np.array([0,1,0])
    elif name=="Bijay":
        return np.array([0,0,1])

3. Create data

This part involves creating data from images. OpenCV can be used for this purpose. Also, we need to divide our data into training and testing parts.

import os
from random import shuffle
from tqdm import tqdm
def my_data():
    data = []
    for img in tqdm(os.listdir("data")):
        path=os.path.join("data",img)
        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img_data = cv2.resize(img_data, (50,50))
        data.append([np.array(img_data), my_label(img)])
    shuffle(data)  
    return data
data = my_data()
train = data[:2400]  
test = data[2400:]
X_train = np.array([i[0] for i in train]).reshape(-1,50,50,1)
print(X_train.shape)
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1,50,50,1)
print(X_test.shape)
y_test = [i[1] for i in test]

4. Create the model

This part involves creating a model with tflearn. The convolutional neural network plays a major role in creating a model. We utilize a ReLU activation layer.

# import warnings
# warnings.filterwarnings('ignore')

import tensorflow as tf
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
tf.reset_default_graph()
convnet = input_data(shape=[50,50,1])
convnet = conv_2d(convnet, 32, 5, activation='relu')
# 32 filters and stride=5 in order for the filter to move 5 pixels or units at once.
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 3, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate = 0.001, loss='categorical_crossentropy')
model = tflearn.DNN(convnet, tensorboard_verbose=1)
model.fit(X_train, y_train, n_epoch=12, validation_set=(X_test, y_test), show_metric = True, run_id="FRS" )

5. Let's visualize the data and make a prediction

Now it's time to visualize our data and make a prediction. Our model should identify authorized users.

def data_for_visualization():
    Vdata = []
    for img in tqdm(os.listdir("Images for visualization")):
        path = os.path.join("Images for visualization", img)
        img_num = img.split('.')[0] 
        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img_data = cv2.resize(img_data, (50,50))
        Vdata.append([np.array(img_data), img_num])
    shuffle(Vdata)
    return Vdata
Vdata = data_for_visualization()
import matplotlib.pyplot as plt   # installation command: pip install matplotlib

fig = plt.figure(figsize=(20,20))
for num, data in enumerate(Vdata[:20]):
    img_data = data[0]
    y = fig.add_subplot(5,5, num+1)
    image = img_data
    data = img_data.reshape(50,50,1)
    model_out = model.predict([data])[0]
    
    if np.argmax(model_out) == 0:
        my_label = 'Ishwar'
    elif np.argmax(model_out) == 1:
        my_label = 'Manish'
    else:
        my_label = 'Bijay'
        
    y.imshow(image, cmap='gray')
    plt.title(my_label)
    
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)
plt.show()

Final output:

Conclusion:

In this way, you can identify and recognize the face of an authorized user using Deep learning. Make sure you collect the face of a user properly. The quality of your output is determined by how your face is generated. I recommend that you watch the video step by step I have provided.

I hope this project is helpful to you. If you have any questions, you can ask me in the comment section below. I will reply as soon as possible. Thanks.