# MATLAB Easy Face Detection

94

2

The main aim of this instructables is to show how much easy ,the image processing will be , With the help of MATLAB

Face detection and tracking has been an important and active research field ,so that's why i'm going to explain how it can be done with the Matlab.

In the following tutorial i'm going to do the below stuff:

1.detecting faces in a image and counting.

2.detecting human eyes in a image and counting.

3.detecting human mouth in a image and counting.

4.detecting faces in a Video and counting.

5.detecting human eyes in a Video and counting.

6.detecting human mouth in a Video and counting.

## Step 1: Detecting Faces in a Image and Counting.

MATLAB SCRIPT:

clear all % clear all objects
clc %clear screen

FDetect = vision.CascadeObjectDetector; %Detect objects using Viola-Jones Algorithm

BB = step(FDetect,image); %Returns Bounding Box values based on number of objects

figure,
imshow(I);

hold on

for i = 1:size(BB,1)

rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r'); %r -red, g-green,b-blue

end

title('Face Detection'); %title of the figure
hold off;

The result will be like the image which was attached in this step itself

To count the number of faces detected:

clear all % clear all objects
clc %clear screen

FDetect = vision.CascadeObjectDetector; %Detect objects using Viola-Jones Algorithm %Read the input image

BB = step(FDetect,image); %Returns Bounding Box values based on number of objects

figure,

imshow(I);

hold on

for i = 1:size(BB,1)

rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r'); %r -red, g-green,b-blue

end

text(10,10, strcat('\colour{red} No of faces =',num2str(length(BB)))); This line gives you the count

title('Face Detection'); %title of the figure

hold off;

## Step 2: Detecting Human Eyes in a Image and Counting.

MATLAB SCRIPT:

clear all;

clc;

%To detect Eyes

BB=step(EyeDetect,image);

figure,

imshow(image);

rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');

title('Eyes Detection');

The result will be like the image which was attached in this step itself

To count the number of eyes detected:

clear all;
clc; %To detect Eyes

BB=step(EyeDetect,image);
figure, imshow(image); rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');

text(10,10, strcat('\colour{red} No of eyes =',num2str(length(BB))));

title('Eyes Detection');

## Step 3: Detecting Human Mouth in a Image and Counting

MATLAB SCRIPT:

clear all;

clc;

%To detect Mouth

BB=step(MouthDetect,image);

figure, imshow(image);

hold on

for i = 1:size(BB,1)

rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');

end

title('Mouth Detection');

hold off;

The result will be like the image which was attached in this step itself

To count the number of Mouth detected:

clear all;
clc; %To detect Mouth

figure, imshow(image);

hold on

for i = 1:size(BB,1)

rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');

end

text(10,10, strcat('\colour{red} No of mouths =',num2str(length(BB))));

title('Mouth Detection');

hold off;

## Step 4: Detecting Faces ,eyes,mouth in a Video and Counting.

clear all;

close all;

clc;

% Capture the video frames using the video input function % You have to replace the resolution & your installed adaptor name.

%only use any one (face/eyes/mouth)

vid = videoinput('winvideo',1,'yuy2_320x240'); % Set the properties of the video object

set(vid, 'FramesPerTrigger', Inf);

set(vid, 'ReturnedColorspace', 'rgb') ;

vid.FrameGrabInterval = 5; %start the video acquisition here

start(vid) % Set a loop that stop after 100 frames of acquisition

while(vid.FramesAcquired<=200) % Get the snapshot of the current frame

data = getsnapshot(vid);

imshow(data);

b=step(a,data);

hold on

for i=1:size(b,1)

rectangle('position',b(i,:),'linewidth',2,'linestyle','-','EdgeColor','r');

end

hold off

text(10,10, strcat('\colour{green} No of faces =',num2str(length(b))));

end

stop(vid); % Stop the video acquisition

## Recommendations

• ### Internet of Things Class

20,854 Enrolled