用PaddlePaddle(飞浆)实现车牌识别

举报
小小谢先生 发表于 2022/04/16 00:36:42 2022/04/16
【摘要】 项目描述:本次实践是一个多分类任务,需要将照片中的每个字符分别进行识别,完成车牌的识别 实践平台:百度AI实训平台-AI Studio、PaddlePaddle1.8.0 动态图 数据集介绍(自己去网上下载车牌识别数据集) 数据集文件名为characterData.zip,其中有65个文件夹 包含0-9,A-Z,以及各省简...

项目描述:本次实践是一个多分类任务,需要将照片中的每个字符分别进行识别,完成车牌的识别

实践平台:百度AI实训平台-AI Studio、PaddlePaddle1.8.0 动态图

数据集介绍(自己去网上下载车牌识别数据集)

  • 数据集文件名为characterData.zip,其中有65个文件夹

  • 包含0-9,A-Z,以及各省简称

  • 图片为12020的灰度图像

  • 本次实验中,取其中的10%作为测试集,90%作为训练集

 


  
  1. #导入需要的包
  2. import os
  3. import zipfile
  4. import random
  5. import json
  6. import cv2
  7. import numpy as np
  8. from PIL import Image
  9. import paddle
  10. import paddle.fluid as fluid
  11. from paddle.fluid.dygraph import Linear
  12. import matplotlib.pyplot as plt

