detecting cats in images with opencv - cache do … · detecting cats in images with opencv ... ,...

11
Navigation Navigation Detecting cats in images with OpenCV by Adrian Rosebrock on June 20, 2016 in Object Detection, Tutorials (source) Did you know that OpenCV can detect cat faces in images…right out-of-the-box with no extras? I didn’t either. But after Kendrick Tan broke the story, I had to check it out for myself…and do a little investigative work to see how this cat detector seemed to sneak its way into the OpenCV repository without me noticing (much like a cat sliding into an empty cereal box, just waiting to be discovered). In the remainder of this blog post, I’ll demonstrate how to use OpenCV’s cat detector to detect cat faces in images. This same technique can be applied to video streams as well. Looking for the source code to this post? Jump right to the downloads section. Detecting cats in images with OpenCV If you take a lookat the OpenCV repository, specifically within the haarcascades directory (where OpenCV stores all its pre-trained Haar classifiers to detect various objects, body parts, etc.), you’ll notice two files: Both of these Haar cascades can be used detecting “cat faces” in images. In fact, I used these very same cascades to generate the example image at the top of this blog post. Doing a little investigative work, I found that the cascades were trained and contributed to the OpenCV repository by the legendary Joseph Howse who’s authored a good many tutorials, books, and talks on computer vision. In the remainder of this blog post, I’llshow you how to utilize Howse’s Haar cascades to detect cats in images. 10 75 haarcascade_frontalcatface.xml haarcascade_frontalcatface_extended.xml Free 21-day crash course on computer vision & image search engines

Upload: truongthien

Post on 30-Jul-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

NavigationNavigation

DetectingcatsinimageswithOpenCVbyAdrianRosebrockonJune20,2016inObjectDetection,Tutorials

(source)

DidyouknowthatOpenCVcandetectcatfacesinimages…rightout-of-the-boxwithnoextras?

Ididn’teither.

ButafterKendrickTanbrokethestory,Ihadtocheckitoutformyself…anddoalittleinvestigativeworktoseehowthiscatdetectorseemedtosneakitswayintotheOpenCVrepositorywithoutmenoticing(muchlikeacatslidingintoanemptycerealbox,justwaitingtobediscovered).

Intheremainderofthisblogpost,I’lldemonstratehowtouseOpenCV’scatdetectortodetectcatfacesinimages.Thissametechniquecanbeappliedtovideostreamsaswell.

Lookingforthesourcecodetothispost?Jumprighttothedownloadssection.

DetectingcatsinimageswithOpenCVIfyoutakealookattheOpenCVrepository,specificallywithinthehaarcascadesdirectory(whereOpenCVstoresallitspre-trainedHaarclassifierstodetectvariousobjects,bodyparts,etc.),you’llnoticetwofiles:

BothoftheseHaarcascadescanbeuseddetecting“catfaces”inimages.Infact,Iusedtheseverysamecascadestogeneratetheexampleimageatthetopofthisblogpost.

Doingalittleinvestigativework,IfoundthatthecascadesweretrainedandcontributedtotheOpenCVrepositorybythelegendaryJosephHowsewho’sauthoredagoodmanytutorials,books,andtalksoncomputervision.

Intheremainderofthisblogpost,I’llshowyouhowtoutilizeHowse’sHaarcascadestodetectcatsinimages.

10 75

haarcascade_frontalcatface.xmlhaarcascade_frontalcatface_extended.xml

Free21-daycrashcourseoncomputervision&imagesearchengines

CatdetectioncodeLet’sgetstarteddetectingcatsinimageswithOpenCV.Openupanewfile,nameit ,andinsertthefollowingcode:

Lines2and3importournecessaryPythonpackageswhileLines6-12parseourcommandlinearguments.Weonlyrequireasingleargumenthere,theinput thatwewanttodetectcatfacesinusingOpenCV.

Wecanalso(optionally)supplyapathourHaarcascadeviathe switch.We’lldefaultthispathtoandassumeyouhavethe fileinthesamedirectoryasyour

