赞
踩
最近在做关于字符识别的项目,发现好多人在问关于车牌号识别方案,了解现在最火的文字识别PaddleOCR,发现只使用PaddleOCR就可以达到很好的效果,而且从头到尾只需要动PaddOCR上少许代码,就能达到很好的效果。
PaddleOCR分为三个模型检测、方向分类和识别,车牌号识别只需用到检测和识别模型即可,而且现在车牌号识别很少能覆盖全部的车牌类型,今天我发布的这个模型将支持蓝牌、黄牌(单行)、绿牌、大型新能源(黄绿)、领使馆车牌、警牌、武警牌(单行)、军牌(单行)、港澳出入境车牌,覆盖极大部分类型的车牌。
1、建议使用PyCharm进行开发,Download PyCharm: Python IDE for Professional Developers by JetBrains
社区版即可
2、在git上下载最新版的PaddleOCR版本,Download PyCharm: Python IDE for Professional Developers by JetBrains
3、导入Pycharm中如下界面:
切换到2.3目录,执行下面的命令,安装环境:
pip install -r requirements.txt
使用PaddleOCR自带的文字检测功能进行车牌号检测训练,可以不需要加载预训练模型,我们要训练一份只需要能检测出车牌号的模型即可,文章结尾有数据集和检测模型下载地址。
目前收集了1万多张左右的真实图片,其中有几千张是手动标注的
复制一份yml文件,注:Eval.transforms.DetResizeForTest设置resize_long: 960
- Global:
- use_gpu: true
- epoch_num: 1200
- log_smooth_window: 20
- print_batch_step: 2
- save_model_dir: ./output/ch_db_mv3_car/
- save_epoch_step: 1200
- # evaluation is run every 5000 iterations after the 4000th iteration
- eval_batch_step: 1500
- cal_metric_during_train: False
- pretrained_model: ./pretrained_models/ch_ppocr_mobile_v2.0_det_train/best_accuracy
- checkpoints:
- save_inference_dir: ./inference/ch_db_mv3_car
- use_visualdl: False
- infer_img: doc/imgs_en/img_10.jpg
- save_res_path: ./output/det_db/predicts_db.txt
-
- Architecture:
- model_type: det
- algorithm: DB
- Transform:
- Backbone:
- name: MobileNetV3
- scale: 0.5
- model_name: large
- disable_se: True
- Neck:
- name: DBFPN
- out_channels: 96
- Head:
- name: DBHead
- k: 50
-
- Loss:
- name: DBLoss
- balance_loss: true
- main_loss_type: DiceLoss
- alpha: 5
- beta: 10
- ohem_ratio: 3
-
- Optimizer:
- name: Adam
- beta1: 0.9
- beta2: 0.999
- lr:
- name: Cosine
- learning_rate: 0.001
- warmup_epoch: 2
- regularizer:
- name: 'L2'
- factor: 0
-
- PostProcess:
- name: DBPostProcess
- thresh: 0.3
- box_thresh: 0.6
- max_candidates: 1000
- unclip_ratio: 1.5
-
- Metric:
- name: DetMetric
- main_indicator: hmean
-
- Train:
- dataset:
- name: SimpleDataSet
- data_dir: ./train_data/
- label_file_list:
- - "./train_data/car1/det_gt_train.txt"
- - "./train_data/car2/det_gt_train.txt"
- ratio_list: [ 1.0, 1.0 ]
- transforms:
- - DecodeImage: # load image
- img_mode: BGR
- channel_first: False
- - DetLabelEncode: # Class handling label
- - IaaAugment:
- augmenter_args:
- - { 'type': Fliplr, 'args': { 'p': 0.5 } }
- - { 'type': Affine, 'args': { 'rotate': [ -30, 30 ] } }
- - { 'type': Resize, 'args': { 'size': [ 0.3, 3 ] } }
- - EastRandomCropData:
- size: [ 960, 960 ]
- max_tries: 50
- keep_ratio: true
- - MakeBorderMap:
- shrink_ratio: 0.4
- thresh_min: 0.3
- thresh_max: 0.7
- - MakeShrinkMap:
- shrink_ratio: 0.4
- min_text_size: 8
- - NormalizeImage:
- scale: 1./255.
- mean: [ 0.485, 0.456, 0.406 ]
- std: [ 0.229, 0.224, 0.225 ]
- order: 'hwc'
- - ToCHWImage:
- - KeepKeys:
- keep_keys: [ 'image', 'threshold_map', 'threshold_mask', 'shrink_map', 'shrink_mask' ] # the order of the dataloader list
- loader:
- shuffle: True
- drop_last: False
- batch_size_per_card: 2
- num_workers: 4
-
- Eval:
- dataset:
- name: SimpleDataSet
- data_dir: ./train_data/
- label_file_list:
- - "./train_data/car/car1/det_gt_test.txt"
- - "./train_data/car/car2/det_gt_test.txt"
- ratio_list: [ 1.0, 1.0 ]
- transforms:
- - DecodeImage: # load image
- img_mode: BGR
- channel_first: False
- - DetLabelEncode: # Class handling label
- - DetResizeForTest:
- # image_shape: [736, 1280]
- resize_long: 960
- - NormalizeImage:
- scale: 1./255.
- mean: [ 0.485, 0.456, 0.406 ]
- std: [ 0.229, 0.224, 0.225 ]
- order: 'hwc'
- - ToCHWImage:
- - KeepKeys:
- keep_keys: [ 'image', 'shape', 'polys', 'ignore_tags' ]
- loader:
- shuffle: False
- drop_last: False
- batch_size_per_card: 1 # must be 1
- num_workers: 2
- use_shared_memory: False
python -m paddle.distributed.launch --gpus 0 tools/train.py -o Global.use_visualdl=True -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0_car.yml
注意显存问题,需要大于4G的显存,2G显存会溢出
visualdl --logdir=output/ch_db_mv3_car/vdl --port 8081
检测模型效果最高能到达:hmean:0.99
- python tools/infer_det.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0_car.yml -o Global.pretrained_model="./output/ch_db_mv3_car/best_accuracy" Global.inf
- er_img=C:\Users\YY007\Desktop\car\1.jpg PostProcess.unclip_ratio=1.8
效果如下,左边原图,右边检测效果图:
- python tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0_car.yml -o Global.pretrained_model="./output/ch_db_mv3_car/best_accuracy" Global.save
- _inference_dir="./inference/ch_db_mv3_car/"
导出模型验证:
python tools/infer/predict_det.py --det_algorithm="DB" --det_model_dir="./inference/ch_db_mv3_car/" --image_dir="C:\Users\YY007\Desktop\car\1.jpg" --use_gpu=True --det_db_unclip_ratio=1.8
导出模型进行预测需将这一行禁掉,否则会导致导出模型的预测结果与训练模型的预测结果出现不一致的情况
详细解释在这里:
关于文字检测训练模型和导出模型效果不一样的问题 · Issue #4371 · PaddlePaddle/PaddleOCR · GitHub
修改方案如下:
文件一:tools/infer/utility.py
添加参数如下参数:
文件二:tools/infer/predict_det.py
其他文件:修改部署预测文件,以hubserving部署为例:
deploy/hubserving/ocr_system/params.py
到这一步,车牌号检测模型就完成了,总体上讲效果还可以,如果需要更好的检测效果,可以更换更深的神经网络,或者通过语义分割来实现,下一章我们讲讲车牌号识别【基于PaddleOCR车牌号识别实现(二)】模型训练。
数据集下载地址:
检测模型下载地址:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。