1、数据准备


  
  1. '''
  2. 参数配置
  3. '''
  4. train_parameters = {
  5. "input_size": [1, 20, 20], #输入图片的shape
  6. "class_dim": -1, #分类数
  7. "src_path":"data/data23617/characterData.zip", #原始数据集路径
  8. "target_path":"/home/aistudio/data/dataset", #要解压的路径
  9. "train_list_path": "./train_data.txt", #train_data.txt路径
  10. "eval_list_path": "./val_data.txt", #eval_data.txt路径
  11. "label_dict":{}, #标签字典
  12. "readme_path": "/home/aistudio/data/readme.json", #readme.json路径
  13. "num_epochs": 100, #训练轮数
  14. "train_batch_size": 32, #批次的大小
  15. "learning_strategy": { #优化函数相关的配置
  16. "lr": 0.0005 #超参数学习率
  17. }
  18. }
  19. def unzip_data(src_path,target_path):
  20. '''
  21. 解压原始数据集,将src_path路径下的zip包解压至data/dataset目录下
  22. '''
  23. if(not os.path.isdir(target_path)):
  24. z = zipfile.ZipFile(src_path, 'r')
  25. z.extractall(path=target_path)
  26. z.close()
  27. else:
  28. print("文件已解压")
  29. def get_data_list(target_path,train_list_path,eval_list_path):
  30. '''
  31. 生成数据列表
  32. '''
  33. #存放所有类别的信息
  34. class_detail = []
  35. #获取所有类别保存的文件夹名称
  36. data_list_path=target_path
  37. class_dirs = os.listdir(data_list_path)
  38. if '__MACOSX' in class_dirs:
  39. class_dirs.remove('__MACOSX')
  40. # #总的图像数量
  41. all_class_images = 0
  42. # #存放类别标签
  43. class_label=0
  44. # #存放类别数目
  45. class_dim = 0
  46. # #存储要写进eval.txt和train.txt中的内容
  47. trainer_list=[]
  48. eval_list=[]
  49. #读取每个类别
  50. for class_dir in class_dirs:
  51. if class_dir != ".DS_Store":
  52. class_dim += 1
  53. #每个类别的信息
  54. class_detail_list = {}
  55. eval_sum = 0
  56. trainer_sum = 0
  57. #统计每个类别有多少张图片
  58. class_sum = 0
  59. #获取类别路径
  60. path = os.path.join(data_list_path,class_dir)
  61. # print(path)
  62. # 获取所有图片
  63. img_paths = os.listdir(path)
  64. for img_path in img_paths: # 遍历文件夹下的每个图片
  65. if img_path =='.DS_Store':
  66. continue
  67. name_path = os.path.join(path,img_path) # 每张图片的路径
  68. if class_sum % 10 == 0: # 每10张图片取一个做验证数据
  69. eval_sum += 1 # eval_sum为测试数据的数目
  70. eval_list.append(name_path + "\t%d" % class_label + "\n")
  71. else:
  72. trainer_sum += 1
  73. trainer_list.append(name_path + "\t%d" % class_label + "\n")#trainer_sum测试数据的数目
  74. class_sum += 1 #每类图片的数目
  75. all_class_images += 1 #所有类图片的数目
  76. # 说明的json文件的class_detail数据
  77. class_detail_list['class_name'] = class_dir #类别名称
  78. class_detail_list['class_label'] = class_label #类别标签
  79. class_detail_list['class_eval_images'] = eval_sum #该类数据的测试集数目
  80. class_detail_list['class_trainer_images'] = trainer_sum #该类数据的训练集数目
  81. class_detail.append(class_detail_list)
  82. #初始化标签列表
  83. train_parameters['label_dict'][str(class_label)] = class_dir
  84. class_label += 1
  85. #初始化分类数
  86. train_parameters['class_dim'] = class_dim
  87. print(train_parameters)
  88. #乱序
  89. random.shuffle(eval_list)
  90. with open(eval_list_path, 'a') as f:
  91. for eval_image in eval_list:
  92. f.write(eval_image)
  93. #乱序
  94. random.shuffle(trainer_list)
  95. with open(train_list_path, 'a') as f2:
  96. for train_image in trainer_list:
  97. f2.write(train_image)
  98. # 说明的json文件信息
  99. readjson = {}
  100. readjson['all_class_name'] = data_list_path #文件父目录
  101. readjson['all_class_images'] = all_class_images
  102. readjson['class_detail'] = class_detail
  103. jsons = json.dumps(readjson, sort_keys=True, indent=4, separators=(',', ': '))
  104. with open(train_parameters['readme_path'],'w') as f:
  105. f.write(jsons)
  106. print ('生成数据列表完成!')
  107. def data_reader(file_list):
  108. '''
  109. 自定义data_reader
  110. '''
  111. def reader():
  112. with open(file_list, 'r') as f:
  113. lines = [line.strip() for line in f]
  114. for line in lines:
  115. img_path, lab = line.strip().split('\t')
  116. img = cv2.imread(img_path)
  117. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  118. img = np.array(img).astype('float32')
  119. img = img/255.0
  120. yield img, int(lab)
  121. return reader
  122. '''
  123. 参数初始化
  124. '''
  125. src_path=train_parameters['src_path']
  126. target_path=train_parameters['target_path']
  127. train_list_path=train_parameters['train_list_path']
  128. eval_list_path=train_parameters['eval_list_path']
  129. batch_size=train_parameters['train_batch_size']
  130. '''
  131. 解压原始数据到指定路径
  132. '''
  133. unzip_data(src_path,target_path)
  134. #每次生成数据列表前,首先清空train.txt和eval.txt
  135. with open(train_list_path, 'w') as f:
  136. f.seek(0)
  137. f.truncate()
  138. with open(eval_list_path, 'w') as f:
  139. f.seek(0)
  140. f.truncate()
  141. #生成数据列表
  142. get_data_list(target_path,train_list_path,eval_list_path)
  143. '''
  144. 构造数据提供器
  145. '''
  146. train_reader = paddle.batch(data_reader(train_list_path),
  147. batch_size=batch_size,
  148. drop_last=True)
  149. eval_reader = paddle.batch(data_reader(eval_list_path),
  150. batch_size=batch_size,
  151. drop_last=True)
  152. Batch=0
  153. Batchs=[]
  154. all_train_accs=[]
  155. def draw_train_acc(Batchs, train_accs):
  156. title="training accs"
  157. plt.title(title, fontsize=24)
  158. plt.xlabel("batch", fontsize=14)
  159. plt.ylabel("acc", fontsize=14)
  160. plt.plot(Batchs, train_accs, color='green', label='training accs')
  161. plt.legend()
  162. plt.grid()
  163. plt.show()
  164. all_train_loss=[]
  165. def draw_train_loss(Batchs, train_loss):
  166. title="training loss"
  167. plt.title(title, fontsize=24)
  168. plt.xlabel("batch", fontsize=14)
  169. plt.ylabel("loss", fontsize=14)
  170. plt.plot(Batchs, train_loss, color='red', label='training loss')
  171. plt.legend()
  172. plt.grid()
  173. plt.show()

  
  1. Batch=0
  2. Batchs=[]
  3. all_train_accs=[]
  4. def draw_train_acc(Batchs, train_accs):
  5. title="training accs"
  6. plt.title(title, fontsize=24)
  7. plt.xlabel("batch", fontsize=14)
  8. plt.ylabel("acc", fontsize=14)
  9. plt.plot(Batchs, train_accs, color='green', label='training accs')
  10. plt.legend()
  11. plt.grid()
  12. plt.show()
  13. all_train_loss=[]
  14. def draw_train_loss(Batchs, train_loss):
  15. title="training loss"
  16. plt.title(title, fontsize=24)
  17. plt.xlabel("batch", fontsize=14)
  18. plt.ylabel("loss", fontsize=14)
  19. plt.plot(Batchs, train_loss, color='red', label='training loss')
  20. plt.legend()
  21. plt.grid()
  22. plt.show()

