前言
本篇博客主要记录在autodl服务器中基于yolov8实现车牌检测与识别,以下记录实现全过程~
yolov8源码:GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
一、环境配置
1、第一步:配置yolov8环境
使用云服务器autoDL
二、数据集准备
数据集下载地址:https://github.com/detectRecog/CCPD
1、数据集介绍
CCPD2020数据集是一个大型的、多样化的、经过仔细标注的中国城市车牌开源数据集。CCPD数据集主要分为CCPD2019数据集和CCPD2020(CCPD-Green)数据集。CCPD2019数据集车牌类型仅有普通车牌(蓝色车牌),CCPD2020数据集车牌类型仅有新能源车牌(绿色车牌)。CCPD数据集没有专门的标注文件,每张图片的文件名就是该图像对于的数据标注。
数据集图片命名规则:
025-95_113-154&383_386&473-386&473_177&454_154&383_363&402-0_0_22_27_27_33_16-37-15.jpg1. 025:车牌区域占整个画面的比例;
2. 95_113: 车牌水平和垂直角度, 水平95°, 竖直113°
3. 154&383_386&473:标注框左上、右下坐标,左上(154, 383), 右下(386, 473)
4. 86&473_177&454_154&383_363&402:标注框四个角点坐标,顺序为右下、左下、左上、右上
5. 0_0_22_27_27_33_16:车牌号码映射关系如下: 第一个0为省份 对应省份字典provinces中的’皖’,;第二个0是该车所在地的地市一级代码,对应地市一级代码字典alphabets的’A’;后5位为字母和文字, 查看车牌号ads字典,如22为Y,27为3,33为9,16为S,最终车牌号码为皖AY339S
省份:[“皖”, “沪”, “津”, “渝”, “冀”, “晋”, “蒙”, “辽”, “吉”, “黑”, “苏”, “浙”, “京”, “闽”, “赣”,
“鲁”, “豫”, “鄂”, “湘”, “粤”, “桂”, “琼”, “川”, “贵”, “云”, “藏”, “陕”, “甘”, “青”, “宁”,
“新”]地市:[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘P’, ‘Q’,
‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’,‘X’, ‘Y’, ‘Z’]车牌字典:[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘P’,
‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’,‘Y’, ‘Z’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’,
‘6’, ‘7’, ‘8’, ‘9’]
CCPD2019数据集包含将近30万张图片、图片尺寸为720x1160x3,共包含8种类型图片,每种类型、数量及类型说明如下表:
2、数据集制作
第一步:新建datasets目录,将数据集上传
第二步:解压数据集,根据以下目录结构制作训练集、验证集和测试集:
代码如下:
import shutil
import cv2
import osdef txt_translate(path, txt_path):print(path)print(txt_path)for filename in os.listdir(path):# print(filename)list1 = filename.split("-", 3) # 第一次分割,以减号'-'做分割subname = list1[2]list2 = filename.split(".", 1)subname1 = list2[1]if subname1 == 'txt':continuelt, rb = subname.split("_", 1) # 第二次分割,以下划线'_'做分割lx, ly = lt.split("&", 1)rx, ry = rb.split("&", 1)width = int(rx) - int(lx)height = int(ry) - int(ly) # bounding box的宽和高cx = float(lx) + width / 2cy = float(ly) + height / 2 # bounding box中心点img = cv2.imread(path + filename)if img is None: # 自动删除失效图片(下载过程有的图片会存在无法读取的情况)print(path + filename)os.remove(path + filename)continuewidth = width / img.shape[1]height = height / img.shape[0]cx = cx / img.shape[1]cy = cy / img.shape[0]txtname = filename.split(".", 1)txtfile = txt_path + txtname[0] + ".txt"# 绿牌是第0类,蓝牌是第1类with open(txtfile, "w") as f:f.write(str(0) + " " + str(cx) + " " + str(cy) + " " + str(width) + " " + str(height))if __name__ == '__main__':# det图片存储地址trainDir = r"/root/ultralytics/datasets/CCPD2020/ccpd_green/train/"validDir = r"/root/ultralytics/datasets/CCPD2020/ccpd_green/val/"testDir = r"/root/ultralytics/datasets/CCPD2020/ccpd_green/test/"# det txt存储地址train_txt_path = r"/root/ultralytics/datasets/platedata/labels/train/"val_txt_path = r"/root/ultralytics/datasets/platedata/labels/val/"test_txt_path = r"/root/ultralytics/datasets/platedata/labels/test/"txt_translate(trainDir, train_txt_path)txt_translate(validDir, val_txt_path)txt_translate(testDir, test_txt_path)
制作之后文件夹结构:
3、配置文件修改
第一步:在/root/ultralytics/ultralytics/cfg/datasets下复制COCO128数据集,重命名为CCPD2020.yaml
更改内容如下:
# Ultralytics YOLO 🚀, AGPL-3.0 license
# COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/detect/coco/
# Example usage: yolo train data=coco128.yaml
# parent
# ├── ultralytics
# └── datasets
# └── coco128 ← downloads here (7 MB)# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
#path: ../datasets/coco128 # dataset root dir
train: /root/ultralytics/datasets/platedata/images/train # train images (relative to 'path') 128 images
val: /root/ultralytics/datasets/platedata/images/val # val images (relative to 'path') 128 images
test: /root/ultralytics/datasets/platedata/images/test # test images (optional)# Classes
names:0: license_plate# Download script/URL (optional)
#download: https://ultralytics.com/assets/coco128.zip
第二步:将/root/ultralytics/ultralytics/cfg/models/v8/yolov8.yaml下的nc改为1
第三步:修改/root/ultralytics/train_v8.py文件,更改内容如下:
4、开始训练
运行python train_v8.py
训练结果: