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.
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
%Creating a mobiledev object to obtain data from sensors
%the sensor we use is the camera
%UsingViola-Jones algorithm to detect objects(people's faces).
c=50; %50 images for each individual in database
temp=0;%Count of number of snapshots currently captured.
%Loop for capturing the images
pic=cam.snapshot('manual');%Taking an RGB photo and storing it in 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
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
Sample image in the database
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
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.
%Loading Alexnet network and storing it in net
net = alexnet
%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.
inputSize = layers(1).InputSize % Input size of Alexnet(22x227x3)
%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', ...
%Training the network using trainNetwork and passing the parameters.
Training progress and accuracy graph
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)
if(sum(bbox)~=0) %If sum(bbox) is not zero , face is present
img=imresize(img,[227 227]);%resizing to alexnet inputsize.
title('No Face Detected');
Test results and faces recognized
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.
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.
Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy
Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].
And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.
Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!
Education is our future. MATLAB is our feature. Happy MATLABing!