一 车牌识别概述
车牌识别属于OCR的一种,但它也有自己的特点。考虑到边缘设备部署,我们没有用lstm,仅用普通的卷积层便实现了高精度的车牌识别方案。车牌识别的应用场景也十分广泛,常见的停车场收费系统,车牌识别算法也是智能交通算法中的基础算法和最为重要的算法。
场景 | 识别准确率 |
---|---|
道路卡口场景 | 99.6% |
小角度场景 | 99.0% |
大角度车牌场景 | 98.2% |
二 车牌识别
本方案采用多标签识别的技术,同时设计的一个分类分支,判断输入是否为车牌。其中车牌全部识别正确的准确率为99.6%,判断输入是否为车牌的识别准确率为99.92%。
直接看测试代码:
from __future__ import division
import os
from utils.plate_string_gp import pla_list
from recog.models import resnet18
import numpy as np
import torch
import torch.nn.functional as F
import cv2
from PIL import Image, ImageDraw, ImageFont
from tqdm import tqdm
import glob
torch.backends.cudnn.enabled = False
class Plate_recog():
def __init__(self, recog_path, use_cuda=True):
self.pla_trans()
self.cuda = torch.cuda.is_available() and use_cuda
self.recog = self._load_model(recog_path)
def _load_model(self, model_path):
model = resnet18(num_classes=len(self.pla_dic)+1)
weight = torch.load(model_path)
model.load_state_dict(weight)
model.eval()
if self.cuda:
model.cuda()
return model
def run_recog(self, img, input_size=(192,64)):
img = cv2.resize(img, input_size)
input_img = np.transpose(img, (2,0,1))
input_img = torch.from_numpy(input_img).float()/255.
if self.cuda:
input_img = input_img.cuda()
res, res2 = self.recog(input_img.unsqueeze(0))
res2 = F.softmax(res2[0,:], dim=0)
if res2[0] > res2[1]:
return [False, '不是中国车牌', '']
else:
is_plate = True
plate = ''
p_score = 1
for i in range(8):
res_softmax = F.softmax(res[0,:,i], dim=0)
_, ind_list = torch.sort(res_softmax)
ind = ind_list[-1].item()
if ind == len(self.pla_dic.keys()): # 7 strings
continue
if ind == len(self.pla_dic.keys()) - 1: # blur
ind = ind_list[-2].item()
p_score *= res_softmax[ind].item()
plate += self.pla_dic[ind]
return [is_plate, plate, p_score]
def pla_trans(self):
self.pla_dic = {}
for idx, v in enumerate(pla_list()):
self.pla_dic[idx] = v
if __name__ =='__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
recog = Plate_recog("recog.pth")
img_list = glob.glob("imgs/*.jpg")
for img_path in img_list:
img = cv2.imread(img_path)
is_plate, plate, p_score = recog.run_recog(img)
print("%s的识别结果:%s" % (img_path, plate))
三 识别效果
车牌检测用了centernet和yolov5两种算法,不同的场景采用不同的算法。
注:所有训练数据都是开源数据,训练代码和训练数据以后开源。如需技术交流,请联系博主。
同时推荐另外两个与本文相关的博客:行人属性识别,车辆属性识别。