Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
295 views

Face Detection and Tracking Using Live Video Acquisition - MATLAB & Simulink Example - MathWorks India

This document describes a MATLAB example that detects and tracks a face in a live video stream from a webcam. It uses a cascade object detector to detect the face, then tracks facial feature points using the KLT algorithm. If the number of tracked points falls below a threshold, it switches back to detection mode to try reacquiring the face. The example loops through video frames, alternating between detection and tracking modes, and displays the annotated frames and tracked bounding box.

Uploaded by

rppvch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
295 views

Face Detection and Tracking Using Live Video Acquisition - MATLAB & Simulink Example - MathWorks India

This document describes a MATLAB example that detects and tracks a face in a live video stream from a webcam. It uses a cascade object detector to detect the face, then tracks facial feature points using the KLT algorithm. If the number of tracked points falls below a threshold, it switches back to detection mode to try reacquiring the face. The example loops through video frames, alternating between detection and tracking modes, and displays the annotated frames and tracked bounding box.

Uploaded by

rppvch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/1/2015

FaceDetectionandTrackingUsingLiveVideoAcquisitionMATLAB&SimulinkExampleMathWorksIndia

FaceDetectionandTrackingUsingLiveVideo
Acquisition
Thisexampleshowshowtoautomaticallydetectandtrackafaceinalivevideostream,usingthe
KLTalgorithm.

OpenthisExample

Overview
Objectdetectionandtrackingareimportantinmanycomputervisionapplicationsincludingactivityrecognition,automotive
safety,andsurveillance.Inthisexampleyouwilldevelopasimplesystemfortrackingasinglefaceinalivevideostream
capturedbyawebcam.MATLABprovideswebcamsupportthroughaHardwareSupportPackage,whichyouwillneedto
downloadandinstallinordertorunthisexample.ThesupportpackageisavailableviatheSupportPackageInstaller.
Thefacetrackingsysteminthisexamplecanbeinoneoftwomodes:detectionortracking.Inthedetectionmodetheyou
canuseavision.CascadeObjectDetectorobjecttodetectafaceinthecurrentframe.Ifafaceisdetected,thenyoumust
detectcornerpointsontheface,initializeavision.PointTrackerobject,andthenswitchtothetrackingmode.
Inthetrackingmode,youmusttrackthepointsusingthepointtracker.Asyoutrackthepoints,someofthemwillbelost
becauseofocclusion.Ifthenumberofpointsbeingtrackedfallsbelowathreshold,thatmeansthatthefaceisnolonger
beingtracked.Youmustthenswitchbacktothedetectionmodetotrytoreacquiretheface.
Setup
Createobjectsfordetectingfaces,trackingpoints,acquiringanddisplayingvideoframes.
%Createthefacedetectorobject.
faceDetector=vision.CascadeObjectDetector();
%Createthepointtrackerobject.
pointTracker=vision.PointTracker('MaxBidirectionalError',2);
%Createthewebcamobject.
cam=webcam();
%Captureoneframetogetitssize.
videoFrame=snapshot(cam);
frameSize=size(videoFrame);
%Createthevideoplayerobject.
videoPlayer=vision.VideoPlayer('Position',[100100[frameSize(2),frameSize(1)]+30]);
DetectionandTracking
Captureandprocessvideoframesfromthewebcaminalooptodetectandtrackaface.Theloopwillrunfor400framesor
untilthevideoplayerwindowisclosed.
runLoop=true;
numPts=0;
frameCount=0;
whilerunLoop&&frameCount<400
%Getthenextframe.
videoFrame=snapshot(cam);
http://in.mathworks.com/help/vision/examples/facedetectionandtrackingusinglivevideoacquisition.html

1/5

8/1/2015

FaceDetectionandTrackingUsingLiveVideoAcquisitionMATLAB&SimulinkExampleMathWorksIndia

