1说明: 1。1对于强大的yolov3来说,汽车车牌的定位,太简单了! 1。2技术要点:对蓝底和白底车牌超强定位,正面的、侧面的均可。 1。3环境:yolov3python3。8OpenCV4。2。0华为笔记本电脑deepinlinux操作系统。 1。4图片来自今日头条正版免费图库,仅供学习,注释讲解,小白秒懂。 pic1:原图 pic2:定位图 pic3:原图 pic4:定位图 pic5:原图 pic6:定位图 2yolov3的三个文件: 2。1下载地址:https:www。kaggle。comachrafkhazriyoloweightsforlicenceplatedetector下载速度超级快 2。2图: 2。3文件结构: 2。4操作示范: 图片定位,视频省略 3main。py代码: 3。1注意本代码可以进行图片车牌定位,也可以进行视频和摄像头视频定位。强!! 3。2完整代码:第1步:使用说明文件所在目录下终端输入Usageexample:python3main。pyvideocar。mp4python3main。pyimagetest。jpeg本机:python3。8main。pyimagetest。jpeg第2步:模块导入importcv2ascvimportargparseimportsysimportnumpyasnpimportos。path第3步:初始化参数InitializetheparametersconfThreshold0。5Confidencethreshold过滤弱检测的最小概率nmsThreshold0。4Nonmaximumsuppressionthreshold非最大值抑制阈值inpWidth416608WidthofnetworksinputimageinpHeight416608Heightofnetworksinputimageargparse库可以让我们直接在命令行中就可以向程序中传入参数并让程序运行parserargparse。ArgumentParser(descriptionObjectDetectionusingYOLOinOPENCV)parser。addargument(image,helpPathtoimagefile。)parser。addargument(video,helpPathtovideofile。)argsparser。parseargs()第4步:加载yolov3文件classesFileclasses。names在图片或视频上框上的名字classesNonewithopen(classesFile,rt)asf:classesf。read()。rstrip()。split()modelConfigurationdarknetyolov3。cfgmodelWeightslapi。weights;加载网络、配置权重netcv。dnn。readNetFromDarknet(modelConfiguration,modelWeights)一般情况都是使用opencvdnn作为后台计算net。setPreferableBackend(cv。dnn。DNNBACKENDOPENCV)表示在CPU设备上使用net。setPreferableTarget(cv。dnn。DNNTARGETCPU)第5步:函数定义Getthenamesoftheoutputlayers获取名字:车牌仅仅一个:LPdefgetOutputsNames(net):GetthenamesofallthelayersinthenetworklayersNamesnet。getLayerNames()Getthenamesoftheoutputlayers,i。e。thelayerswithunconnectedoutputsreturn〔layersNames〔i〔0〕1〕foriinnet。getUnconnectedOutLayers()〕画预测绿色框boxDrawthepredictedboundingboxdefdrawPred(classId,conf,left,top,right,bottom):Drawaboundingbox。cv。rectangle(frame,(left,top),(right,bottom),(0,255,0),3)label。2fconfGetthelabelfortheclassnameanditsconfidenceifclasses:assert(classIdlen(classes))labels:s(classes〔classId〕,label)DisplaythelabelatthetopoftheboundingboxlabelSize,baseLinecv。getTextSize(label,cv。FONTHERSHEYSIMPLEX,0。5,1)topmax(top,labelSize〔1〕)红色文字框cv。rectangle(frame,(left,topround(1。5labelSize〔1〕)),(leftround(1。5labelSize〔0〕),topbaseLine),(0,0,255),cv。FILLED)黑色文字显示cv。putText(frame,label,(left,top),cv。FONTHERSHEYSIMPLEX,0。75,(0,0,0),2)定位过程函数defpostprocess(frame,outs):获取宽和高frameHeightframe。shape〔0〕frameWidthframe。shape〔1〕初始化三个列表classIds〔〕confidences〔〕boxes〔〕类似yolov3的目标检测读取foroutinouts:print(out。shape:,out。shape)fordetectioninout:scoresdetection〔5:〕classIdnp。argmax(scores)confidencescores〔classId〕ifdetection〔4〕confThreshold:print(detection〔4〕,,scores〔classId〕,th:,confThreshold)print(detection)ifconfidenceconfThreshold:centerxint(detection〔0〕frameWidth)centeryint(detection〔1〕frameHeight)widthint(detection〔2〕frameWidth)heightint(detection〔3〕frameHeight)leftint(centerxwidth2)topint(centeryheight2)classIds。append(classId)confidences。append(float(confidence))boxes。append(〔left,top,width,height〕)indicescv。dnn。NMSBoxes(boxes,confidences,confThreshold,nmsThreshold)foriinindices:ii〔0〕boxboxes〔i〕leftbox〔0〕topbox〔1〕widthbox〔2〕heightbox〔3〕画出预测框:绿色框和文字框drawPred(classIds〔i〕,confidences〔i〕,left,top,leftwidth,topheight)第6步:图片和视频判定如果是视频,则在根目录下动态显示目标检测或者车牌检测的视频outputFileyolooutpy。avi图片判定if(args。image):Opentheimagefileifnotos。path。isfile(args。image):print(Inputimagefile,args。image,doesntexist)sys。exit(1)capcv。VideoCapture(args。image)在当前目录下输出outputFileargs。image〔:4〕yolooutpy。jpg视频判断elif(args。video):Openthevideofileifnotos。path。isfile(args。video):print(Inputvideofile,args。video,doesntexist)sys。exit(1)capcv。VideoCapture(args。video)当前目录下输出完成后的视频outputFileargs。video〔:4〕yolooutpy。avi摄像头判断else:Webcaminputcapcv。VideoCapture(0)如果不是图片,那么写入视频设置Getthevideowriterinitializedtosavetheoutputvideoif(notargs。image):vidwritercv。VideoWriter(outputFile,cv。VideoWriterfourcc(M,J,P,G),30,(round(cap。get(cv。CAPPROPFRAMEWIDTH)),round(cap。get(cv。CAPPROPFRAMEHEIGHT))))第7步:循环whilecv。waitKey(1)0:视频类判定getframefromthevideohasFrame,framecap。read()StoptheprogramifreachedendofvideoifnothasFrame:print(Doneprocessing!!!)print(Outputfileisstoredas,outputFile)cv。waitKey(3000)breakCreatea4Dblobfromaframe。blobcv。dnn。blobFromImage(frame,1255,(inpWidth,inpHeight),〔0,0,0〕,1,cropFalse)Setstheinputtothenetworknet。setInput(blob)Runstheforwardpasstogetoutputoftheoutputlayersoutsnet。forward(getOutputsNames(net))Removetheboundingboxeswithlowconfidencepostprocess(frame,outs)Putefficiencyinformation。ThefunctiongetPerfProfilereturnstheoveralltimeforinference(t)andthetimingsforeachofthelayers(inlayersTimes)t,net。getPerfProfile()labelInferencetime:。2fms(t1000。0cv。getTickFrequency())左上角输出红色的文字:耗时,暂时注释掉cv。putText(frame,label,(0,15),cv。FONTHERSHEYSIMPLEX,0。5,(0,0,255))写入设置if(args。image):cv。imwrite(outputFile,frame。astype(np。uint8))else:vidwriter。write(frame。astype(np。uint8))