2、定义模型


  
  1. 定义DNN网络
  2. class MyDNN(fluid.dygraph.Layer):
  3. '''
  4. DNN网络
  5. '''
  6. def __init__(self):
  7. super(MyDNN,self).__init__()
  8. self.hidden1= Linear(20*20,400,act='relu')
  9. self.hidden2 = Linear(400,200,act='relu')
  10. self.hidden3 = Linear(200,100,act='relu')
  11. self.out = Linear(100,65,act='softmax')
  12. def forward(self,input): # forward 定义执行实际运行时网络的执行逻辑
  13. '''前向计算'''
  14. x = fluid.layers.reshape(input, shape=[-1,20*20]) #-1 表示这个维度的值是从x的元素总数和剩余维度推断出来的,有且只能有一个维度设置为-1
  15. x = self.hidden1(x)
  16. x = self.hidden2(x)
  17. # print('2',x.shape)
  18. x = self.hidden3(x)
  19. # print('3',x.shape)
  20. y = self.out(x)
  21. # print('4',y.shape)
  22. return y

3、训练模型


  
  1. with fluid.dygraph.guard():
  2. model=MyDNN() #模型实例化
  3. model.train() #训练模式
  4. opt=fluid.optimizer.SGDOptimizer(learning_rate=train_parameters['learning_strategy']['lr'], parameter_list=model.parameters())#优化器选用SGD随机梯度下降,学习率为0.001.
  5. epochs_num=train_parameters['num_epochs'] #迭代次数
  6. for pass_num in range(epochs_num):
  7. for batch_id,data in enumerate(train_reader()):
  8. images=np.array([x[0].reshape(1,20,20) for x in data],np.float32)
  9. labels = np.array([x[1] for x in data]).astype('int64')
  10. labels = labels[:, np.newaxis]
  11. image=fluid.dygraph.to_variable(images)
  12. label=fluid.dygraph.to_variable(labels)
  13. predict=model(image) #数据传入model
  14. loss=fluid.layers.cross_entropy(predict,label)
  15. avg_loss=fluid.layers.mean(loss)#获取loss值
  16. acc=fluid.layers.accuracy(predict,label)#计算精度
  17. if batch_id!=0 and batch_id%50==0:
  18. Batch = Batch+50
  19. Batchs.append(Batch)
  20. all_train_loss.append(avg_loss.numpy()[0])
  21. all_train_accs.append(acc.numpy()[0])
  22. print("train_pass:{},batch_id:{},train_loss:{},train_acc:{}".format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
  23. avg_loss.backward()
  24. opt.minimize(avg_loss) #优化器对象的minimize方法对参数进行更新
  25. model.clear_gradients() #model.clear_gradients()来重置梯度
  26. fluid.save_dygraph(model.state_dict(),'MyDNN')#保存模型
  27. draw_train_acc(Batchs,all_train_accs)
  28. draw_train_loss(Batchs,all_train_loss)

训练结果:

4、模型评估


  
  1. #模型评估
  2. with fluid.dygraph.guard():
  3. accs = []
  4. model_dict, _ = fluid.load_dygraph('MyDNN')
  5. model = MyDNN()
  6. model.load_dict(model_dict) #加载模型参数
  7. model.eval() #训练模式
  8. for batch_id,data in enumerate(eval_reader()):#测试集
  9. images=np.array([x[0].reshape(1,20,20) for x in data],np.float32)
  10. labels = np.array([x[1] for x in data]).astype('int64')
  11. labels = labels[:, np.newaxis]
  12. image=fluid.dygraph.to_variable(images)
  13. label=fluid.dygraph.to_variable(labels)
  14. predict=model(image)
  15. acc=fluid.layers.accuracy(predict,label)
  16. accs.append(acc.numpy()[0])
  17. avg_acc = np.mean(accs)
  18. print(avg_acc)

输出精确率为:

0.9289216

5、使用模型

5.1对车牌图像进行预处理


  
  1. # 对车牌图片进行处理,分割出车牌中的每一个字符并保存
  2. license_plate = cv2.imread('work/车牌.png')
  3. gray_plate = cv2.cvtColor(license_plate, cv2.COLOR_RGB2GRAY)
  4. ret, binary_plate = cv2.threshold(gray_plate, 175, 255, cv2.THRESH_BINARY) #ret:阈值,binary_plate:根据阈值处理后的图像数据
  5. # 按列统计像素分布
  6. result = []
  7. for col in range(binary_plate.shape[1]):
  8. result.append(0)
  9. for row in range(binary_plate.shape[0]):
  10. result[col] = result[col] + binary_plate[row][col]/255
  11. # print(result)
  12. #记录车牌中字符的位置
  13. character_dict = {}
  14. num = 0
  15. i = 0
  16. while i < len(result):
  17. if result[i] == 0:
  18. i += 1
  19. else:
  20. index = i + 1
  21. while result[index] != 0:
  22. index += 1
  23. character_dict[num] = [i, index-1]
  24. num += 1
  25. i = index
  26. # print(character_dict)
  27. #将每个字符填充,并存储
  28. characters = []
  29. for i in range(8):
  30. if i==2:
  31. continue
  32. padding = (170 - (character_dict[i][1] - character_dict[i][0])) / 2
  33. #将单个字符图像填充为170*170
  34. ndarray = np.pad(binary_plate[:,character_dict[i][0]:character_dict[i][1]], ((0,0), (int(padding), int(padding))), 'constant', constant_values=(0,0))
  35. ndarray = cv2.resize(ndarray, (20,20))
  36. cv2.imwrite('work/' + str(i) + '.png', ndarray)
  37. characters.append(ndarray)
  38. def load_image(path):
  39. img = paddle.dataset.image.load_image(file=path, is_color=False)
  40. img = img.astype('float32')
  41. img = img[np.newaxis, ] / 255.0
  42. return img

5.2 对标签进行转换


  
  1. #将标签进行转换
  2. print('Label:',train_parameters['label_dict'])
  3. match = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N',
  4. 'O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z',
  5. 'yun':'云','cuan':'川','hei':'黑','zhe':'浙','ning':'宁','jin':'津','gan':'赣','hu':'沪','liao':'辽','jl':'吉','qing':'青','zang':'藏',
  6. 'e1':'鄂','meng':'蒙','gan1':'甘','qiong':'琼','shan':'陕','min':'闽','su':'苏','xin':'新','wan':'皖','jing':'京','xiang':'湘','gui':'贵',
  7. 'yu1':'渝','yu':'豫','ji':'冀','yue':'粤','gui1':'桂','sx':'晋','lu':'鲁',
  8. '0':'0','1':'1','2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9'}
  9. L = 0
  10. LABEL ={}
  11. for V in train_parameters['label_dict'].values():
  12. LABEL[str(L)] = match[V]
  13. L += 1
  14. print(LABEL)

