Face Counter Using MATLAB

Vivek Panchabhaiya. The author is a B.Tech in electronics and communication from SRCEM, Gwalior


This face counter using MATLAB program helps count the number of persons present in a meeting hall or classroom at a time. The program logic can also be used in other applications, such as automatic temperature control which depends on the number of persons in a room. Video camera plays a very important role in this project.

If you try to count a large number of people in a hall, you may take time to count them and probably also make a mistake. With the help of this program, you can get the number of people instantly and more accurately using a camera and image processing.

Fig. 1: Face-counter program output
Fig. 1: Face-counter program output
Fig. 2: Face counter using MATLAB
Fig. 2: Face counter using MATLAB

Software program

Fig. 3: Checking the device ID

A graphic user interface (GUI) allows users to perform tasks interactively through such controls as switches and sliders. You can create GUI and run it in MAT LAB or as standalone application. The initial program output of this project is shown in Fig. 1 and its output after face detection is shown in Fig. 2.

Face detection algorithm

There are different types of algorithms used in face detection. Here, we used Viola-Jones algorithm for face detection with MATLAB program.

The algorithm actually works in following steps:
1. Creates a detector object using Viola-Jones algorithm
2. Takes the image from the video
3. Detects the features
4. Annotates the detected features

MATLAB functions

The program (testing.m) has many functions. Do not edit the functions as these are linkers and non-executable codes. First of all, you have to find the format supported by the camera and its device ID using the command given below (also shown in Fig. 3):

info = imaqhwinfo(‘winvideo’)

After knowing the device ID, you can change the device ID number in your source code.

We are having device ID as {1}, so have written ‘1’ in the code, as mentioned below:

vid = videoinput(‘winvideo’,1,’YUY2

We also have other formats in MATLAB. You can check which format your camera supports by using commands below (also shown in Fig. 4):


In Fig. 4, you can see that format ‘YUY2_160x120’ is the one which is supported by the camera by default. But there are also other formats (resolutions) which your camera can support, as shown in last line in this screenshot. If you select another format and device number, you should make changes in the source code accordingly.

Fig. 4: Formats supported by camera

To detect a face or a particular feature on the faces of people, following steps are used in MATLAB program (testing.m):

1. Define and set up your cascade object detector using the constructor.


It creates a system object detector that detects objects using the Viola-Jones algorithm. Its Classification Model property controls the type of object to detect. By default, the detector is configured to detect faces.

2. Call the step method with the input image I, the cascade object detector object, detector, points PTS and any optional properties. See the syntax below for using the step method. Use the step syntax with input image I, the selected cascade object detector object, and any optional properties to perform detection.

BBOX = step (detector, I)

It returns BBOX, an M-by-4 matrix defining M bounding boxes containing the detected objects. This method performs multi-scale object detection on the input image I. Each row of the output matrix BBOX contains a four-element vector (x, y, width and height) that specifies in pixels, the upper-left corner and size of a bounding box. Input image I must be a grayscale or true colour (RGB) image.

3. insertObjectAnnotation(I,’rectangle’,Position,Label)

It inserts rectangles and corresponding labels at the location indicated by the position matrix. The position input must be an M-by-4 matrix, where each row (M) specifies a rectangle as a four-element vector (x, y, width and height). The elements x and y indicate the upper-left corner of the rectangle, and the width and height specify the size.


To test this program, follow the steps below:
1. Install MATLAB R2012a or higher version in your system. Launch it from desktop shortcut. You will see a blank command window.
2. Download the source folder given below.
3. Check the device ID, as shown in Fig. 3, and write that device ID number in the source code.
4. Run the program (count.m). A graphic user interface will appear, as shown in Fig. 1.
5. Click on Start Camera button to initialise camera settings.
6. Next click on Count Face button and the camera will start counting the faces.
7. To stop, click Stop button.

Download source folder: click here


Enter your email address:

Delivered by FeedBurner


  1. please send me ppt of this project.
    and also code because it is not working. It take an issue after running program.
    can u send a videos also with running program and also one ppt on this project.

  2. hello sir i am ravi kumar sir mene face counter project par work kiya use matlab me code kiya but error aa rha hia me error ko yaha copy kar deta hu aap check kar please mujhe solution bta de
    >> count
    Maximum recursion limit of 500 reached. Use set(0,’RecursionLimit’,N)
    to change the limit. Be aware that exceeding your available stack
    space can
    crash MATLAB and/or your computer.

    Error in count

  3. i am not getting my device id sir it is saying like this sir
    Error using imaqhwinfo (line 109)
    Invalid ADAPTOR specified. Type ‘imaqhwinfo’ for a list of installed ADAPTORs. Image acquisition adaptors may be
    available as downloadable support packages. Open Support Package Installer to install additional vendors.

    can you please help sir??

    • Seems your webcam is not supported. You need either an inbuilt-camera or an external webcam to test the project. If you dont have camera it wont work. After connecting external camera you need to note the camera device ID.

  4. can u send me source code of this program please [email protected]
    While running of i am getting following errors
    Error event occurred at 16:55:25 for video input object: YUY2_640x480-winvideo-1.
    GETDATA timed out before FRAMES were available.
    Error using imaqdevice/getdata (line 149)
    GETDATA timed out before FRAMES were available.

    Error in count>count_Callback (line 108)
    image = getdata(vid); %store that frame
    in ‘image’

    Error in gui_mainfcn (line 96)

    Error in count (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)count(‘count_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

  5. m getting the following error..

    ??? function varargout = count(varargin)
    Error: Function definitions are not permitted in this context

  6. Good day!

    This article is a big help as a reference for my project. In line with that, I’d like to talk with you through e-mail regarding face recognition and face tracking, if possible. This is my e-mail: [email protected]

    Looking forward to talk with you soon!

  7. Hello EFY Team,
    I am very impressed with your project. I had downloaded the source code and also run the program , but unfortunetly got many errors can please send me your source code my email id as soon as posssible. It will be very kind of you..
    [email protected] this is my email Id…

  8. info = imaqhwinfo(‘winvideo’)
    when i do this it’s showing like this errors

    Error using imaqhwinfo (line 109)
    Invalid ADAPTOR specified. Type ‘imaqhwinfo’ for a list of installed ADAPTORs.
    Image acquisition adaptors may be available as downloadable support packages.
    Open Support Package Installer to install additional vendors.

    can u help me??
    can u send the source code to my mail???
    my mail is—>[email protected]

    • Hi, the source code is available at end of the article.
      For the video, I have forwarded your request to the concerned person.

  9. Sir, first of all, thank you for sharing such a helpful project article. Sir, I’m working on this project and need a bit of help.
    I execute your given source code, after executing GUI gets starts but when I press the start camera button nothing happens. I can also share the video recording of what happening when I’m going through the process.
    Need your Assistance, because I’ve to submit this project.

  10. Sir need your help, I followed all the procedure but when I execute the program and after that Gui got open. But when I press the start camera button nothing comes up although I’ve selected the device Id ‘1’ following the testing procedure mentioned above.
    Sir need your help as soon as possible.

  11. hello, I download the source code but it’s appearing in weird characters that my Matlab doesn’t recognize can you send me the code plesase


Please enter your comment!
Please enter your name here