基于AIdlux的智慧社区之车牌识别

一、项目简介

Aidlux智慧社区训练营下子任务之车牌识别!!!。本次训练营由dadao老师教学,其中会用到车牌检测+车牌识别的项目。

关于Aidlux的使用说明和详细操作实例。大家可以看一下这个:《智慧安防AI实战训练营:边缘端实现越界识别》Lesson2

  • 车牌识别方案

因在不同的业务场景中,车牌识别的算法方案是不一样的。比如在加油站的车牌识别场景中,车占整个图片的比例较小时,则建议在车牌检测前,增加一个车的检测模块。在检测到车后,再对车进行车牌的检测和识别。

车牌识别的方案主要有两种:

1.粗粒度的:车牌检测+车牌识别;

2.细粒度的:车牌检测+车牌矫正+车牌识别。

后一种方法增加了车牌矫正,考虑场景中车牌在区域中发生角度变化,若车牌与相机是相对平行的,则不需要矫正。否则,需矫正,一般车牌的水平度和垂直度超过15°,建议增加矫正环节。

d842ab63b6594014a71155b8fb428f17.png

 

 

(1)粗粒度车牌识别:前者我们需要两个标签,车牌两角围成的矩形框做车牌检测训练,车牌号码标签做车牌识别训练。

(2)细粒度车牌识别:后者则需要加上一个标签,即车牌的四个角以及倾斜角,用于车牌矫正训练。

具体的开发过程可以去项目简介内查看。

  • 最终代码及视频
  1. 通过对车牌识别模型进行轻量化转化为 tflite框架,车辆检测通过yolov5来完成,转换为onnx再导出tflite模型就行。使用netron 校对outputs和inputs的shape。
  2. 将整体代码上传至AIDLUX上实现部署,注意在aidlux上要使用cvs模块
  3. 使用path = "/home/code_plate_detection_recognization_1/aidlux/ppad.mp4",capture = cvs.VideoCapture(path),实现视频流读取,对每帧进行识别速度太慢可以设置5帧识别一下2b4c45b8d074454dbda2fcc2ccd203ff.png

 

4. 使用pil 重写plot_one_box_class 方法实现中文显示,PIL格式是RGB而CV格式BRG注意转换

801a3faeb32d45fa86fee086a8514b81.png

 

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    

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值