File Exchange

image thumbnail

Detect and Track Multiple Faces

version (2.9 KB) by Dima Lisin
Detect and track multiple faces from live video.


Updated 01 Sep 2016

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This is a simple system for detecting and tracking multiple faces from live video. It uses the Computer Vision System Toolbox and the Webcam Support Package. The system detects faces using the Viola-Jones algorithm, detects min-eigen corners within each face's bounding box, and tracks the corners using the Kanade-Lucas-Tomasi (KLT) algorithm. It re-detects the faces every 10 frames in order to correct the tracker and replenish the points.

Cite As

Dima Lisin (2021). Detect and Track Multiple Faces (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (77)

Anahí Serrano


Syahirah Nasir

it's working, but it will stop if it's no face. not good for surveillance

Ivan Gromyko

Ansam alzubaidy

hi ... how can i use computer vision toolbox ,thnx

Zahra Moayed

Luis Gualpa

Xu Wang

seokjin go

Eric Staker

selvia pratiwi

hai, this function is an error when there is no face
Error in detectAndTrackFaces (line 58)
displayFrame = insertObjectAnnotation (framergb, 'rectangle', ...
how to fix it?

omar elharrouss

Dear Dima Lisin
based on which article references that you are used to developed this algorithm?
best regards,



Gereziher W. Adhane

frans Thabo

wekel sigab

tao huang

That's cool!

Xyful Haqeem

Hi Dima..i had these errors when run the code;

Attempt to reference field of non-structure array.

Error in MultiObjectTracker/addDetections (line 109)
if this.PointTracker.isLocked();

Error in MultiObjectTracker2 (line 29)
tracker.addDetections(frame, bboxes);

How to overcome this errors? I hope you can reply asap, thanks :)

Gmal Ahmed

please, i need to modify pointTracker.m
I have error writing pointTracker.m

sanjeewa darshana

Kh. Islam

Charul Singh

Hi, I have tried for multiple people detection in a video but the problem I'm facing while detection is that the bounding box is not making for a specific number of people and also it continues to make bounding box again and again for the same number of people.could you please provide the steps where the loop should stop.

Nesara Lingaraj

Undefined function 'thingSpeakRead' for input arguments of type

Error in main_modv3 (line 16)

md khan

It breaks when there is no face in the frame.

Error using insertObjectAnnotation
Expected LABEL to be nonempty.

Error in detectAndTrackFaces (line 58)
displayFrame = insertObjectAnnotation(framergb, 'rectangle',...


Shuvam Mallick

it is giving an error in the line

tracker = MultiObjectTrackerKLT;

guangming xu

if there is no object,it will break.


Chirag Shekar


Hi,if I want to count how many faces in the video I had detected,what should I do?Could you provide me some good ideas?

tantri indrabulan

Monika Aggarwal

Hi Dima, please write the steps to be followed to run this program...


how to make the program play the video before detect the face?
(since this code only display the sequence after detect the face).
Any idea?

Adithya Chakilam

How can i estimate the change in position eye by using this code?

Liang Seacom


How to input images sequence?


Dear sir,
Can the class file(MultiObjectTrackerKLT) be put in a matlab directory so that the script can be executed without keeping the both in same directory?
Or, is it possible to set the path for the class in the script?
Thanks & regards


Wonderful work is done Sir. Thanks a lot.

Dima Lisin

Hi Chaitanya,

You have to install the Webcam Support Package for this to work:

Chaitanya P

Undefined function or variable 'webcam'.

Error in detectAndTrackFaces (line 10)
vidObj = webcam;

Can u tell me whats wrong??
I am from computer science background..


Hello Sir,
Thanks for uploading the codes.
After running the program, I am getting the below error:
Error using
Time out occured while trying to get a frame from the webcam.

Error in webcam/snapshot (line 227)
[image, timestamp] = obj.CamController.getCurrentFrame();

Error in face_prac (line 16)
frame = snapshot(vidObj);

Can u please help me to debug this.
Thanks in advance.

Dima Lisin

Hi Osumanu,

Of course, there is. You would have to modify the code to crop each bounding box using imcrop(), and save it using imwrite().

Osumanu Ra-is

please is there a way to detect and track the face and save it into a folder for a trainning images

Dima Lisin

Hi Arun,

I am not sure what you mean. What you download is a .zip file. You then have to extract the .m files from it. On Windows, you should be able to do that from the Windows Explorer. On Linux, you can use the unzip command on the command prompt.

Alternatively, you can simply click on the links to the .m files on the right, and save them to your local machine one by one.

Arun Krishna

can somebode please tell me how to get the code from this file ? it seems encrypted

zhu jiagang

Thanks lot. It is very useful.


very usefull work, thank so much...

Piyali Mukherjee

Hi! Thank you so much for this amazing file. Would there be any additional documentation or code on how to train your algorithm further? That is, would it be possible for us to cross-validate all the frames that are rendered to see how many of them belong to the same face?

yk s

jialin lu

bhaskar appu

Dima Lisin

Hi Ajay,
It sounds like you may be running an older version of MATLAB. This example requires R2014a or later, and it also requires the Computer Vision System Toolbox.

Dima Lisin

Hi Mudassar,
It sounds like the directory containing the files needed for this example is not on your path. The easiest thing is to change your working directory to the one where you have downloaded the files.

Dima Lisin

Hi Mitesh,

To use webcam you have to install the Webcam Support Package, which is a type of plugin. The error message should contain a link, which will take you to the Support Package Installer.

ajay mankhair

hii this code not running at all in my system showing some line 100 annotation error what should i do


i am running but there is error saying
"Undefined function or variable 'MultiObjectTrackerKLT'."
Please help me


i am try to run, but getting some ERROR >

Error using webcam (line 13)
MATLAB Support Package for Webcams has not been installed. Open Support Package Installer to install the Webcam
Support Package.

i am use MATLAB 2014a

Dima Lisin

@Carnie, all you have to do is use vision.VideoFileReader to read the video one frame at a time. The step() method of vision.VideoReader would replace the calls to snapshot().


Hello, can anyone tell me what modifications should we make to this code to make it work on a video rather than webcam?

zeng changqi

@Dima Lisin,@Ben,
I'm not proficient at the MATLAB, and don't know how to add the code to turned off the camera. Could you help?


Dima Lisin

@Ben, cool! Thanks for the tip.


@Dima Lisin,

I found a way to handle this by using the onCleanup function as below:

%% Setup a clean up function to deal with Ctrl-C
fnCleanup = onCleanup(@() cleanup(videoPlayer, vidObj));

And then simply write a function like this:

function cleanup(videoPlayer, vidObj)
%% Clean up
if ~isempty(videoPlayer)

if ~isempty(vidObj)
clear vidObj;

This works fine on my Mac.

Dima Lisin

Hi Ben. Thank you for trying it out! Unfortunately Ctrl-C cannot turn off the camera. To turn it off, you would have to clear vidObj. As of R2014b, you can use the isOpen() method of vision.VideoPlayer to exit gracefully when the player window is closed.


One minor issue: the camera was not turned off after pressed "Ctrl+C" to exit.


Thanks a lot for sharing! It ran very well on my first trial.

Alexander Sierra

Dima Lisin

Hi Lee. Unfortunately that will not work. The code uses features from R2014a. The key component here is vision.PointTracker, which was introduced in R2012b. So, with some work, you can probably make it run on R2012b or later.

Lee Dan

How to do this code run on matlab r2007b version ?

Dima Lisin

Unfortunately, this code will not work in 2012a. With some tweaking you can probably make it work in 2012b, which is when vision.PointTracker was added.

Hi, I am kind of started for video analytic. Actually I am unable to run the code. Can i run this code on MATLAB 2012a?

Dima Lisin

@txrxmo, detectAndTrackFaces.m is a script, which you can run from the command prompt. It uses vision.VideoPlayer to display the video.


Hi. This system can be used for a matlab GUI editor? Or its for command window?

vinothkumar r


Nicole corriel

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!