输出label为:

Label: {'0': 'sx', '1': 'e1', '2': 'yu1', '3': 'yue', '4': 'K', '5': 'P', '6': '3', '7': 'qing', '8': 'yu', '9': 'E', '10': 'zang', '11': 'xin', '12': 'J', '13': 'hei', '14': 'M', '15': 'lu', '16': 'S', '17': '6', '18': '0', '19': 'hu', '20': 'U', '21': 'A', '22': 'D', '23': 'shan', '24': 'zhe', '25': 'liao', '26': 'H', '27': 'Z', '28': 'wan', '29': 'N', '30': 'W', '31': 'C', '32': 'meng', '33': 'X', '34': '8', '35': 'F', '36': 'jl', '37': 'R', '38': 'ji', '39': 'Q', '40': 'Y', '41': 'yun', '42': 'gan1', '43': 'L', '44': 'cuan', '45': '9', '46': 'su', '47': 'jin', '48': 'min', '49': 'V', '50': '1', '51': 'gui1', '52': 'B', '53': '7', '54': 'xiang', '55': 'qiong', '56': 'G', '57': 'jing', '58': 'ning', '59': 'T', '60': '5', '61': 'gui', '62': '2', '63': '4', '64': 'gan'}
{'0': '晋', '1': '鄂', '2': '渝', '3': '粤', '4': 'K', '5': 'P', '6': '3', '7': '青', '8': '豫', '9': 'E', '10': '藏', '11': '新', '12': 'J', '13': '黑', '14': 'M', '15': '鲁', '16': 'S', '17': '6', '18': '0', '19': '沪', '20': 'U', '21': 'A', '22': 'D', '23': '陕', '24': '浙', '25': '辽', '26': 'H', '27': 'Z', '28': '皖', '29': 'N', '30': 'W', '31': 'C', '32': '蒙', '33': 'X', '34': '8', '35': 'F', '36': '吉', '37': 'R', '38': '冀', '39': 'Q', '40': 'Y', '41': '云', '42': '甘', '43': 'L', '44': '川', '45': '9', '46': '苏', '47': '津', '48': '闽', '49': 'V', '50': '1', '51': '桂', '52': 'B', '53': '7', '54': '湘', '55': '琼', '56': 'G', '57': '京', '58': '宁', '59': 'T', '60': '5', '61': '贵', '62': '2', '63': '4', '64': '赣'}

5.3 使用模型进行预测


  
  1. #构建预测动态图过程
  2. with fluid.dygraph.guard():
  3. model=MyDNN()#模型实例化
  4. model_dict,_=fluid.load_dygraph('MyDNN')
  5. model.load_dict(model_dict)#加载模型参数
  6. model.eval()#评估模式
  7. lab=[]
  8. for i in range(8):
  9. if i==2:
  10. continue
  11. infer_imgs = []
  12. infer_imgs.append(load_image('work/' + str(i) + '.png'))
  13. infer_imgs = np.array(infer_imgs)
  14. infer_imgs = fluid.dygraph.to_variable(infer_imgs)
  15. result=model(infer_imgs)
  16. lab.append(np.argmax(result.numpy()))
  17. print(lab)
  18. display(Image.open('work/车牌.png'))
  19. for i in range(len(lab)):
  20. print(LABEL[str(lab[i])],end='')
输出:[15, 21, 17, 34, 17, 9, 12]

<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=722x170 at 0x7F8E98B3C410>
预测:鲁A686EJ。。表示预测正确

文章来源: blog.csdn.net,作者:小小谢先生,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/xiewenrui1996/article/details/107714711

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。