REAL TIME FACE RECOGNITION USING KNN

Isha Dhanya
4 min readMay 31, 2021

Introduction

Fасe reсоgnitiоn is а methоd оf identifying оr verifying the identity оf аn individuаl using their fасe. Fасe reсоgnitiоn systems саn be used tо identify рeорle in рhоtоs, videо, оr in reаl-time. Lаw enfоrсement mаy аlsо use mоbile deviсes tо identify рeорle during роliсe stорs. Fасiаl reсоgnitiоn teсhnоlоgy (FRT) hаs emerged аs аn аttrасtive sоlutiоn tо аddress mаny соntemроrаry needs fоr identifiсаtiоn аnd the verifiсаtiоn оf identity сlаims.

Our project here mainly aims to identify a person without any physical interaction on behalf on the user. We can easily identify a person if the person appears in front of any camera or video system that generates images of sufficient quality and resolution. We have recognized an individual using Machine Learning algorithm.

Problem Statement

The problem statement is to recognize the face that is feeded into the model via webcam. The image will be feeded to the pre-trained model via a webcam. This venture was finished with OpenCV, Windows 10 as OS and Python, as language.

Algorithm

KNN Algorithm:

1. Load the Data

2. Initialize K to your chosen number of neighbors

3. For example in the data-

(i) Calculate the distance between the query example and

the current example from the data.

(ii) Add the distance and the index of the example

to an ordered collection

4. Sort the ordered collection of distances and indices from the smallest to largest (in

ascending order) by the distances

5. Pick the first K entries from the sorted collection

6. Get the labels of the selected K entries

7. For classification, return the mode of the K labels

Haar cascade:

It is an Object Detection Algorithm used to identify faces in an image or a real time video. The algorithm uses edge or line detection features.

It is trained by superimposing the positive image over a set of images. This type of training is generally done on a server and on various stages.

The haar feature continuously traverses from the top left of the image to the bottom right to search for the particular feature.

Data Flow diagram

Libraries used in this project:

Scikit-Learn (Sklearn)

Numpy- Ndarray objects

OS Module

OpenCV

Working of the project

To design a real-time face recognition system, work was done in basically three steps:

1.DATA COLLECTION

Generating Selfie training data using webcam

Writing a python script that captures images from Webcam and it detects face. Also the script draws a bounding box around each face.

If there are multiple faces, we are going to take the biggest face, crop it, then save it in the numpy array.

We will ask the user to give the name of the person of whose face it is. We will create a 2dimensional matrix, which is an image (picture) itself and we are going to flatten it in the form of linear array and save it as a numpy file.

We are going to do it for multiple persons. First we will take images of person A, person B, person C, then we are going to store all these data in the numpy array and this will help us to get the training data.

We are going to generate real time training data by taking selfies from webcam using the python script.

Detect faces and show boundary box

We are going to make use of face cascade object, and this object has one method which is called detect MultiScale

2. TRAINING THE DATA

Storing every tenth frame from the video stream

For storing every tenth frame we will make an array s

If there are multiple faces, then there would be sorting based on width and height

We will use lambda function to sort the faces based upon this key. We are going to do sorting based upon the area of the face.

So, we write:

Faces=sorted (faces, key=lambda f:f[2]*f[3])

We are going to do sorting so that the largest face comes to the front of the list. While iterating through the faces, we can pick the last face, which is the biggest also. We can start from -1 to the end.

After every tenth frame we have increased the counter by one.

3. PREDICTING THE DATA

To flatten the faces

We need to flatten our face image and save it in a numpy array. Number of rows should be the same as number of faces. Number of columns should be figured out automatically.

Saving data and recognizing the face

Now we are going to save this data into file system np.save(dataset_path+filename+”.npy”,face_data)

File name should be taken as input. We should ask the user to give the name of the person, whose face we are scanning.

There would be multiple files created in the data folder every time we run the script, one file will be created. We save the file and we print one statement

Print(“data successfully saved at”+dataset+file_name+”.npy”)

Recognizing the entered data, i.e Kanishk Raj

CONCLUSION

Here, in this project we have recognized an individual using Machine Learning algorithm. We have trained our dataset using approximately 50 sets of images of different individuals using OpenCV and applied K-Nearest Neighbors Algorithm and used Hааr Саsсаde сlаssifier tо identify fасes in аn imаge оr а reаl time videо.on our dataset. Then after training the dataset we then tested our algorithm using our Laptop Webcam. Our model worked fine with a decent accuracy of 85%.

In the lоng run, rоbоts using fасiаl reсоgnitiоn teсhnоlоgy mаy аlsо соme tо fоrаy. They саn be helрful in соmрleting the tаsks thаt аre imрrасtiсаl оr diffiсult fоr humаn beings tо соmрlete.

Team members

Devashish Mandal (ENG18CS0086)

Isha Dhanya (ENG18CS0116)

Kanishk Raj (ENG18CS0124)

--

--