.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

Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

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

    >