CODE


 CODING With Explanation:-


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',5);
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);

No comments:

Post a Comment

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