一、项目简介
Aidlux智慧社区训练营下子任务之车牌识别!!!。本次训练营由dadao老师教学,其中会用到车牌检测+车牌识别的项目。
关于Aidlux的使用说明和详细操作实例。大家可以看一下这个:《智慧安防AI实战训练营:边缘端实现越界识别》Lesson2
因在不同的业务场景中,车牌识别的算法方案是不一样的。比如在加油站的车牌识别场景中,车占整个图片的比例较小时,则建议在车牌检测前,增加一个车的检测模块。在检测到车后,再对车进行车牌的检测和识别。
车牌识别的方案主要有两种:
1.粗粒度的:车牌检测+车牌识别;
2.细粒度的:车牌检测+车牌矫正+车牌识别。
后一种方法增加了车牌矫正,考虑场景中车牌在区域中发生角度变化,若车牌与相机是相对平行的,则不需要矫正。否则,需矫正,一般车牌的水平度和垂直度超过15°,建议增加矫正环节。
(1)粗粒度车牌识别:前者我们需要两个标签,车牌两角围成的矩形框做车牌检测训练,车牌号码标签做车牌识别训练。
(2)细粒度车牌识别:后者则需要加上一个标签,即车牌的四个角以及倾斜角,用于车牌矫正训练。
具体的开发过程可以去项目简介内查看。
4. 使用pil 重写plot_one_box_class 方法实现中文显示,PIL格式是RGB而CV格式BRG注意转换
5. AIDLUX最终效果展示
基于AIdlux的实时车牌识别展示ps:手机太差了卡卡的_哔哩哔哩_bilibili
基于AIdlux的智慧社区之车牌识别!!- AidLux开发者社区
6.整体代码
# aidlux相关
from cvs import *
import aidlite_gpu
from utils import *
import time
import cv2
import os
anchor = [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]]
source ="/home/code_plate_detection_recognization_1/demo/images"
det_model_path = "/home/code_plate_detection_recognization_1/weights/yolov5.tflite"
recog_model_path = "/home/code_plate_detection_recognization_1/weights/LPRNet_Simplified.tflite"
save_dir = "/home/code_plate_detection_recognization_1/demo/output"
imgsz =640
# AidLite初始化:调用AidLite进行AI模型的加载与推理,需导入aidlite
aidlite = aidlite_gpu.aidlite()
# Aidlite模型路径
# 定义输入输出shape
# 加载Aidlite检测模型:支持tflite, tnn, mnn, ms, nb格式的模型加载
aidlite.set_g_index(0)
in_shape0 = [1 * 3* 640 * 640 * 4]
out_shape0 = [1 * 3*40*40 * 6 * 4,1 * 3*20*20 * 6 * 4,1 * 3*80*80 * 6 * 4]
aidlite.ANNModel(det_model_path, in_shape0, out_shape0, 4, 0)
# 识别模型
aidlite.set_g_index(1)
inShape1 =[1 * 3 * 24 *94*4]
outShape1= [1 * 68*18*4]
aidlite.ANNModel(recog_model_path,inShape1,outShape1,4,-1)
path = "/home/code_plate_detection_recognization_1/aidlux/ppad.mp4"
capture = cvs.VideoCapture(path)
index = 1
while True:
if index %5 ==0:
image_ori = capture.read()
print("adadada",image_ori.shape)
# frame = cv2.imread("/home/code_plate_detection_recognization_1/demo/images/003748802682-91_84-220&469_341&511-328&514_224&510_224&471_328&475-10_2_5_22_31_31_27-103-12.jpg")
# img = preprocess_img(frame, target_shape=(640, 640), div_num=255, means=None, stds=None)
img, scale, left, top = det_preprocess(image_ori, imgsz=640)
# 数据转换:因为setTensor_Fp32()需要的是float32类型的数据,所以送入的input的数据需为float32,大多数的开发者都会忘记将图像的数据类型转换为float32
aidlite.set_g_index(0)
aidlite.setInput_Float32(img, 640, 640)
# 模型推理API
aidlite.invoke()
# 读取返回的结果
outputs = [0,0,0]
for i in range(len(anchor)):
pred = aidlite.getOutput_Float32(i)
# 数据维度转换
if pred.shape[0] ==28800:
pred = pred.reshape(1, 3,40,40, 6)
outputs[1] = pred
if pred.shape[0] ==7200:
pred = pred.reshape(1, 3,20,20, 6)
outputs[0] = pred
if pred.shape[0]==115200:
pred = pred.reshape(1,3,80,80, 6)
outputs[2] = pred
# 模型推理后处理
boxes, confs, classes = det_poseprocess(outputs, imgsz, scale, left, top,conf_thresh=0.3, iou_thresh =0.5)
pred = np.hstack((boxes, confs,classes)).astype(np.float32, copy=False)
for i, det in enumerate(pred): # detections per image
if len(det):
xyxy,conf, cls= det[:4],det[4],det[5:]
if xyxy.min()<0:
continue
# filter
xyxy = np.reshape(xyxy, (1, 4))
xyxy_ = np.copy(xyxy).tolist()[0]
xyxy_ = [int(i) for i in xyxy_]
if (xyxy_[2] -xyxy_[0])/(xyxy_[3]-xyxy_[1])>6 or (xyxy_[2] -xyxy_[0])<100:
continue
# image_crop = np.array(image_ori[xyxy_[1]:xyxy_[3], xyxy_[0]:xyxy_[2]])
# image_crop = np.asarray(image_crop)
img_crop = np.array(image_ori[xyxy_[1]:xyxy_[3], xyxy_[0]:xyxy_[2]])
image_recog = reg_preprocess(img_crop)
print(image_recog.max(), image_recog.min(),type(image_recog),image_recog.shape)
# recognization inference
aidlite.set_g_index(1)
aidlite.setInput_Float32(image_recog,94,24)
aidlite.invoke()
#取得模型的输出数据
probs = aidlite.getOutput_Float32(0)
print(probs.shape)
probs = np.reshape(probs, (1, 68, 18))
print("------",probs)
# proprocess
probs = reg_postprocess(probs)
# print("pred_str", probs)
for prob in probs:
lb = ""
for i in prob:
lb += CHARS[i]
cls = lb
# result show
label = f'names{[str(cls)]} {conf:.2f}'
print(label)
# plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
image_ori = plot_one_box_class(xyxy_, image_ori, label=label, predstr=cls,
line_thickness=3)
# plot_one_box_class(xyxy_, image_ori, label=label, predstr=cls,
# line_thickness=3)
# Save results (image with detections)
# img_path = os.path.join(save_dir, img_name)
# cv2.imwrite(img_path, image_ori)
cvs.imshow(image_ori)
index+=1