.st0{fill:#FFFFFF;}

Face Recognition using MATLAB mobile 

 May 12, 2021

By  Prithvi

Face recognition

Many of us use the face-unlock features on our smartphones to quickly unlock our devices. This uses some type of face recognition algorithm and different versions of it can be seen implemented in social media websites to identify people, in-camera applications, and Google photos.

Face_recognition

We can use the robust neural networks provided by MATLAB and train them to recognize faces. Our program has used AlexNet, a Convolutional Neural Network trained to classify 1000 everyday objects. We then use Transfer learning techniques to train it to recognize faces discussed in the upcoming sections.

Face recognition can be implemented practically on our smartphones due to its portability and the fact that cameras on smartphones have essentially become professional grade. We can develop a face recognition program directly on a smartphone with the help of MATLAB mobile.

Prerequisites for the program

1) You must have the Deep Learning Toolbox along with the AlexNet support package installed in your current MATLAB version.

2) You must have MATLAB mobile application installed on your smartphone.

3) You must install and set up your MATLAB drive and sync it with your MATLAB desktop.

Capturing Images from the Camera

The whole face recognition code is separated into 3 separate programs. The first one involves capturing images from the camera to form a database.

The steps involved are

1) Creating a mobile developer object to retrieve data from the camera(sensor)

2) Taking snapshots of the live video stream of the camera using a loop.

3) Using Viola-Jones algorithm to detect faces in the image and bounding box(bbox) matrix for the rectangle around the face.

4) Cropping and resizing to 227x227 size to match AlexNet input size and saving it in .bmp format.

5) We repeat the above steps the same number of times as there are many faces to identify, which is 4.

%Topic:Program to capture images from mobile camera and store it
%Author:Prithvi Kishan
%Date:02-04-21
clc;close;clear;
%Creating a mobiledev object to obtain data from sensors
%the sensor we use is the camera
m=mobiledev;
cam=camera(m);
%UsingViola-Jones algorithm to detect objects(people's faces).
faceDetector=vision.CascadeObjectDetector;
c=50; %50 images for each individual in database
temp=0;%Count of number of snapshots currently captured.
%Loop for capturing the images
while true
pic=cam.snapshot('manual');%Taking an RGB photo and storing it in pic
bbox =faceDetector(pic);
% Creating bouding box around face in pic by calling systemobject facedetector
% the above step returns an M-by-4 matrix, bbox, that defines
% M bounding boxes containing the detected objects
if(sum(bbox)~=0) %When sum(bboxes) is not zero, face is detected
if(temp>=c)
break;
else
img=imcrop(pic,bbox(1,:)); %Cropping the image to the boudingboxes values.
img=imresize(img,[227 227]); %Resizing to match AlexNet inputsize.
filename=strcat(num2str(temp),'.bmp');%Saving the image in correct name format
imwrite(img,filename);
temp=temp+1;
imshow(img);
drawnow;
end
else
imshow(pic);
drawnow;
end
end

Sample image in the database

Sampleimg

Training Network to recognize faces

Since AlexNet by itself cannot recognize faces, we need to modify the 23rd and 25th layers. You can better understand the working of AlexNet by reading its documentation. Even though this code can be run directly on your smartphone, it would be better to run it on your desktop or laptop due to its higher computing power and work faster.

AlexNet Network Analysis

AlexNet analysis

The steps involved in this program are:

1) Load AlexNet onto a variable(net).

2) Extract the layers property of the network onto another variable ‘layers’ which will have the new properties of the new network.

3) Pass a fullyConnectedLayer with parameter 4(number of classes) to the 23rd layer and a classificationLayer to the 25th layer.

4) Load the image database onto a variable.

5) Then, we split the image database into a training set and a validation set in the ratio 7:3.

6)We perform image augmentation on the training set using the ‘imageDataAugmenter’ function.

7) Then, we can use the ‘trainingOptions’ functions to create a variable that selects the training parameters like solvername, MiniBatchSize, MaxEpochs, ValidationFrequency, etc.

8) We use the database, layer, and training options parameters to train the network from the ‘trainNetwork’ function and save the network with an appropriate name.

clc;close;clear;
%Loading Alexnet network and storing it in net
net = alexnet
layers=net.Layers;
%Using Transfer learning techniques and changing the 23rd and 25th layer
layers(23)=fullyConnectedLayer(4); % The parameter 4 implies 4 classes(faces) to be identified.
layers(25)=classificationLayer;
inputSize = layers(1).InputSize % Input size of Alexnet(22x227x3)
allImages=imageDatastore('ImageDataBase','IncludeSubfolders',true, 'LabelSource','foldernames');
%Splitting up the database to training and validationset in the ratio 7:3.
[imdsTrain,imdsValidation] = splitEachLabel(allImages,0.7,'randomized');
pixelRange = [-30 30];
%Creating an augemnted image database
imageAugmenter = imageDataAugmenter('RandXReflection',true,'RandXTranslation',pixelRange,'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain,'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
%Setting training options
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',4, ...
'InitialLearnRate',0.001, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
%Training the network using trainNetwork and passing the parameters.
detectorNet=trainNetwork(augimdsTrain,layers,options);
save detectorNet

Training progress and accuracy graph

TrainingProgress

Running and testing the Face Recognition Program

After our network has been trained, we can finally run and test it on our smartphones to recognize faces using the camera.

Reference Images for each class(face)

Reference images
clc;close;clear;
m=mobiledev;
cam=camera(m);
load detectorNet;
faceDetector=vision.CascadeObjectDetector;
while true
pic=cam.snapshot('manual');
bbox =faceDetector(pic);
if(sum(bbox)~=0) %If sum(bbox) is not zero , face is present
img=imcrop(pic,bbox(1,:));
img=imresize(img,[227 227]);%resizing to alexnet inputsize.
name=classify(detectorNet,img);
image(pic);
title(char(name));
drawnow;
else
image(pic);
title('No Face Detected');
end
end

Test results and faces recognized

Recognized Faces

The program recognized every face it was tasked with correctly. We can infer from the above result that our program had an accuracy of 100%. This is mainly because we have a tiny number of classes (4) to identify. In real-world scenarios involving a much larger number of classes(faces) to recognize, a 100% accuracy is rarely achieved.

Conclusion

There is scope improvement in terms of recognizing multiple faces in a single frame. Face recognition algorithms are improving rapidly due to advancements in the field of artificial intelligence. We can deploy our program to make attendance systems in schools and colleges, intelligent surveillance cameras, etc.

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Prithvi

Engineering student with a keen interest to learn and implement new things. Currently working as a MATLAB developer at MATLAB helper.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Connect with MATLAB Helper ®

Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

Use Website Chat or WhatsApp at +91-8104622179

Watch our Latest Video

Join a Training Module

MATLAB Helper Training
>