.st0{fill:#FFFFFF;}

Face Detection Counter with MATLAB 

 January 6, 2019

By  Aditya Chaudhari

Image Processing has enormous applications to support our life. One of the great use of Image Processing is recognizing the number of people from any frame. Do you want to build your personal face detector using MATLAB for your academic project or Do you want to amaze your friends and family with a functioning GUI Application? If yes, continue reading the blog.

Coding Part

Initialization

clc; clear;close all;
vidObj = VideoReader('GOT.mp4'); % Use your mp4 file
vidObj.CurrentTime = 4; % Capturing Live Feed from Camera
faceDetector = vision.CascadeObjectDetector(); % Finds faces by default
tracker = MultiObjectTrackerKLT;

Video Player & Iteration until we have successfully detected a face

videoPlayer  = vision.DeployableVideoPlayer;
bboxes = [];
while isempty(bboxes)
    framergb = readFrame(vidObj);
    frame = rgb2gray(framergb);
    bboxes = faceDetector.step(frame);
    videoPlayer.step(framergb);
end
 
tracker.addDetections(frame, bboxes);
% And loop until the player is closed
frameNumber = 0;
keepRunning = true;

Main Loop to Track and Display

while keepRunning
    framergb = readFrame(vidObj);
    frame = rgb2gray(framergb);
    if mod(frameNumber, 10) == 0
        %(Re)detect faces.
        % NOTE: face detection is more expensive than imresize; we can
        % speed up the implementation by reacquiring faces using a
        % downsampled frame:
        bboxes = 2 * faceDetector.step(imresize(frame, 0.5));
        if ~isempty(bboxes)
            tracker.addDetections(frame, bboxes);
        end
    else
        % Track faces
        tracker.track(frame);
    end
    % Display bounding boxes and tracked points.
    if ~isempty(tracker.BoxIds)
        displayFrame = insertObjectAnnotation(framergb, 'rectangle',...
            tracker.Bboxes, 'Detected');
        displayFrame = insertMarker(displayFrame, tracker.Points);
        count = length(tracker.BoxIds);
        position=[1,1];
        value= sprintf('%d People in the Frame',count);
        displayFrame= insertText(displayFrame,position,value);
        videoPlayer.step(displayFrame);
 
        frameNumber = frameNumber + 1;
        keepRunning = isOpen(videoPlayer);
    else
        bboxes = [];
        while isempty(bboxes)
            framergb = readFrame(vidObj);
            frame = rgb2gray(framergb);
            bboxes = faceDetector.step(frame);
            videoPlayer.step(framergb);
        end
        tracker.addDetections(frame, bboxes);
        %And loop until the player is closed
        frameNumber = frameNumber + 1;
        keepRunning = true;
    end
end

Clean up Video Player

release(videoPlayer);
faceDetector = vision.CascadeObjectDetector();
pedestrianDetector = vision.PeopleDetector;
clear vidObj;

Results

FaceDetectBlog

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Aditya Chaudhari

  • How to upload video file in matlab. I am doing project on face recognition and face count ..

    • Aditya Chaudhari says:

      Hi Anurag, you can use the inbuilt function ‘VideoReader’, syntax as below:

      vidObj = VideoReader(‘filename.mp4’);

      You can add the video to your current directory by simply dragging dropping the video into it. I hope that clears your doubt. If not you can comment or directly get in touch with our team at ​[email protected]

  • I’m facing a challenge here with the code. when I execute it tells me this: Undefined function or variable ‘MultiObjectTrackerKLT’.

    Error in EntireCode (line 5)
    tracker = MultiObjectTrackerKLT;

    • Aditya Chaudhari says:

      Hi Jayson, you will have to download the resources from above or use the link: https://gitlab.com/matlabhelperteam/wolfie/-/tree/master/Face%20Detection%20Counter%20Blog

      Here you can find a separate file ‘MultiObjectTrackerKLT.m’ which contains the function. By putting the file in the same working directory, you should be able to execute the code as mentioned in the blog and video.

      Hope you are able to get the desired output! Please feel free to drop another comment, if you face any difficulties.

    • Abhishek mishra says:

      I need some help regarding my project. If u could then pls contact me whatsapp 9453955033

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >