Face Recognition using Computer Vision

img

Face recognition using Computer Vision is a method of identifying or verifying the identity of an individual using their face. Face recognition systems can be used to identify people in photos, videos, or in real time.

  • Preventing crime
  • Unlock device
  • Blind assistance
  • Attendance system
  • Payments

Installing Libraries:-

OpenCV-Contrib: pip install opencv-contrib-python

Fisher faces Recognizer:-

  • Fisher faces algorithm extracts principle components that separate one individual from another. So, now an individual’s features can’t dominate another person’s features.
  • The Fisher face method will be applied to generate a feature vector of facial image data used by the system and then to match the vector of traits of the training image with the vector characteristic of the test image using the euclidean distance formula

LBPHFaceRecognizer:-

Local Binary Pattern (LBP) is a simple yet very efficient texture operator which labels the pixels of an image by thresholding the neighborhood of each pixel and considers the result as a binary number.

It doesn’t look at the image as a whole but instead tries to find its local structure by comparing each pixel to its neighboring pixels.

LBPH uses 4 parameters

  • Radius – to build the circular local binary pattern and represents the radius around the central pixel. It is usually set to 1.
  • Neighbors -: The more sample points you include, the higher the computational cost. It is usually set to 8
  • X Grid – the number of cells in the horizontal direction.
  • Y Grid – the number of cells in the vertical direction.

Face recognition using Computer Vision

Block Diagram:-

Algorithm of Face recognition using Computer Vision

The Cascade Classifier is an algorithm you will learn to classify a certain object to start training. We need two sets of images the first set with faces with the positive images you want to detect and the second set of images called the images negative which are images of anything but ease.

Creating Datasets:-

import cv2, os
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'datasets'
sub_data = 'sanjay'

path = os.path.join(datasets, sub_data)
if not os.path.isdir(path):
os.mkdir(path)
(width, height) = (130, 100)

face_cascade = cv2.CascadeClassifier(haar_file)
webcam = cv2.VideoCapture(2)

count = 1
while count < 31:

print(count)
(_, im) = webcam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
for (x,y,w,h) in faces:

cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (width, height))
cv2.imwrite('%s/%s.png' % (path,count), face_resize)

count += 1

cv2.imshow('OpenCV', im)
key = cv2.waitKey(10)
if key == 27:
break

webcam.release()
cv2.destroyAllWindows()

Source Code:-

import cv2, numpy, os

size = 4

haar_file = 'haarcascade_frontalface_default.xml'

datasets = 'datasets'

print('Training...')

(images, labels, names, id) = ([], [], {}, 0)

for (subdirs, dirs, files) in os.walk(datasets):

for subdir in dirs:

names[id] = subdir

subjectpath = os.path.join(datasets, subdir)

for filename in os.listdir(subjectpath):

path = subjectpath + '/' + filename

label = id

images.append(cv2.imread(path, 0))

labels.append(int(label))

id += 1

(width, height) = (130, 100)

(images, labels) = [numpy.array(lis) for lis in [images, labels]]

model = cv2.face.LBPHFaceRecognizer_create()

#model =  cv2.face.FisherFaceRecognizer_create()

model.train(images, labels)

# Part 2: Use fisherRecognizer on camera stream

face_cascade = cv2.CascadeClassifier(haar_file)

webcam = cv2.VideoCapture(0)

cnt=0

while True:

(_, im) = webcam.read()

#im = imutils.resize(im, width=200)

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

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:

cv2.rectangle(im,(x,y),(x+w,y+h),(255,255,0),2)

face = gray[y:y + h, x:x + w]

face_resize = cv2.resize(face, (width, height))

#Try to recognize the face

prediction = model.predict(face_resize)

cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)

if prediction[1]<800:

cv2.putText(im,'%s - %.0f' % (names[prediction[0]],prediction[1]),(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(255, 0, 0))

print (names[prediction[0]])

cnt=0

else:

cnt+=1

cv2.putText(im,'Unknown',(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))

if(cnt>100):

print("Unknown Person")

cv2.imwrite("input.jpg",im)

cnt=0

cv2.imshow('OpenCV', im)

key = cv2.waitKey(10)

if key == 27:

break

webcam.release()

cv2.destroyAllWindows()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.