Tuesday, February 9, 2021

Face Detection & Face Tracking in MATLAB Full Code Free

Using Our Own Face images and Creating Our own Face dataset by Labelling in Image labeler App and Training our Object Detector to Detect Faces on Image & Video.


OUTPUT

Face detection on Image :-

 FACE TRACKING VIDEO Example 

  

DETAIL   VIDEO        Subscribe






Face detected 


                                     

 Side Face

Full video on Face detection On Image in detail :-

 



Steps :-


1.Face images of our own with different Background,Side Face,Frontal Face and Also you can Download Face Images from the Internet but make sure they look Original not much Processed.

You can also Download FACE images for Free :-

 
2. Labelling in Image labeler App.
Labelling means:- Drawing Rectangle Bounding Box around Face.

3.Exporting Labeled Images 
4.Training ACF object Detector to Certain Stages.
5.Testing our AI on Unlabelled Images.


Step 1:- Collect Sample Face images either Face dataset available on internet or Of your Own Images with different Backrounds and Light Conditions also.
Around 100  Faces images to max 200-300 are more than Enough for  Training Our Detector.

Warning:- Dont Use Images with same Background and Filtered Images from Insta or any such photo edited app.


Step 2:-  Open MATLAB 
Open Image labeler App by Clicking on APP icon and searching for image labeler.


Image labeler app  is available on Latest Versions of  MATLAB like 2017,2018,2019,2020 MATLAB Versions.



By Drawing Rectangle box i.e Boundary or Bounded Box on Face region of Image.

Now, After Labeling ,The AI extracts Features from the Bounding Box like Shape of Nose,Eyes,Mouth & Skin color.

We also need to supply Negative Images i.e images not having face,
so AI can learn to find Difference what a FACE is and What features face doesnt have.

But,  We are using ACF Object detector which doesnt Requires Negative Images, Only Positive Image with Labeling Face region are enough.
ACF Detector Creates its own Negative examples to Train.

Step 3:- Export Labeled images and
Load it in MATLAB, By Writing Command.

load('face.mat');

Step 4 :- Make Training Data folder ,
use Command to Train ACF DETECTOR..

CODE is Given in Video and also below .

ACF detector requires  10-20 Stages and it takes around 
10-20 minutes .

Once Detector is detected ,u can use it to detect faces in images as well as In Video.

CODING:-


Copy Code for Face detection on Image

Bold Letter is Coding Part.

% Face Detection Steps :-
% 1.Image labelling
% 2.Training
% 3.Testing

load('Facedata.mat');  

%  1. Load labelled data file ,which created through image labelling

Facedetect = selectLabels(gTruth,'Face');

% 2. Creating Variable Facedetect in which will store labels 'Face'

if isfolder(fullfile('TrainingData'))    
cd TrainingData
else 
mkdir TrainingData
end
addpath('TrainingData');

% 3. if else condition, if means 'if full name TrainingData exist ,locate that file ' else here if TrainingData Files doesnt exits Make one and 
%        add it to the MATLAB Path

trainingData = objectDetectorTrainingData(Facedetect,'SamplingFactor',1,'writeLocation','TrainingData');

% Make variable trainingData in which will store and passing Parameters like Facedetect that is labels , Sampling factor means Examples Face images
% if sampling factor is 2 than 2times negative images taken, Writing location = as TrainingData Folder

detector = trainACFObjectDetector(trainingData,'NumStages',20);

% detector is variable storing data of ACF Object Detector Neural network , Numstages= Number of Training stages,
% More stages like 10,20 takes long time to train but with higher %Accuracy

save('Detector.mat','detector');

%  saving Detector file , so once ACF detector trained ,it can be used to detect Faces 

rmpath('TrainingData');

% Saving detector file in TrainingData Folder  
% Upto this 13 lines of Code , It needs to run Only Once .
% once we have save Our Neural Network 'Detector.mat' file which detects faces . one Have saved in TrainingData folder ,
% So to use it whenever we just need to load it by specfying  its path 


% Now u can delete Above codes,above 13 line is only one time needed to run.


This final Code.

load('Detector.mat');

% Load Detector file , it is Pretrained Neural network for face detection

img = imread('‪C:\Users\ANKIT TIWARI\Desktop\Face detection UTUBE\anu.jpg');

% img is Variable , imread is function for reading Image.

[bboxes,scores] = detect(detector,img);

% bboxes = Bounding Boxes which surrounds Face -Rectangle Box
% Scores = Confidence that is how sure a Detector is for identifying Human Face

for i = 1:length(scores)
For loop when i=1:Scores,
annotation = sprintf('Confidence = %.1f',scores(i));

%annotation is labels like face and Confidence in percentage

img = insertObjectAnnotation(img,'rectangle',bboxes(i,:),annotation);
end
figure
imshow(img);


Face  Tracking CODE:- 

LINK :-



 load('Facedata.mat');

 %  1. Load labelled data file ,which created through image labelling

Facedetect = selectLabels(gTruth,'Face');

% 2. Creating Variable FaceGtruth in which will store labels 'Face'

if isfolder(fullfile('TrainingData'))
cd TrainingData
else 
mkdir TrainingData
end
addpath('TrainingData');

% 3. if else condition, if means 'if full name TrainingData exist ,locate that file ' else here if TrainingData Files doesnt exits Make one and 
%        add it to the MATLAB Path

trainingData = objectDetectorTrainingData(Facedetect,'SamplingFactor',1,'writeLocation','TrainingData');
detector = trainACFObjectDetector(trainingData,'NumStages',20);
save('Detector.mat','detector');

rmpath('TrainingData');

% Make variable trainingData in which will store and passing Parameters like FaceGtruth that is labels , Sampling factor means Examples Face images
% if sampling factor is 2 than 2times negative images taken, Writing location = as TrainingData Folder


load('Detector.mat');

% load Trained Object Detector

vidReader = VideoReader('C:\Users\ANKIT TIWARI\Desktop\Face detection UTUBE\face Rawdata\MEDface.mp4');

% VideoReader is for reading Video

vidPlayer = vision.DeployableVideoPlayer;

% Play Video in MATLAB
i = 1;

results = struct('Boxes',[],'Scores',[]);

% results is variable for storing bounding box i.e rectangle values

while(hasFrame(vidReader))
% While loop  
I = readFrame(vidReader);
[bboxes, scores] = detect(detector,I,'Threshold',1);
[~,idx] = max(scores);
results(i).Boxes = bboxes;
results(i).Scores = scores;
annotation = sprintf('% , Confidence %4.2f' ,detector.ModelName,scores(idx));
I = insertObjectAnnotation(I,'rectangle',bboxes(idx,:),annotation);
step(vidPlayer,I);
i =i+1;
end
results = struct2table(results);


Other Articles :-

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello. I'm Hajra and I saw your Youtube video based on this project which I found very relatable because I am working on something that regards Image labeller app in Matlab, for which I need a little help. I have some questions regarding the app and with some proceedings. I look forward to your reply to that we can discuss further.

    ReplyDelete

Face Detection & Face Tracking in MATLAB Full Code Free

Using Our Own Face images and Creating Our own Face dataset by Labelling in Image labeler App and Training our Object Detector to Detect Fac...

My Blogs