script.

Note:I’veconvenientlyincludedthecode,catdetectorHaarcascade,andexampleimagesusedinthistutorialinthe“Downloads”sectionofthisblogpost.Ifyou’renewtoworkingwithPython+OpenCV(orHaarcascades),Iwouldsuggestdownloadingtheprovided.zipfiletomakeiteasiertofollowalong.

Next,let’sdetectthecatsinourinputimage:

OnLines15and16weloadourinputimagefromdiskandconvertittograyscale(anormalpre-processingstepbeforepassingtheimagetoaHaarcascadeclassifier,althoughnotstrictlyrequired).

Line20loadsourHaarcascadefromdisk(inthiscase,thecatdetector)andinstantiatesthe object.

DetectingcatfacesinimageswithOpenCVisaccomplishedonLines21and22bycallingthe methodoftheobject.Wepassfourparameterstothe method,including:

1. Ourimage, ,thatwewanttodetectcatfacesin.2. A ofourimagepyramidusedwhendetectingcatfaces.Alargerscalefactorwillincreasethespeedofthedetector,butcouldharmourtrue-positivedetectionaccuracy.Conversely,asmallerscalewillslowdownthedetectionprocess,butincreasetrue-positivedetections.However,thissmallerscalecanalsoincreasethefalse-positivedetectionrateaswell.Seethe“AnoteonHaarcascades”sectionofthisblogpostformoreinformation.

3. The parametercontrolstheminimumnumberofdetectedboundingboxesinagivenareafortheregiontobeconsidereda“catface”.Thisparameterisveryhelpfulinpruningfalse-positivedetections.

4. Finally,the parameterisprettyself-explanatory.Thisvalueensuresthateachdetectedboundingboxisatleastwidthxheightpixels(inthiscase,75x75).

The functionreturns ,alistof4-tuples.Thesetuplescontainthe(x,y)-coordinatesandwidthandheightofeachdetectedcatface.

Finally,let’sdrawarectanglesurroundeachcatfaceintheimage:

Givenourboundingboxes(i.e., ),weloopovereachofthemindividuallyonLine25.

cat_detector.py

--image

--cascadehaarcascade_frontalcatface.xml haarcascade_frontalcatface.xmlcat_detector.py

cv2.CascadeClassifier

detectMultiScaledetector detectMultiScale

grayscaleFactor

minNeighbors

minSize

detectMultiScale rects

rects

123456789101112

#importthenecessarypackagesimportargparseimportcv2#constructtheargumentparseandparsetheargumentsap=argparse.ArgumentParser()ap.add_argument("-i","--image",required=True, help="pathtotheinputimage")ap.add_argument("-c","--cascade", default="haarcascade_frontalcatface.xml", help="pathtocatdetectorhaarcascade")args=vars(ap.parse_args())

141516171819202122

#loadtheinputimageandconvertittograyscaleimage=cv2.imread(args["image"])gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)#loadthecatdetectorHaarcascade,thendetectcatfaces#intheinputimagedetector=cv2.CascadeClassifier(args["cascade"])rects=detector.detectMultiScale(gray,scaleFactor=1.3, minNeighbors=10,minSize=(75,75))

242526272829303132

#loopoverthecatfacesanddrawarectanglesurroundingeachfor(i,(x,y,w,h))inenumerate(rects): cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2) cv2.putText(image,"Cat#{}".format(i+1),(x,y-10), cv2.FONT_HERSHEY_SIMPLEX,0.55,(0,0,255),2)#showthedetectedcatfacescv2.imshow("CatFaces",image)cv2.waitKey(0)

DetectingcatsinimageswithOpenCV Python

DetectingcatsinimageswithOpenCV Python

DetectingcatsinimageswithOpenCV Python

WethendrawarectanglesurroundingeachcatfaceonLine26,whileLines27and28displaysaninteger,countingthenumberofcatsintheimage.

Finally,Lines31and32displaytheoutputimagetoourscreen.

