detecting cats in images with opencv - cache do … · detecting cats in images with opencv ... ,...
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