赞
踩
上一章讲了检测模型的实现,这一章将一下识别模型的实现,PaddleOCR提供的通用字符集有6623个,但是车牌号就不需要这么多,我们只需要识别这些车牌,蓝牌、黄牌(单行)、绿牌、大型新能源(黄绿)、领使馆车牌、警牌、武警牌(单行)、军牌(单行)、港澳出入境车牌,只用提取极少部分字符即可,下面我们来接着将代码如何实现的。
在ppocr/utils/目录中创建一份只有数字的字符集ppocr_keys_car.txt,71个字符:
川鄂甘赣贵桂黑沪吉冀晋津京辽鲁蒙闽宁青琼陕苏皖湘新渝豫粤云藏浙澳港警学使领 0123456789 ABCDEFGHJKLMNPQRSTUVWXYZ
注意字母字符中不包含“O”,这与数字0在车牌上显示有歧义。
数据集可在上一篇文章中下载,目前收集了3万张左右的数据集,为了保证数据集字符平衡,我们就需要合成一部分数据集,这3万张中一半是真实一半是合成的
数据集部分切图,上面是真实的,下面是合成的:
复制一份yml文件
文件内容如下:
- Global:
- use_gpu: true
- epoch_num: 500
- log_smooth_window: 20
- print_batch_step: 10
- save_model_dir: ./output/rec_chinese_lite_v2.0_car
- save_epoch_step: 50
- # evaluation is run every 5000 iterations after the 4000th iteration
- # eval_batch_step: [0, 2000]
- eval_batch_step: 100
- cal_metric_during_train: True
- # pretrained_model: ./pretrained_models/ch_ppocr_mobile_v2.0_rec_pre/best_accuracy
- pretrained_model:
- checkpoints:
- save_inference_dir: ./inference/rec_chinese_lite_v2.0_car
- use_visualdl: False
- infer_img: doc/imgs_words/ch/word_1.jpg
- # for data or label process
- character_dict_path: ppocr/utils/ppocr_keys_car.txt
- character_type: ch
- max_text_length: 25
- infer_mode: False
- use_space_char: False
- save_res_path: ./output/rec/predicts_chinese_lite_v2.0.txt
-
-
- Optimizer:
- name: Adam
- beta1: 0.9
- beta2: 0.999
- lr:
- name: Cosine
- learning_rate: 0.001
- regularizer:
- name: 'L2'
- factor: 0.00001
-
- Architecture:
- model_type: rec
- algorithm: CRNN
- Transform:
- Backbone:
- name: MobileNetV3
- scale: 0.5
- model_name: small
- small_stride: [1, 2, 2, 2]
- Neck:
- name: SequenceEncoder
- encoder_type: rnn
- hidden_size: 48
- Head:
- name: CTCHead
- fc_decay: 0.00001
-
- Loss:
- name: CTCLoss
-
- PostProcess:
- name: CTCLabelDecode
-
- Metric:
- name: RecMetric
- main_indicator: acc
-
- Train:
- dataset:
- name: SimpleDataSet
- data_dir: ./train_data/
- label_file_list:
- - "./train_data/car/car1/rec_gt_train.txt"
- - "./train_data/car/car2/rec_gt_train.txt"
- - "./train_data/car/car3/rec_gt_train.txt"
- ratio_list: [1.0,1.0,0.3]
- transforms:
- - DecodeImage: # load image
- img_mode: BGR
- channel_first: False
- - RecAug:
- - CTCLabelEncode: # Class handling label
- - RecResizeImg:
- image_shape: [3, 32, 320]
- - KeepKeys:
- keep_keys: ['image', 'label', 'length'] # dataloader will return list in this order
- loader:
- shuffle: True
- batch_size_per_card: 32
- drop_last: True
- num_workers: 8
-
- Eval:
- dataset:
- name: SimpleDataSet
- data_dir: ./train_data/
- label_file_list:
- - "./train_data/car/car1/rec_gt_test.txt"
- - "./train_data/car/car2/rec_gt_test.txt"
- - "./train_data/car/car3/rec_gt_test.txt"
- ratio_list: [1.0,1.0,0.3]
- transforms:
- - DecodeImage: # load image
- img_mode: BGR
- channel_first: False
- - CTCLabelEncode: # Class handling label
- - RecResizeImg:
- image_shape: [3, 32, 320]
- - KeepKeys:
- keep_keys: ['image', 'label', 'length'] # dataloader will return list in this order
- loader:
- shuffle: False
- drop_last: False
- batch_size_per_card: 32
- num_workers: 8
python -m paddle.distributed.launch --gpus 0 tools/train.py -o Global.use_visualdl=True -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0_car.yml
注意显存问题,需要大于4G的显存,2G显存会溢出
visualdl --logdir=output/rec_chinese_lite_v2.0_car/vdl --port 8081
识别模型效果最高能到达:acc:0.99
- python tools/export_model.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0_car.yml
- -o Global.pretrained_model="./output/rec_chinese_lite_v2.0_car/best_accuracy" Global.save
- _inference_dir="./output/rec_chinese_lite_v2.0_car/"
检测模型和识别模型串联验证:
python tools/infer/predict_system.py --det_algorithm="DB" --det_model_dir="./inference/ch_db_mv3_car/" --rec_model_dir=./inference/rec_chinese_lite_v2.0_car/ --image_dir="C:\Users\YY007\Desktop\car\1.jpg" --rec_char_dict_path="./ppocr/utils/ppocr_keys_car.txt" --use_gpu=True --use_angle_cls=False --det_db_unclip_ratio=1.8
到这一步,车牌号识别已经完成,通过PaddleOCR来完成车牌号识别功能,只需要改动很少的代码就可以完成识别功能,具体改动参考“基于PaddleOCR车牌号识别实现(一)”文本检测部分,大家可通过提升数据集数量或者使用别的分割网络来试试看效果,下方提供的模型开箱就可以使用。
数据集下载地址:
识别模型下载地址:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。