CatdetectionresultsTotestourOpenCVcatdetector,besuretodownloadthesourcecodetothistutorialusingthe“Downloads”sectionatthebottomofthispost.

Then,afteryouhaveunzippedthearchive,youshouldhavethefollowingthreefiles/directories:

1. :OurPython+OpenCVscriptusedtodetectcatsinimages.2. :ThecatdetectorHaarcascade.3. :Adirectoryoftestingimagesthatwe’regoingtoapplythecatdetectorcascadeto.

Fromthere,executethefollowingcommand:

Figure1:Detectingacatfaceinanimage,evenwithpartsofthecatoccluded(source).

Noticethatwehavebeenabletodetectthecatfaceintheimage,eventhoughtherestofitsbodyisobscured.

Let’stryanotherimage:

cat_detector.pyhaarcascade_frontalcatface.xmlimages

1 $pythoncat_detector.py--imageimages/cat_01.jpg

1 pythoncat_detector.py--imageimages/cat_02.jpg

DetectingcatsinimageswithOpenCV Shell

DetectingcatsinimageswithOpenCV Python

Figure2:AsecondexampleofdetectingacatinanimagewithOpenCV,thistimethecatfaceisslightlydifferent(source).

Thiscat’sfaceisclearlydifferentfromtheotherone,asit’sinthemiddleofa“meow”.Ineithercase,thecatdetectorcascadeisabletocorrectlyfindthecatfaceintheimage.

Thesameistrueforthisimageaswell:

Figure3:CatdetectionwithOpenCVandPython(source).

OurfinalexampledemonstratesdetectingmultiplecatsinanimageusingOpenCVandPython:

1 $pythoncat_detector.py--imageimages/cat_03.jpgDetectingcatsinimageswithOpenCV Shell

Figure4:DetectingmultiplecatsinthesameimagewithOpenCV(source).

NotethattheHaarcascadecanreturnboundingboxesinanorderthatyoumaynotlike.Inthiscase,themiddlecatisactuallylabeledasthethirdcat.Youcanresolvethis“issue”bysortingtheboundingboxesaccordingtotheir(x,y)-coordinatesforaconsistentordering.

AquicknoteonaccuracyIt’simportanttonotethatinthecommentssectionofthe files,JosephHowedetailsthatthecatdetectorHaarcascadescanreportcatfaceswherethereareactuallyhumanfaces.

Inthiscase,herecommendsperformingbothfacedetectionandcatdetection,thendiscardinganycatboundingboxesthatoverlapwiththefaceboundingboxes.

AnoteonHaarcascadesFirstpublishedin2001byPaulViolaandMichaelJones,RapidObjectDetectionusingaBoostedCascadeofSimpleFeatures,thisoriginalworkhasbecomeoneofthemostcitedpapersincomputervision.

Thisalgorithmiscapableofdetectingobjectsinimages,regardlessoftheirlocationandscale.Andperhapsmostintriguing,thedetectorcanruninreal-timeonmodernhardware.

Intheirpaper,ViolaandJonesfocusedontrainingafacedetector;however,theframeworkcanalsobeusedtotraindetectorsforarbitrary“objects”,suchascars,bananas,roadsigns,etc.

Theproblem?ThebiggestproblemwithHaarcascadesisgettingthe parametersright,specifically and

.Youcaneasilyrunintosituationswhereyouneedtotunebothoftheseparametersonanimage-by-imagebasis,whichisfarfromidealwhenutilizinganobjectdetector.

The variablecontrolsyourimagepyramidusedtodetectobjectsatvariousscalesofanimage.Ifyour istoolarge,thenyou’llonlyevaluateafewlayersoftheimagepyramid,potentiallyleadingtoyoumissingobjectsatscalesthatfallinbetweenthepyramidlayers.

Ontheotherhand,ifyouset toolow,thenyouevaluatemanypyramidlayers.Thiswillhelpyoudetectmoreobjectsinyourimage,butit(1)makesthedetectionprocessslowerand(2)substantiallyincreasesthefalse-positivedetectionrate,somethingthatHaarcascadesareknownfor.

