Analytics Vidhya
Published in

Analytics Vidhya

Face Recognition Using Knn & OpenCV

Project Link:

Photo by May on Unsplash


Face recognition is a crucial security application. Through this project, a very basic form of face recognition has been implemented using the Haar Cascades Classifier, openCV & K-Nearest Neighbors Algorithm.


The face is the most crucial entity for human identity. It is the feature that best distinguishes a person. And for the very same reasons, Face Recognition is an important technique. Face recognition is an interesting and challenging problem and impacts important applications in many areas such as identification for law enforcement, authentication for banking and security system access, and personal identification among others.

Face recognition is an easy task for humans but its an entirely different task for a computer. A very little is known about human recognition to date on How do we analyze an image and how does the brain encode it and Are inner features (eyes, nose, mouth) or outer features (head shape, hairline) used for successful face recognition? Neurophysiologist David Hubel and Torsten Wiesel have shown that our brain has specialized nerve cells responding to specific local features of a scene, such as lines, edges, angles or movement. Since we don‟t see the world as scattered pieces, our visual cortex must somehow combine the different sources of information into useful patterns. Automatic face recognition is all about extracting those meaningful features from an image, putting them into a useful representation and performing some classifications on them.

The whole process can be divided into three major steps where the first step is to find a good database of faces with multiple images for each individual. The next step is to detect faces in the database images and use them to train the face recognizer and the last step is to test the face recognizer to recognize faces it was trained for.

Technology Stack

Python — The whole code has been written in Python

cv2 — cv2 is the OpenCV module and is used here for reading & writing images & also to input a video stream

Algorithm — KNN

Classifier — Haar Cascades


  • Generating Training Data: The following steps are followed to generate training data

# Write a Python Script that captures images from your webcam video stream

# Extracts all Faces from the image frame (using haar cascades)

# Stores the Face information into numpy arrays

# 1. Read and show video stream, capture images

# 2. Detect Faces and show bounding box (haar cascade)

# 3. Flatten the largest face image(gray scale) and save in a numpy array

# 4. Repeat the above for multiple people to generate training data

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")

skip = 0
face_data = []
dataset_path = './data/'
offset = 10

file_name = input("Enter the name of the person : ")
while True:
ret,frame =

if(ret == False):

gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(frame,1.3,5)
faces = sorted(faces,key=lambda f:f[2]*f[3])

#pick the last face (largest)
for face in faces[-1:]:
x,y,w,h = face

#extract main face
face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
face_section = cv2.resize(face_section,(100,100))

skip += 1
if(skip%10 == 0):

cv2.imshow("VIDEO FRAME",frame)

keypressed = cv2.waitKey(1) & 0xFF
if(keypressed == ord('q')):

# convert our face list array into a numpy array
face_data = np.array(face_data)
face_data = face_data.reshape((face_data.shape[0],-1))

#save this data into file system + file_name + '.npy',face_data)
print("data successfully saved at " + dataset_path+file_name+'.npy')
  • Building The Face Classifier

# Recognise Faces using the classification algorithm — KNN.

# 1. load the training data (numpy arrays of all the persons)

# x- values are stored in the numpy arrays

# y-values we need to assign for each person

# 2. Read a video stream using opencv

# 3. extract faces out of it

# 4. use knn to find the prediction of face (int)

# 5. map the predicted id to name of the user

# 6. Display the predictions on the screen — bounding box and name

import cv2
import numpy as np
import os

########## KNN CODE ############
def distance(v1, v2):
# Eucledian
return np.sqrt(((v1-v2)**2).sum())

def knn(train, test, k=5):
dist = []

for i in range(train.shape[0]):
# Get the vector and label
ix = train[i, :-1]
iy = train[i, -1]
# Compute the distance from test point
d = distance(test, ix)
dist.append([d, iy])
# Sort based on distance and get top k
dk = sorted(dist, key=lambda x: x[0])[:k]
# Retrieve only the labels
labels = np.array(dk)[:, -1]

# Get frequencies of each label
output = np.unique(labels, return_counts=True)
# Find max frequency and corresponding label
index = np.argmax(output[1])
return output[0][index]

#Init Camera
cap = cv2.VideoCapture(0)

# Face Detection
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")

skip = 0
dataset_path = './data/'

face_data = []
labels = []

class_id = 0 # Labels for the given file
names = {} #Mapping btw id - name
# Data Preparation
for fx in os.listdir(dataset_path):
if fx.endswith(‘.npy’):
#Create a mapping btw class_id and name
names[class_id] = fx[:-4]
print(“Loaded “+fx)
data_item = np.load(dataset_path+fx)

Images of Working Demo




In addition to being used for security systems, authorities have found a number of other applications for facial recognition systems. While earlier post-9/11 deployments were well-publicized trials, more recent deployments are rarely written about due to their covert nature.

There are also a number of potential uses for facial recognition that are currently being developed. For example, the technology could be used as a security measure at ATMs. Instead of using a bank card or personal identification number, the ATM would capture an image of the customer’s face, and compare it to the account holder’s photo in the bank database to confirm the customer’s identity.

Facial recognition systems are used to unlock software on mobile devices. An independently developed Android Marketplace app called Visidon Applock makes use of the phone’s built-in camera to take a picture of the user. Facial recognition is used to ensure only this person can use certain apps that they choose to secure.

Face detection and facial recognition are integrated into the iPhoto application for Macintosh, to help users organize and caption their collections.

Also, in addition to biometric usages, modern digital cameras often incorporate a facial detection system that allows the camera to focus and measure exposure on the face of the subject, thus guaranteeing a focused portrait of the person being photographed. Some cameras, in addition, incorporate a smile shutter or take automatically a second picture if someone closed their eyes during exposure.

Because of certain limitations of fingerprint recognition systems, facial recognition systems are used as an alternative way to confirm employee attendance at work for the claimed hours.

Another use could be a portable device to assist people with prosopagnosia in recognizing their acquaintances.


Among the different biometric techniques, facial recognition may not be the most reliable and efficient. However, one key advantage is that it does not require the cooperation of the test subject to work. Properly designed systems installed in airports, multiplexes, and other public places can identify individuals among the crowd, without passers-by even being aware of the system. Other biometrics like fingerprints, iris scans, and speech recognition cannot perform this kind of mass identification. However, questions have been raised on the effectiveness of facial recognition software in cases of railway and airport security.


Face recognition is not perfect and struggles to perform under certain conditions. Ralph Gross, a researcher at the Carnegie Mellon Robotics Institute, describes one obstacle related to the viewing angle of the face: “Face recognition has been getting pretty good at full frontal faces and 20 degrees off, but as soon as you go towards profile, there’ve been problems.” Other conditions where face recognition does not work well include poor lighting, sunglasses, long hair, or other objects partially covering the subject’s face, and low-resolution images. Another serious disadvantage is that many systems are less effective if facial expressions vary. Even a big smile can render the system less effective. For instance: Canada now allows only neutral facial expressions in passport photos. There is also inconstancy in the datasets used by researchers. Researchers may use anywhere from several subjects to scores of subjects and a few hundred images to thousands of images. It is important for researchers to make available the datasets they used to each other, or have at least a standard dataset. On 18 January 2013 Japanese researchers created a privacy visor that uses nearly infrared light to make the face underneath it unrecognizable to facial recognition software.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store