Seeing With OpenCV
Transcription
Seeing With OpenCV
SeeingWithOpenCV FollowThatFace! . o do d e s c r i b e di n l a s t m o n t h ' sa r t i c l e T rithmin moredetail C a m s h i f tw a s o r i g i n a l l yd e v e l o p e d f o r h a n d s - f r egea m i n g .l t ' s d e s i g n e dt o "lightweight" so the be veryfast and computer can do other tasks while t r a c k i n g .S i n c ei t w a s d e v e l o p e da s a t l s oh a sa n g a m i n gi n t e r f a c eC , a m s h i fa ( l i m i t e d )a b i l i t y t o d e t e c t c h a n g e si n view, s h o u l d e ro, r t u r n st o w a r d s t h e f r o n t a lf a c ed e t e c t o rw i l l n o l o n g e r r i o t c r t i t c n v n r r ' dn e p dt o h a n d l et h a t s i t u a t i o na, s w e l l . Fortunately,OpenCV includes s p e c i a l i z e dc o d e f o r t r a c k i n g a f a c e e f f i c i e n t l y ,u s i n g c o n t i n u i t y b e t w e e n f r a m e st o h e l p f i n d t h e b e s t m a t c hf o r t h e f a c ei t ' sf o l l o w i n g . T h e a l g o r i t h mt h a t O p e n C Vu s e s for face tracking is called Camshift C a m s h i f tu s e s c o l o r i n f o r m a t i o n ,b u t r a t h e rt h a n r e l y i n go n a s i n g l ec o l o r ,i t t r a c k sa c o m b i n a t i o no f c o l o r s .S i n c ei t tracks by color, it can follow a face t h r o u g h o r i e n t a t i o nc h a n g e st h a t t h e . h es i d e b a r , H a a rd e t e c t o rc a n ' th a n d l eT h e a d p o s i t i o n s, u c ha s t i l t i n gt h e h e a d t o o n e s i d e .C o u l dy o u u s e t h a t a b i l i t y t o c o m m u n i c a t ew i t h y o u r r o b o t ? "Come M a y b et w o f a s t h e a dt i l t s m e a n h e r e ,r o b o t ! " F i g u r e 1 s h o w s O p e n C V ' sf a c e t r a c k e ri n a c t i o n- f o l l o w i n ga f a c e a s i t t i l t s t o o n e s i d ea n d d u r i n g a t u r n t o p r o fi l e . Lastmonth'sarticlein this t h a t , h o w e v e ry, o u ' d n e e dt o d e c i d ei f how to tt hh ee fsaacme yeofua dc ee .t el fc tt eh dei ndeeat ec hcftroarmf i nedi ss seriesexplained and configurem o r e t h a n o n e f a c e i n a f r a m e ,y o u ' d implement n e e d t o d e c i d ew h i c h d e t e c t i o ni s t h e Thismonth, o n e y o u ' r e t r a c k i n g . F i n a l l y ,i f a facedetection. l'll showyou howto use p e r s o n ' s h e a d t i l t s t o wp raorfdi lse o n e to trackataceonce OpenCV it. you'vedetected FaceTrackingin OpenCV T r a c k i n ga f a c e i s m o r e d r f f i c u l t t h a n t r a c k i n ga s t r o n g l y - c o l o r eodb j e c t . S k i nr e f l e c t st h e a m b i e n tl i g h ti n s u b t l e , c h a n g i n g w a y s a s a p e r s o n ' sh e a d t u r n so r t i l t s . I n p r i n c i p l ey, o u c o u l dt r a c ka f a c e b y l o c a t i n gi t o v e r a n d o v e r I n e v e r y frame, using the Haar detector Jv tvu TheCamshiftDemo T h e O p e n C V s a m p l e sd i r e c t o r Y c o n t a i n sa p r o g r a m c a l l e d c a m s h r f t d e m o .Y o u c a n g e t s o m eg o o d h a n d s a n d a n i n t u i t i v ef e e l f o r on experience the Camshifa t l g o r i t h mw i t h t h i s d e m o "How O p e n C V ' s p r o g r a m .H e r ea r e t h e s t e p sf o r d o i n g Works," t h a t : FaceTracker e x p l a i n st h i s a l g o 1 ) P l u gi n a w e b c a m . F I G U R E1 . O p e n C V ' s face tracker in action, 2 ) L a u n c ht h e p r o g r a mc a l l e dc a m s h i f t It's able to follow a d e m o i n t h e s a m p l e sd i r e c t o r y . face as it tilts to one s i d e a n d d u r i n ga t u r n ; t rectangle 3 ) U s ey o u r m o u s et o s e l e c a to profile. c e n t e r e dt i g h t l yo n y o u r f a c e . yindow 4 ) C l i c k i n t h e v i d e o - d i s p l aw and type the letter b. (The display s h o u l dc h a n g et o l o o k s o m e t h i n gl i k e t h e v i e w i n F i g u r e2 . ) 2. Totunethe Camshiftparameters FIGURE smin drtd rmin" run the camshiftdemoI programin the samplesdirectory.These parametersare easierto set if you toggle to the backprojectionview by clickingin the view window, then typing b, i l 36 srnvoo3.eoo7 t.t - : e n C V ' sf a c et r a c k e ru s e sa n a l g o r i t h mc a l l e dC a m s h i f t , FIGURE A. Two examplesof ' - ' : c o n s i s tos f f o u r s t e P s : th€ color histogram that Camshiftusesto represenla face, .z-:e a color histogramto representthe face. in OPenCV. , :- ate a "faceprobability"for each pixelin the incoming backprojection" built-in method Ihere's a '-':':: t h a t i m p l e m e n t si t , c a l l e d ^-.^^f ^n^^r,^l^^ .. .:_-_!!L^' - 'i the location of the face rectanglein eachvid e o f r a m e . c v ( d . c d o c K P r o l e c . Figure C shows the f a c e - p r o b a b i l i t yi m a g e i n - : : , l a t e t h e s i z ea n d a n g l e , o n e v i d e o f r a m ea s C a m s h i f t my face. Blackpixels tracks 'row 'l : each stePworks: have the lowest probabilitY white,the high.'eate a histogram.Camshift representsthe face it's v a l u g a n d pixels lie somewherein the middle. est. Gray ^ S ( a l s oc a l l e da b a r c h a r to) f c o l o rv a l u e s . -. a sa h i s t o g r a m 'a A shows two example histogramsproduced by the - =^ rft demo programthat shipswith OpenCV.The heightof 3) Shift to a new location. With each new video frame, "shifts"its estimateof the face location,keepingit -- colored bar indicateshow many pixels in an image Camshift "hue." Hue is one of three valuesdescribing centeredover the area with the highestconcentrationof : . ldve that , e s c o l o r i n t h e H S V( H u e ,S a t u r a t i o nV,a l u e c) o l o r m o d e l . bright pixels in the face-probabilityimage.lt finds this new ' -ore on color and color models, see "Ihe World of f,ra"utlon OVttarting at the previow to.u-tionand computing the cenler of gravityof the face-probabilityvalueswithin a Magazine,November'05,) :' SERVO - the imageregionrepresentedby the top histogram,a rectangle.lt then shifts the rectangleso it's right over the ^ h u e i s m o s tc o m m o n ,a n d a s l i g h t l ym o r e l a v e n d e h r u e center of gravity. lt does this a few times to center the a rectangle well. The OpenCV f unction cvcars rif - r ) shows histogram The bottom :-e next most common, location. :a in which the most common hue is the rightmostbin. implementsthe stepsfor shiftingto the new with to correspond rectangle process the of shifting This . rue is almos! but not quite, red, "Mean the center of gravityis based on an algorithmcalled h i f t , " b y D o r i n C o m a n i c i u ,I n f a c t , C a m s h i f ts t a n d s f o r -atculate face probability - simpler than it sounds! fhe S " C o n t i n u o u sA l yd a p t i v eM e a nS h i f t . " ::gram is created only once, at the start of tracking. "face-probability" value to :e'wards, it's used to assigna 4) Calcutate size and angte. The OpenCV method is that follow' pixel video frames the in -- rrrl6g€ "ContinuouslyAdaptive" and not just "Mean Shift" Faceprobability"soundsterribly complicatedand heav- called * athematical, but it's neither!Here'show it works.FigureB because it also adjusts the size and angle of the face - ^'s the barsfrom a histogramstackedone atop the other. rectangleeach time it shifts it, lt does this by selectingthe ':e' stacking t h e m ,i t ' sc l e a rt h a t t h € r i g h t m o sbt a r a c c o u n t s s c a l e a n d o r i e n t a t i o n F I G U R E C. The normal and face- :oout 457oof the pixels in the region.That meansthe that are the best fit to probabilityviewsas Camshift tracks :caoility that a pixel seleded randomlyfrom this region t h e f a c e - p r o b a b i l i t y m y f a c e . I n t h e f a c e - p r o b a b i l i t y "faceprob- pixels inside the n€w view, black pixelshave the lowest : - d fall into the rightmostbin is 45%.That'sthe '- i,' for a pixelwith this hue.The samereasoningindicates :: tf'e face probabilitYfor 'z rext histogram b i nt o t h e :-: ls about 2O7o,since it -:rutts for about 207oof stack's total -h€ight. - :: s all there isto it.. As new video frames ' .e, the hue value for ':. pixel is determined. '': r that,the facehistogram , ..;sed to assign a face : c a b i l i t yt o t h e p i x e l .T h i s "histogram :cess is called -:JRE B. To see what ..e probability" means/ t h e b a r si n :grre stacking : histogram on€ atoP '-e other. The probability . . ; o c i a t e dw i t h e a c hc o l o ri s ' z p e r c e n lt h a t c o l o r b a r : : rtributes to the total -:ght of this stack. r;."ii." iu.ii"si" il:#2i1,"f"'f,lf,fil;f.',? ;?];? % 3V SERVOO3.2OO1 1- //// Constants 2 const char * DISPLAY_WINDOUI= "Di-splayWindow"; 3 #def ine OPniCV_R@I "C: /Proqrarn Files/@enCv/1.0" A 5 //// cLoi:p.l variables * pvideoFramecopy = 0; 5 Ipllmage 1 B void main( inc argc, char** argTv ) g { 10 11 L2 13 L4 15 1 6 L] fB L9 CvRect. * pFaceRect = 0; if( linitAllO ) exitprogram(-1); video frames until a face // Capture and display // is detected while( I ) { / / Ir,ok for a face in the next video frame capEureVideoFrameO; pFaceRect = detectFace(pvideoFramecopy); 20 2I 22 23 24 25 26 21 28 29 30 31 32 33 3 4 35 36 17 ?A // Showthe display image cvSlor./Tfiage( DISPI,AY-WINffi,i. pVideoFrarreCoEl ) ; if( (char)27==cvlvaitKey(1)) exitprogram(0); loop when a face is detected // *it if(pFaceRect) break; ) // inLtLalize tracking startTracking(pVideoFrameCogl, pFaceRect); / / Track the detected face using Camshift while( I ) { CvBolD faceBox; / / dcr lhe next video ^ . n t s 1 , r 6 l / i A a ^ F r. a a m e ( l ; frame 39 40 / / track the face in the new video frame 4L faceBox = track(pVideoFrameCopy); 42 43 // outli-ne face ellipse 44 cvEllipseBox(pvideoFramecopEr,faceBox, 45 C V _ R G B ( 2 5 5 , 0 , 03) ., C V _ A A ,0 ) ; pVideoFrarpCoEf ); 46 cvShovulrnage(DISPIAY_WINDC&\I, 4'1 if( (char)27==cwlaitKq/(l) ) break; 4 8 ) 49 50 exitProgram(0); 51 ) 5) Adjustthe slidersfor sminand vmin untilthe ellipseis well positioned and the background is mostlyblack. 6) RepeatStep 4 to toggle back to normal view, then use Camshiftto trackyourface. TuningCamshift As mentioned above, Camshift usesa combination of colorsto track faces. In the representation that Camshiftuses,color is undefinedfor pixelsthat havea neutralshade(white, 38 sEnvoo3.9oo7 not onlymy face,but halfthe roomas well!Thereasonfor the oversized face detectionis clearlyvisiblein the faceprobabilityview. Backgroundpixels with a nearlyneutralshadecontributed too muchnoisewhen vrnindfld smin gray,or black).Color wereat theirdefaultvalues. The middle and right views in canbe computed for pixels that are Figure2 showthe effectof increasing almost neutral,but first snj-n,then rzmin.In the right-hand theircolorvaluesare view, noisy pixelshave been largely unstable,and these eliminated,but the face region still pixels contribute producesa strongsignal.Trackingis noisethat interferes now quitegood,and the ellipseis well with tracking. oositioned. Camshift uses two parameters smin ood vmin - to screenout thisnoise. These parameters OoenCVincludessourcecodefor camshiftdemo, definethresholds for but it's not easy to ignoringpixelsthat adapt, since it combinesuser-input are too close to handlersand view togglingwith the neutral.rrminsetsthe stepsfor facetracking. "almost for lf you're programmingin C++, threshold ratherthan in C, you could use the black,"and smi-nfor "almostgray." class,defined in These CvCamShiftTracker Again,however,this class two thresholdlevels cvaux.hpp. is fairly complex, with many will need to be to C++ interfaces, and is onlyavailable adjusted for your programmers. setup to get good results withCamshift. To make the Camshifttracker Camshift also moreaccessible, l'vewrittena wrapper usesa third parame- for it in C with four maininterfaces: ter called r,rmax,to 1) createTrackerO pre-allocates set a thresholdfor pixelsthat are too internaldatastructures. bright. But smin has the side effect 2) releaselracker ( ) releasesthese of also eliminating resources. pixelsthat are close 3) startrrackinsO initiatestracking to white, so you f rom an image plus a rectangular shouldn't need to tweak r,'maxto get region. FIGURE3. The main program listing for detectinga face in a live video stream,then tracking it using the Camshift wrapperAPl. good results. The easiestway to selectgood valuesfor your setupis with camshiftdemo.As suggested in the preceding section,it's easierto set theseif you toggle the viewingmode by clicking the view window and typingb. (This viewis the calledthe "facealternative "backprojection" probability," view. or It'sexplained in the sidebar.) Figure 2 shows the effect of Initially, in the adjusting sminondvrnj-n. firstframe,thesewereat theirdefault values.At these levels,Camshiftdisplayeda verylargeellipse that included Ihe SimpleCamshift Wrapper I J ' ) J J G aa a t : :s 4) tracko tracksthe object in this region f rom f rame to f rame using Camshift. r'6 !:lo r:l Therearetwo additional interfaces vmin and for settingthe parameters smrn: !l .1"5 I 1) setvmino 2) setsrnin() 0t rraa - TheCamshiftwrapperis onlineat www.cog notics.com/opencv/down loads/camshift-wra pper/i ndex.html. I FIGURE 4. ThehelperfunctionsinirAll ( ) and exitProgramO handle program initializationand cleanup. Face Combinins -and Detection Tracking ! \ 'l or io Jt he you neededto In camshiftdemo, manuallyinitializetrackingwith the mouse.For a roboticsapplication,it wouldbe muchnicerto initialize tracking automatically, usinga face detection that the Haardetectorreturned. (Seelastmonth'sarticlefor detailson implementing facedetection.) Thissectionshowshow to do that usingthe Camshiftwrapperdescribed above.The programdescribedhere detectsa face in a livevideostream, then tracks it with Camshift.The sourcefor code for the complete program,called"TrackFaces,"is also availableonline at www.cognotics. com/opencv/down loads/camshiftwrapper/index.html. 1 2 3 4 5 6 7 8 9 10 1tL2 13 I4 15 l6 L] t-8 L9 20 21, 22 23 24 25 26 21 28 initAll t-nt o t if{ linicCapture{} ) return 0; if ( !initFaceDet (OPn{CV_R@T " /daLa/haarcascades/haarcascade_fronLaI f ace-def aulc . xn] " ) ) return 0; Sfarr'rn mpqq^da fplls rqpr how ro l-eoin and how tO exit ( \\ni*******************************************\n" // pfintf "To exit, "then inside click press the the video display,\n" ESC key\n\n" "Press <mJIER>to begin" \\\n********************************************\n/ ), fdof./cl-din\. // CrcarP fhe disnlaw windOw cvNamedwindow( DISPLAY_WINmW, I / / IntLialtze n:nfr rrol/i ); t.racker donE r^ma / I ' if ( lcreateTracker(pVideoFramecopy) ) return 0; // Sef Camshift n:r:metefs set\iinin(60); setSmin(50); return 1; ne rn I )) rry .++ ker )t e s rese krng 't - 'l-:' r t hl s r snl g The Main Program Figure3 showsthe mainprogram listingfor detectinga face in a live videostream,thentrackingit usingthe CamshiftwrapperAPl.(Thisportionis in TrackFaces.c There in the download.) arethreemainprogramsegments: 1) Detecta face. 2) Startthe tracker. 3) Trackthe face. 1) Detect a face. Lines 15-27 molementa looo to examinevideo 'ramesuntil a face is detected.The invokes :all to capLureVideoFrameO a helper method to bring in the -ext videoframeand createa copyof : (Recallfrom Part1 of thisseries that : s neversafeto modifythe original . Jeo image!)The working copy is .:rred as pVideoPramecopy, declared :: rrne 6. races and r-? aI lown html. . Start the tracker.When a face is :=:ected, the codeexitsthisloop(line - a and startsthe tracker(line 30), ',; ng it the facerectangle from the ",:'detector. tTGURE 5. The helper function '::reVideoFrame0. At line '11,the :all to cvFlipO flips the imageupside or,vn if th" orisin fi"ld ir 0. 1 void exitProgram(int code) 2 { in this file 3 // Release resources allocated 4 cvDestroyWindow ( DISPI"AY-WINDOW) ; 5 cvReleaselmage ( &pvideoFrameCopy ) ; 6 1 in oLher projecc // Release resources allocated .l^eoa^nfrrroll. B 9 closeFaceDet ( ) ; -^t ^-^^m-^^t,^/ \ 10 lElgoJg1!aLAYr \ / i 11 L2 exit (code) ; L3 ] files with a a rectangle datatyperepresents rotation angle. The call to cvEJ-lipseBox0at lines44-45 draws the ellipsedefinedby thisbox. 3) Trackthe face.Lines33-48contain the face-tracking loop.Eachcallto the wrapper'strackO method(line 41) invokesCamshiftto find the face locationin the currentvideoframe.The Camshiftresult is returnedas an OpenCVdatatypecalledcveoxzn.This HelperFunctions In additionto the mainprogram, 1 rrnid ^ ^ n t s , , r 6 \ I i d a ^ t r r . . -a]neo 2 { 3 / / Capture the next frame 4 lplIrnage * pvideoFrarne = nextVideoFrameo; C In\Iido^trr^mo if/ I owirDr^dr:ml-11. 6 '7 t B // Q it f^ Pr ^!,.^^,, v v v v y j Llif1 L 2 \ fha dicnlrrr ufJPrsJ im:aa inrzorfinc ir if nccdcd lpVideoFrameCopy ) n\/i danFr^maf-^n/ J I n a nnz tvPl rt if( Luuvl - r\r,'LLwJ / \ ^\/r y v r v v v r i ^ ^ E r . m ar t 4 , , v , .\raro:f nr/i FTm:ao uY! rlaatr-:ma.an,, y v 4 s ! v r r w , ! ! ! v t s J , 6==pVideoFramecopy->origin /errCci-Qi \! zc ln\/i vvvev+g 0 dcotrr:mel R ? \ . ) ; ) cvFlip(pvideoFrameCopy,0,0) ; SERVO 03.e00739 detectFace ( ) . OpenCVon Sourceforge http :// sourcef orge.na/ Proleds/ opencvllbrary a,= ., . Official OpenCVusergrouP hllp: / / lech.groups.yahoo.com/ group/Open€V "Computer . G.R.Bradski, videoface trackingfor usein a perceptualuser interface,"lntel TechnologyJournal, Q9 1998. . D. Comaniciuand P.Meer, "Robust Analysisof FeatureSPaces:Color fmageSegmentation,"CVPR,1997. . The Simple Camshift \VraPPer www.cognotics.com/opencv/down loads/camshilt-wr apger/ index. htmI . Sourcecode in this articlecan be downloaded from: www. cognotics. com/ ope ncv/ sewo T r a c k F a c ec s a l s o c o n t a i n s h e l p e r f u n c t i o n sf o r i n i t i a l i z a t i oann d c l e a n u p i:,i t;---i ) a n d e x t , t i l ql al 1I . T h e s ea r e s h o w n i n F i g u r e4 . A t l i n e 2 1 i n r - . : r . . .. 1 , t h e c a l l Camshrft wraPper's to the ': i: function Prea l l o c a t e st h e w r a p p e r ' si n t e r n a ld a t a to prestructures1t'snot necessarV ffClne 6. Th; detecipaceO function. deliver image pixelsstarting at the Themin-neighborsparameteris setto 6 to b o t t o m , r a t h e rt h a n a t t h e t o p , o f t h e false a detection. the chance of reduce i m a g e T h er r ' : r . n f i e l di n d i c a t ewsh i c h r o w o r d e ' t h e l p l l m a g eu s e s . S o m e a l l o c a t ten e t r a c k i n gd a t a ,b u t d o i n gs o O p e n C Vf u n c t i o n sw i l l o n l y w o r k c o r speeds the transition from face r e c t l yw h e n t h e s ei m a g e sa r e i n v e r t e d . d e t e c t i o nt o t r a c k i n g T h e n e x t t w o F i n a l l y ,F i g u r e 6 c o n t a i n s t h e statements (lines 24 25) set the n:rameters a n d . . :- : . . Ti r e b e s t r i e - e c i , : a r , -ie if u n c t i o n .A l t h o u g ht h i s code should be famlliarfrom last v a l u e st o u s e f o r t h e s e d e p e n d s o n y o u r s e t u p ,s o i t ' s a g o o d i d e at o s e l e c t m o n t h ' s a r t i c l e , o n e p o i n t w o r t h t h e m a h e a do f t i m e u s i n gt h e c a m s h i f t - n o t i n g i s t h a t t h e r r : r - r - , c , q : b o r ' : ' , p a r a m e t esr h o u l db e s e t h i g h e n o u g h d e m o p r o g r a m ,a s d e s c r i b e da b o v e . t h a t f a l s ef a c ed e t e c t i o n as r e u n l i k e l y Figure 5 shows the listingfor ( O t h e r w i s e ,y o u r r o b o t m i g h t s t a r t i t , . r : r . i : : f r l .A t l i n e 1 1 , a c a l l .1' rl r a g n e t s l )A t f l r p st h e i m a g eu p s i d e t r a c k i n gt h e r e f r i g e r a t om to .- -,'' d o w n i f t h e l r r o ; r - .f i e l d i s O T h e l i n e 1 0 , l ' v e s e t i t t o 6 , w h i c h i s m o r e for doingthrsisthatsomeweb- r e s t r i c t i vteh a n t h e d e f a u l tv a l u eo f 3 reason - ^ ^ A . .v ,c,r ^) . . ^c ).P^v^L -t o, i-l ,) r , 7O n W i n d o w s _ LdtTr ur Showthe punk with the volcono o reol sciencefoir proiect. \ t/ Poper Mdche hos been done. Build your proiect with some unique technology! ComingUp S o f a r ,t h e f a c e sw e ' v eb e e nf i n d i n g a n d f o l l o w i n gh a v e b e e n a n o n y m o u s T l " er o b o tc a nt e l lt h e r e ' sa f a c ep r e s e n t , and can follow it, but has no waYof k n o w i n gw h o s e f a c e i t i s . T h e p r o c e s s o f l r n k i n gf a c e st o n a m e si s c a l l e df a c e recognition OpenCV contains a c o m p l e t e i m p l e m e n t a t i o no f a f a c e recognitiom n e t h o dc a l l e de i g e n f a c e . t w o a r t i c l e si n t h i s The remaining s e r i e sw i l l e x p l a i nh o w t o u s eO p e n C V ' s e i g e n f a c e i m p l e m e n t a t i o nf o r f a c e r e c o g n i t i o nI .n t h e f i r s t o f t h e s e ,l ' l l e x p l a i nh o w t h e a l g o r i t h mw o r k s a n d n g il ,vr (o ryrUn U r r rl U n rU1 qo lLnU .L(lFqAq +LtC ^ .{-+.1-...^ d Udlduo)q ^{ Vl "knows." Thearticle p e o p l ey o u r r o b o t f o l l o w i n gt h a t t a k e s y o u t h r o u g h t h e s t e p s f o r r e c o g n i t i o nf r o m l i v e v i d e o , a n d g i v e sy o u t i p s t o h e l p y o u g e t t h e m o s to u t o f e i g e n f a c e B e s e e t n gy o u l 40 sEnvoo3.eoo7