OpenCV调用OpenVINO模型vehicle-license-plate-detection-barrier检测车牌
Posted in openCV By On 2020年12月14日,3197 Views在OpenVINO+Win10安装及环境配置攻略中,我们通过调用相关模型检测并识别车牌及检测车身颜色。本文将分享如何在python+OpenCV的环境下调用OpenVINO的车牌检测模型vehicle-license-plate-detection-barrier-0106检测车牌并分析该模型的检测输出数据。
1. 运行环境
openvino安装与环境配置请参考OpenVINO+Win10安装及环境配置攻略。
2. 参考资料
3. vehicle-license-plate-detection-barrier-0106
这里我们重点关注其对检测对象的要求:
1 2 3 4 5 6 7 8 |
name: "input" , shape: [1x3x300x300] - An input image in the format [BxCxHxW], where: B - batch size C - number of channels H - image height W - image width Expected color order is BGR. |
要求输入图像的高度与宽度均为300,颜色顺序为BGR,OpenCV的imread读取图像的顺序为BGR。
除上述要求外,该模型可检测车辆的姿态为“Front facing cars”,车牌最小宽度为“96 pixels”。本文也做了一些简单的测试,车头或车尾的车牌均可以检测,但图片像素过低时无法检测。大家根据自己的需求进行选用。 在OpenVINO+Win10安装及环境配置攻略一文中运行图像检测脚本demo_security_barrier_camera.bat时,会下载名为:
不同的电脑可能会有些许差异,可参考上述路径进行查找。本文为测试便捷,将vehicle-license-plate-detection-barrier-0106文件夹及其内容拷贝至测试路径下。
3. 在OpenCV调用模型vehicle-license-plate-detection-barrier-0106
在vehicle-license-plate-detection-barrier-0106文件夹相同路径下新建testOpenVINO.py文件,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import numpy as np import cv2 import imutils testImg = cv2.imread("AE8F80.jpg") pd_net = cv2.dnn.Net_readFromModelOptimizer('./vehicle-license-plate-detection-barrier-0106/vehicle-license-plate-detection-barrier-0106.xml', './vehicle-license-plate-detection-barrier-0106/vehicle-license-plate-detection-barrier-0106.bin') pd_net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE) pd_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) # testImg = cv2.resize(testImg, (300, 300), interpolation = cv2.INTER_AREA) #name: "input" , shape: [1x3x300x300] - An input image in the format [BxCxHxW], where: pd_blob = cv2.dnn.blobFromImage(testImg, size=(300, 300)) pd_net.setInput(pd_blob) out_pb = pd_net.forward() print("result shape:",out_pb.shape) print("plate results:",out_pb) # Draw detected cars on the image. for detection in out_pb.reshape(-1, 7): confidence = float(detection[2]) xmin = int(detection[3] * testImg.shape[1]) ymin = int(detection[4] * testImg.shape[0]) xmax = int(detection[5] * testImg.shape[1]) ymax = int(detection[6] * testImg.shape[0]) if confidence > 0.5: cv2.rectangle(testImg, (xmin, ymin), (xmax, ymax), color=(0, 0, 255)) showImg = imutils.resize(testImg, height=600) cv2.imshow('testImg', showImg) cv2.waitKey(0) |
第1~2行导入必要库;
运行testOpenVINO.py的步骤:
3.1 打开command.exe,切换至OpenVINO安装目录下的setupvars.bat文件所在路径,运行setupvars.bat。
3.2 在当前command.exe窗口中切换至testOpenVINO.py所在路径运行。上述两步的运行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
C:\Users\username>cd C:\Program Files (x86)\Intel\openvino_2021.1.110\bin C:\Program Files (x86)\Intel\openvino_2021.1.110\bin>setupvars.bat Python 3.6.5 [setupvars.bat] OpenVINO environment initialized C:\Program Files (x86)\Intel\openvino_2021.1.110\bin>D: D:\>cd D:\Python\OpenVINOMODEL D:\Python\OpenVINOMODEL>python testOpenVINO.py [E:] [BSL] found 0 ioexpander device |
4. vehicle-license-plate-detection-barrier检测结果解析
接下来我们以上图为例来说明vehicle-license-plate-detection-barrier-0106的检测结果。
在vehicle-license-plate-detection-barrier-0106中可以看到其对输出的描述如下:
1 2 3 4 5 6 7 |
The net outputs a blob with the shape: [1, 1, N, 7], where N is the number of detected bounding boxes. For each detection, the description has the format: [image_id, label, conf, x_min, y_min, x_max, y_max] image_id - ID of the image in the batch label - predicted class ID conf - confidence for the predicted class (x_min, y_min) - coordinates of the top left bounding box corner (x_max, y_max) - coordinates of the bottom right bounding box corner. |
对于每一个检测到目标的外接矩形,其属性包含当前图片的ID,检测到目标的类别,该目标检测结果的置信度,目标外接矩形的左上角和右下角坐标。
检测目标的类别:label=1,表示当前目标是车身;label=2,表示当前目标是车牌。
例图的检测结果输出为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
result shape: (1, 1, 200, 7) plate results: [[[[0. 1. 0.9855322 ... 0.03102607 0.94578093 0.65100473] [0. 1. 0.1294965 ... 0.21292609 0.5185891 0.6665251 ] [0. 1. 0.01475777 ... 0.02002868 1.0053165 0.5260596 ] ... [0. 0. 0. ... 0. 0. 0. ] [0. 0. 0. ... 0. 0. 0. ] [0. 0. 0. ... 0. 0. 0. ]]]] |
本文到此结束,感谢支持,欢迎关注。