videoFrameGray=rgb2gray(videoFrame);
frameCount=frameCount+1;
ifnumPts<10
%Detectionmode.
bbox=faceDetector.step(videoFrameGray);
if~isempty(bbox)
%Findcornerpointsinsidethedetectedregion.
points=detectMinEigenFeatures(videoFrameGray,'ROI',bbox(1,:));
%Reinitializethepointtracker.
xyPoints=points.Location;
numPts=size(xyPoints,1);
release(pointTracker);
initialize(pointTracker,xyPoints,videoFrameGray);
%Saveacopyofthepoints.
oldPoints=xyPoints;
%Converttherectanglerepresentedas[x,y,w,h]intoan
%Mby2matrixof[x,y]coordinatesofthefourcorners.This
%isneededtobeabletotransformtheboundingboxtodisplay
%theorientationoftheface.
bboxPoints=bbox2points(bbox(1,:));
%Converttheboxcornersintothe[x1y1x2y2x3y3x4y4]
%formatrequiredbyinsertShape.
bboxPolygon=reshape(bboxPoints',1,[]);
%Displayaboundingboxaroundthedetectedface.
videoFrame=insertShape(videoFrame,'Polygon',bboxPolygon,'LineWidth',3);
%Displaydetectedcorners.
videoFrame=insertMarker(videoFrame,xyPoints,'+','Color','white');
end
else
%Trackingmode.
[xyPoints,isFound]=step(pointTracker,videoFrameGray);
visiblePoints=xyPoints(isFound,:);
oldInliers=oldPoints(isFound,:);
numPts=size(visiblePoints,1);
ifnumPts>=10
%Estimatethegeometrictransformationbetweentheoldpoints
%andthenewpoints.
[xform,oldInliers,visiblePoints]=estimateGeometricTransform(...
oldInliers,visiblePoints,'similarity','MaxDistance',4);
%Applythetransformationtotheboundingbox.
bboxPoints=transformPointsForward(xform,bboxPoints);
http://in.mathworks.com/help/vision/examples/facedetectionandtrackingusinglivevideoacquisition.html

2/5

8/1/2015

FaceDetectionandTrackingUsingLiveVideoAcquisitionMATLAB&SimulinkExampleMathWorksIndia

%Converttheboxcornersintothe[x1y1x2y2x3y3x4y4]
%formatrequiredbyinsertShape.
bboxPolygon=reshape(bboxPoints',1,[]);
%Displayaboundingboxaroundthefacebeingtracked.
videoFrame=insertShape(videoFrame,'Polygon',bboxPolygon,'LineWidth',3);
%Displaytrackedpoints.
videoFrame=insertMarker(videoFrame,visiblePoints,'+','Color','white');
%Resetthepoints.
oldPoints=visiblePoints;
setPoints(pointTracker,oldPoints);
end
end
%Displaytheannotatedvideoframeusingthevideoplayerobject.
step(videoPlayer,videoFrame);
%Checkwhetherthevideoplayerwindowhasbeenclosed.
runLoop=isOpen(videoPlayer);
end
%Cleanup.
clearcam;
release(videoPlayer);
release(pointTracker);
release(faceDetector);

http://in.mathworks.com/help/vision/examples/facedetectionandtrackingusinglivevideoacquisition.html

3/5

8/1/2015

FaceDetectionandTrackingUsingLiveVideoAcquisitionMATLAB&SimulinkExampleMathWorksIndia

References
Viola,PaulA.andJones,MichaelJ."RapidObjectDetectionusingaBoostedCascadeofSimpleFeatures",IEEECVPR,
2001.
BruceD.LucasandTakeoKanade.AnIterativeImageRegistrationTechniquewithanApplicationtoStereoVision.
InternationalJointConferenceonArtificialIntelligence,1981.
CarloTomasiandTakeoKanade.DetectionandTrackingofPointFeatures.CarnegieMellonUniversityTechnicalReport
CMUCS91132,1991.
JianboShiandCarloTomasi.GoodFeaturestoTrack.IEEEConferenceonComputerVisionandPatternRecognition,
1994.
ZdenekKalal,KrystianMikolajczykandJiriMatas.ForwardBackwardError:AutomaticDetectionofTrackingFailures.
InternationalConferenceonPatternRecognition,2010

http://in.mathworks.com/help/vision/examples/facedetectionandtrackingusinglivevideoacquisition.html

4/5

8/1/2015

FaceDetectionandTrackingUsingLiveVideoAcquisitionMATLAB&SimulinkExampleMathWorksIndia

http://in.mathworks.com/help/vision/examples/facedetectionandtrackingusinglivevideoacquisition.html

5/5

You might also like