小kiss!Yolov3的汽车车牌定位python实现
1 说明:
=====
1.1 对于强大的yolov3来说,汽车车牌的定位,太简单了!
1.2 技术要点:对蓝底和白底车牌超强定位,正面的、侧面的均可。
1.3 环境:yolov3+python3.8+OpenCV4.2.0+华为笔记本电脑+deepin-linux操作系统。
1.4 图片来自今日头条正版免费图库,仅供学习,注释讲解,小白秒懂。
pic1:原图
pic2:定位图
pic3:原图
pic4:定位图
pic5:原图
pic6:定位图
2 yolov3的三个文件:
===============
2.1 下载地址:https://www.kaggle.com/achrafkhazri/yolo-weights-for-licence-plate-detector #下载速度超级快
2.2 图:
2.3 文件结构:
2.4 操作示范:
图片定位,视频省略
3 main.py代码:
===========
3.1 注意本代码可以进行图片车牌定位,也可以进行视频和摄像头视频定位。强!!
3.2 完整代码:#第1步:使用说明 #文件所在目录下终端输入 # Usage example: python3 main.py --video=car.mp4 # python3 main.py --image=test.jpeg #本机:python3.8 main.py --image=test.jpeg #第2步:模块导入 import cv2 as cv import argparse import sys import numpy as np import os.path #第3步:初始化参数 Initialize the parameters confThreshold = 0.5 #Confidence threshold 过滤弱检测的最小概率 nmsThreshold = 0.4 #Non-maximum suppression threshold 非最大值抑制阈值 inpWidth = 416 #608 #Width of network"s input image inpHeight = 416 #608 #Height of network"s input image #argparse库可以让我们直接在命令行中就可以向程序中传入参数并让程序运行 parser = argparse.ArgumentParser(description="Object Detection using YOLO in OPENCV") parser.add_argument("--image", help="Path to image file.") parser.add_argument("--video", help="Path to video file.") args = parser.parse_args() #第4步:加载yolov3文件 classesFile = "classes.names" #在图片或视频上框上的名字 classes = None with open(classesFile, "rt") as f: classes = f.read().rstrip(" ").split(" ") modelConfiguration = "darknet-yolov3.cfg" modelWeights = "lapi.weights"; # 加载网络、配置权重 net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights) #一般情况都是使用opencv dnn作为后台计算 net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV) #表示在CPU设备上使用 net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) #第5步:函数定义 # Get the names of the output layers # 获取名字:车牌仅仅一个:LP def getOutputsNames(net): # Get the names of all the layers in the network layersNames = net.getLayerNames() # Get the names of the output layers, i.e. the layers with unconnected outputs return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()] #画预测绿色框box # Draw the predicted bounding box def drawPred(classId, conf, left, top, right, bottom): # Draw a bounding box. cv.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3) label = "%.2f" % conf # Get the label for the class name and its confidence if classes: assert(classId < len(classes)) label = "%s:%s" % (classes[classId], label) #Display the label at the top of the bounding box labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1) top = max(top, labelSize[1]) #红色文字框 cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (0, 0, 255), cv.FILLED) #黑色文字显示 cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 2) #定位过程函数 def postprocess(frame, outs): #获取宽和高 frameHeight = frame.shape[0] frameWidth = frame.shape[1] #初始化三个列表 classIds = [] confidences = [] boxes = [] #类似yolov3的目标检测读取 for out in outs: print("out.shape : ", out.shape) for detection in out: scores = detection[5:] classId = np.argmax(scores) confidence = scores[classId] if detection[4]>confThreshold: print(detection[4], " - ", scores[classId], " - th : ", confThreshold) print(detection) if confidence > confThreshold: center_x = int(detection[0] * frameWidth) center_y = int(detection[1] * frameHeight) width = int(detection[2] * frameWidth) height = int(detection[3] * frameHeight) left = int(center_x - width / 2) top = int(center_y - height / 2) classIds.append(classId) confidences.append(float(confidence)) boxes.append([left, top, width, height]) indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold) for i in indices: i = i[0] box = boxes[i] left = box[0] top = box[1] width = box[2] height = box[3] #画出预测框:绿色框和文字框 drawPred(classIds[i], confidences[i], left, top, left + width, top + height) #第6步:图片和视频判定 #如果是视频,则在根目录下动态显示目标检测或者车牌检测的视频 outputFile = "yolo_out_py.avi" #图片判定 if (args.image): # Open the image file if not os.path.isfile(args.image): print("Input image file ", args.image, " doesn"t exist") sys.exit(1) cap = cv.VideoCapture(args.image) #在当前目录下输出 outputFile = args.image[:-4]+"_yolo_out_py.jpg" #视频判断 elif (args.video): # Open the video file if not os.path.isfile(args.video): print("Input video file ", args.video, " doesn"t exist") sys.exit(1) cap = cv.VideoCapture(args.video) #当前目录下输出完成后的视频 outputFile = args.video[:-4]+"_yolo_out_py.avi" #摄像头判断 else: # Webcam input cap = cv.VideoCapture(0) #如果不是图片,那么写入视频设置 # Get the video writer initialized to save the output video if (not args.image): vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc("M","J","P","G"), 30, (round(cap.get(cv.CAP_PROP_FRAME_WIDTH)),round(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))) #第7步:循环 while cv.waitKey(1) < 0: #视频类判定 # get frame from the video hasFrame, frame = cap.read() # Stop the program if reached end of video if not hasFrame: print("Done processing !!!") print("Output file is stored as ", outputFile) cv.waitKey(3000) break # Create a 4D blob from a frame. blob = cv.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop=False) # Sets the input to the network net.setInput(blob) # Runs the forward pass to get output of the output layers outs = net.forward(getOutputsNames(net)) # Remove the bounding boxes with low confidence postprocess(frame, outs) # Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes) t, _ = net.getPerfProfile() label = "Inference time: %.2f ms" % (t * 1000.0 / cv.getTickFrequency()) #左上角输出红色的文字:耗时,暂时注释掉 #cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255)) #写入设置 if (args.image): cv.imwrite(outputFile, frame.astype(np.uint8)) else: vid_writer.write(frame.astype(np.uint8))
白血病专挑孩子下手,建议这3种水果,家长不要买给孩子吃导语当我们看新闻发现有的孩子小小年纪就被病魔侵扰,心里都不是个滋味。孩子小,抵抗力不完全,很容易就被白血病等强劲的病毒伤害,这不由得让家长医院社会思考,我们应当做些什么才能够让孩子
高智商儿童拥有的22个特征,你家孩子有吗?父母都希望自己的孩子聪明伶俐,可是高智商孩子的特征有哪些呢?先来看看几个关于高智商孩子的故事。英国有个小神童叫奥斯卡塞尔,7岁时便成为了英国财政大臣乔治奥斯本最年轻的一位顾问,就英
小婴儿多大才认母?若能在这一月龄前认出妈妈,说明很努力了中国有一句老话叫母子连心。孩子和母亲之间好像有天然的羁绊,而母亲对于孩子的爱是非常伟大的。从十月怀胎到一朝分娩,再到之后的几十年,母亲总是用爱陪伴着孩子。而这样的羁绊在孩子的身上也
消息!幼儿园迎来新改革,家长拍手叫好,私立学校注意了好在现如今的幼儿园主要分为两种,个人修建和公家投资,由于某些幼儿园收费高乱收费教师水平良莠不齐等问题屡屡发生。要求国家重新审视幼儿园呼声也越来越大,针对此类问题,国家推出了一系列的方
这3种老人带出来的孩子更聪明更独立,聪明的父母会这么做第一脾气性格温和的老人带孩子本身就是件繁琐的工作,孩子一天频繁地要吃要喝要玩还要闹脾气,更何况日复一日,年复一年要重复着这件工作,而打骂都不利于孩子的成长,破坏孩子的天性不说,还影
现在的孩子不敢小觑我的花样生活昨天去弟妹开的幼儿园帮忙。一个五岁的小男孩把我惊到,他的观察能力,空间想象能力都使我震惊。还有一个敢于表达自己情感,敢爱的孩子,也令我刮目相看。感叹现在的孩子真是了不得
如何解决孩子玩电子游戏?随着信息化社会的进步,电子产品成为了每个人不能缺少的生活必需品,身边随处可见低头族,不论大人小孩都沉迷于其中。对于小孩子来说,他们根本没有自控的能力,很多时候都需要家长来执行,有时
引领右脑专家建议千万别对孩子说反话小时候,我们最害怕的就是父母说不要我了。每次一听到这句话,就一边哭,一边拽住他们的腿,哪里都不让他们去,生怕他们走了,就不回来。所以,总是像跟屁虫一样跟着他们。前几天,听朋友小米说
等我有了孩子,可不敢让她当老师了,幼教晒出工资单,太委屈文文儿谁能比我更喜欢孩子,我都去当幼师了!文文此话一出,姐妹们顿时哑火,原本是在讨论妈妈对孩子和家庭的付出贡献,被文文这么一掺和,主题瞬间变成我们有多喜欢孩子。对比文文这名人民教师
人到中年,应该明白的道理有哪些?1人到中年,应该看透人际关系,本质是价值交换。2人到中年,应该把赚钱放在第一位,明白金钱的重要性。3人到中年,应该对家人负责任,因为父母老了,孩子还没成年。4人到中年,应该看清自己
孩子的缺点大多源于父亲,别再忽视父亲的重要性了,影响孩子成长现在的家庭教育有一个普遍现象孩子张开闭口叫的都是妈妈,吃饭叫妈妈,睡觉也叫妈妈。孩子的成长过程中,妈妈占了绝大部分的比重,而爸爸只是忙于工作,很少出席孩子的成长。有的家庭认为,妈妈