Torememberthis,weoftenapplyHistogramofOrientedGradients+LinearSVMdetectioninstead.

TheHOG+LinearSVMframeworkparametersarenormallymucheasiertotune—andbestofall,HOG+LinearSVMenjoysamuchsmallerfalse-positivedetectionrate.Theonlydownsideisthatit’shardertogetHOG+LinearSVMtoruninreal-time.

Interestedinlearningmoreaboutobjectdetection?

.xml

detectMultiScale scaleFactorminNeighbors

scaleFactor scaleFactor

scaleFactor

1 $pythoncat_detector.py--imageimages/cat_04.jpgDetectingcatsinimageswithOpenCV Shell

Figure5:LearnhowtobuildcustomobjectdetectorsinsidethePyImageSearchGuruscourse.

Ifyou’reinterestedinlearninghowtotrainyourowncustomobjectdetectors,besuretotakealookatthePyImageSearchGuruscourse.

Insidethecourse,Ihave15lessonscovering168pagesoftutorialsdedicatedtoteachingyouhowtobuildcustomobjectdetectorsfromscratch.You’lldiscoverhowtodetectroadsigns,faces,cars(andnearlyanyotherobject)inimagesbyapplyingtheHOG+LinearSVMframeworkforobjectdetection.

TolearnmoreaboutthePyImageSearchGuruscourse(andgrab10FREEsamplelessons),justclickthebuttonbelow:

ClickheretolearnmoreaboutPyImageSearchGurus!

SummaryInthisblogpost,welearnedhowtodetectcatsinimagesusingthedefaultHaarcascadesshippedwithOpenCV.TheseHaarcascadesweretrainedandcontributedtotheOpenCVprojectbyJosephHowse,andwereoriginallybroughttomyattentioninthispostbyKendrickTan.

WhileHaarcascadesarequiteuseful,weoftenuseHOG+LinearSVMinstead,asit’sabiteasiertotunethedetectorparameters,andmoreimportantly,wecanenjoyamuchlowerfalse-positivedetectionrate.

IdetailhowtobuildcustomHOG+LinearSVMobjectdetectorstorecognizevariousobjectsinimages,includingcars,roadsigns,andmuchmoreinsidethePyImageSearchGuruscourse.

Anyway,Ihopeyouenjoyedthisblogpost!

Beforeyougo,besuretosignupforthePyImageSearchNewsletterusingtheformbelowtobenotifiedwhennewblogpostsarepublished.

Downloads:Ifyouwouldliketodownloadthecodeandimagesusedinthispost,pleaseenteryouremailaddressintheformbelow.Notonlywillyougeta.zipofthecode,I’llalsosendyouaFREE11-pageResourceGuideonComputerVisionandImageSearchEngines,includingexclusivetechniquesthatIdon’tpostonthisblog!Soundgood?Ifso,enteryouremailaddressandI’llsendyouthecodeimmediately!Emailaddress:

Youremailaddress

DOWNLOADTHECODE!

Considerationswhensettingupdeeplearninghardware MyTop9FavoritePythonDeepLearningLibraries

ResourceGuide(it’stotallyfree).

Enteryouremailaddressbelowtogetmyfree11-pageImageSearchEngineResourceGuidePDF.UncoverexclusivetechniquesthatIdon'tpublishonthisblogandstartbuildingimagesearchenginesofyourown!

Youremailaddress

DOWNLOADTHEGUIDE!

classification,haarcascades,objectdetection

16ResponsestoDetectingcatsinimageswithOpenCV

JosephHowseJune20,2016at6:36pm#

Thankssomuchforfeaturingthecatfacecascades!Iamdelightedtoseetheminactionherealongsideyourothergreattutorials!

Funfacts:

Thehaarcascade_frontalcatface_extended.xmlversionusesthe“extended”Haarfeaturesettomakeitsensitivetodiagonalfeaturessuchasearsandwhiskers.>^-^<

