Sunteți pe pagina 1din 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

S-ar putea să vă placă și