BesidestheHaarversions,youwillfindanLBPversion,lbpcascade_frontalcatface.xml,inOpenCV'slbpcascadesfolder.TheLBPversionislessaccuratebutfaster;youmightlikeitforRaspberryPiorotherplatformswithlimitedresources.

Detailsaboutthetrainingofthecatfacecascadescanbefoundinmybook,OpenCVforSecretAgents,andinfreepresentationsonmywebsite'sOpenCVlandingpage(http://www.nummist.com/opencv/).

Ninelives,

Joe

REPLY

AdrianRosebrockJune23,2016at1:34pm#

You’retheoneweshouldbethankingJoe—youtrainedtheactualcascades!

REPLY

KendrickTanJune20,2016at11:46pm#

HiAdrian,I’mabitstokedwhenIsawmynamecomeuponthenewsletter.

Anyway,Ijustcameheretosaythankyou.Yourtutorialsreallygavemeafirmunderstandingofthebasicsofcomputervision,andmachinelearning.Reallyappreciateyourtimeandeffort.

REPLY

AdrianRosebrockJune23,2016at1:33pm#

HeyKendrick!Thanksfordoingtheinitialawesomepost—withoutyourpost,thecatdetectioncascadewouldhavetotallypassedmeby.Ishouldbetheonethankingyou

REPLY

LinusJune21,2016at7:39am#

Wow.ThanksAdrian,thisissoawesome!I’vethoughtaboutthepossibitityofdetectinganimalssometimeago,butI’veneverfounda

REPLY

solutionWhataboutotheranimals(dogsetc.)?Aretherealsoxmlfilesavailable?Andisusingthisinalivecamerafeedalsopossibe,withamoderateexecutionspeed?

AdrianRosebrockJune23,2016at1:29pm#

AsfarasIknow,thereisonlytheHaarcascadeforcats—thereisn’tonefordogs.Althoughyoucouldcertainlytrainone.IwouldsuggestusingHOG+LinearSVMinsteadsinceithasalowerfalse-positiverate.

Andyes,thismethodcanbeusedtoruninreal-time.TakealookatPracticalPythonandOpenCVformoredetailsonrunningHaarcascadesinreal-time.

REPLY

RanjeetSinghJune21,2016at12:06pm#

ButnowIwanttoknowwhatthatxmlfileis?Howithasbeenwritten?

REPLY

AdrianRosebrockJune23,2016at1:27pm#

TheXMLfileisaserializedHaarcascade.Itisgeneratedbytrainingamachinelearningalgorithmtorecognizevariousobjectsinimages.Torecognizeyourownobjects,you’llneedtotrainyourowncustomobjectdetector.

REPLY

swightNovember10,2016at7:23pm#

HowwouldIwritethefinalimagetofileinsteadofdisplayingtoscreen?

REPLY

AdrianRosebrockNovember14,2016at12:16pm#

Youcanjustusethecv2.imwritefunction:

cv2.imwrite("/path/to/my/image.jpg",image)

IfyouneedhelplearningthebasicsofcomputervisionandimageprocessingIwouldsuggestyouworkthroughPracticalPythonandOpenCV.

REPLY

VictorDecember17,2016at9:34pm#

WetrainedanLBPcascadewithbettertimeandaccuracyperformance.Ifanyonewantsthis,wecanpushittoour10imaging/opencvrepoonGitHub.

REPLY

RidgeDecember19,2016at8:01pm#

@Victor–Sure,I’dliketoseeit.

REPLY

GismoSchaMarch15,2017at8:28pm#

ThanksAdrian,itworks!Howcaniextract/copytherectangledareaandwritetofile?

REPLY

AdrianRosebrockMarch17,2017at9:31am#

YouwoulduseNumPyarrayslicingtoextracttheboundingboxandthenusecv2.imwritetowritetheimagetodisk.IdiscussthesebasicoperationsinsidePracticalPythonandOpenCV.IwouldsuggestyoustartthereifyouarenewtoOpenCV.

REPLY

LeaveaReply

Name(required)

Email(willnotbepublished)(required)

Website

SUBMITCOMMENT

ResourceGuide(it’stotallyfree).

Clickthebuttonbelowtogetmyfree11-pageImageSearchEngineResourceGuidePDF.UncoverexclusivetechniquesthatIdon'tpublishonthisblogandstartbuildingimagesearchenginesofyourown.

DownloadforFree!

DeepLearningforComputerVisionwithPythonBook

HarmanSinghMarch18,2017at5:06pm#

ThankAdrian,yourtutorialsaregreatandfunandalsodetectsfairlygoodbutwhiledeployingthisscriptonmultipleimageswithdifferentsize,theoutputisnottoaccurate.

forexamplein1stpic(catrolledinpaper)igot2rectangleonecoveringthefaceandotheronthenose..(toget1rect,iadjustedscaleFactorto1.715)

evenafterconvertingalltheimagetosizestillgettingmixedresultlikementionedaboveandsometimesnone.

doihavetoadjustscalefactoreverytime?orisitmehavingthisproblem?

ThankYou.

REPLY

AdrianRosebrockMarch21,2017at7:35am#

ThisisacommonproblemwithHaarcascades(theyareverypronetofalse-positives).YoucanreadmoreabouttheissueinthisblogpostonHistogramofOrientedGradients.

REPLY

You'reinterestedindeeplearningandcomputervision,butyoudon'tknowhowtogetstarted.Letmehelp.Mynewbookwillteachyouallyouneedtoknowaboutdeeplearning.

CLICKHERETOPRE-ORDERMYNEWBOOK

Youcandetectfacesinimages&video.

Areyouinterestedindetectingfacesinimages&video?ButtiredofGooglingfortutorialsthatneverwork?Thenletmehelp!Iguaranteethatmynewbookwillturnyouintoafacedetectionninjabytheendofthisweekend.Clickheretogiveitashotyourself.

CLICKHERETOMASTERFACEDETECTION

PyImageSearchGurus:NOWENROLLING!

ThePyImageSearchGuruscourseisnowenrolling!Insidethecourseyou'lllearnhowtoperform:

AutomaticLicensePlateRecognition(ANPR)DeepLearningFaceRecognitionandmuchmore!

Clickthebuttonbelowtolearnmoreaboutthecourse,takeatour,andget10(FREE)samplelessons.

TAKEATOUR&GET10(FREE)LESSONS

Hello!I’mAdrianRosebrock.

FindmeonTwitter,Facebook,Google+,andLinkedIn.©2017PyImageSearch.AllRightsReserved.

I'manentrepreneurandPh.Dwhohaslaunchedtwosuccessfulimagesearchengines,IDMyPillandChicEngine.I'mheretosharemytips,tricks,andhacksI'velearnedalongtheway.

Learncomputervisioninasingleweekend.

Wanttolearncomputervision&OpenCV?Icanteachyouinasingleweekend.Iknow.Itsoundscrazy,butit’snojoke.Mynewbookisyourguaranteed,quick-startguidetobecominganOpenCVNinja.Sowhynotgiveitatry?Clickheretobecomeacomputervisionninja.

CLICKHERETOBECOMEANOPENCVNINJA

SubscribeviaRSS

Nevermissapost!SubscribetothePyImageSearchRSSFeedandkeepuptodatewithmyimagesearchenginetutorials,tips,andtricks

InstallOpenCVandPythononyourRaspberryPi2andB+FEBRUARY23,2015

HomesurveillanceandmotiondetectionwiththeRaspberryPi,Python,OpenCV,andDropboxJUNE1,2015

HowtoinstallOpenCV3onRaspbianJessieOCTOBER26,2015

Installguide:RaspberryPi3+RaspbianJessie+OpenCV3APRIL18,2016

BasicmotiondetectionandtrackingwithPythonandOpenCVMAY25,2015

InstallOpenCV3.0andPython2.7+onUbuntuJUNE22,2015

AccessingtheRaspberryPiCamerawithOpenCVandPythonMARCH30,2015

Search

Search...

POPULAR