YOLOv5训练CCPD2019数据集 数据转化


本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

数据集

CCPD: Chinese City Parking Dataset
链接:https://pan.baidu.com/s/17JQL6-ckiNK9eZzDb1rWZA?pwd=r2az 
提取码:r2az 

  • ccpd_base.zip: contains 1000 pictures which are taken from different perspectives and different distances, under different illuminations and in different.

  • ccpd_blur.zip: 模糊 contains 1000 pictures where pictures are blurred largely.

  • ccpd_challenge.zip: 挑战 contains 1000 pictures which is the most difficult benchmark for LPDR algorithm.

  • ccpd_characters.zip: 没看到是啥 contains numerical and character images which is designed for training neural networks to recognize segmented character images.

  • ccpd_db.zip: 很亮或者很暗的环境 contains 1000 pictures where illuminations on the LP area are dark or extremely bright.

  • ccpd_fn.zip: 很近或者很远的环境 contains 1000 pictures where the distance from the LP to the shooting location is relatively far or very near.

  • ccpd_np.zip: 有车缺没有车牌 contains 1000 pictures where the car in the picture dose not own a LP.

  • ccpd_rotate.zip: contains 1000 pictures with great horizontal tilt degree.

  • ccpd_tilt.zip: contains 1000 pictures with both relatively great horizontal tilt degree and vertical tilt degree.

  • ccpd_weather.zip: contains 1000 pictures which are taken in rainy weather.

CCPD2019 数据格式转到 YOLO 格式

图片名就是标签:

转化程序:

# coding:utf-8
import os
import os.path
import re
import shutil

import cv2
from tqdm import tqdm

def listPathAllfiles(dirname):
    result = []
    for maindir, subdir, file_name_list in os.walk(dirname):
        for filename in file_name_list:
            apath = os.path.join(maindir, filename)
            result.append(apath)
    return result

if __name__ == '__main__':
    data_path = r'E:\train_data\number_plate'  # 数据集在哪里需要填写
    save_path = r'E:\detection\15carplate\ccpd'  # 程序会存到这个路径需要填写

    images_save_path = os.path.join(save_path, "images")
    labels_save_path = os.path.join(save_path, "labels")

    if not os.path.exists(images_save_path): os.makedirs(images_save_path)
    if not os.path.exists(labels_save_path): os.makedirs(labels_save_path)

    images_files = listPathAllfiles(data_path)

    cnt = 1
    for name in tqdm(images_files):
        if name.endswith(".jpg") or name.endswith(".png"):
            img = cv2.imread(name)
            height, width = img.shape[0], img.shape[1]

            str1 = re.findall('-\d+\&\d+_\d+\&\d+-', name)[0][1:-1]
            str2 = re.split('\&|_', str1)
            x0 = int(str2[0])
            y0 = int(str2[1])
            x1 = int(str2[2])
            y1 = int(str2[3])

            x = round((x0 + x1) / 2 / width, 6)
            y = round((y0 + y1) / 2 / height, 6)
            w = round((x1 - x0) / width, 6)
            h = round((y1 - y0) / height, 6)

            txtfile = os.path.join(labels_save_path, "plate_" + str(cnt).zfill(6) + ".txt")
            imgfile = os.path.join(images_save_path,
                                   "plate_" + str(cnt).zfill(6) + "." + os.path.basename(name).split(".")[-1])

            open(txtfile, "w").write(" ".join(["0", str(x), str(y), str(w), str(h)]))
            shutil.move(name, imgfile)  # 移动文件到别的位置,且重命名

            cnt += 1

检查 yolo labels 对不对

import os

import cv2
import matplotlib.pyplot as plt
import numpy as np

ASSETS_DIRECTORY = "assets"
plt.rcParams["savefig.bbox"] = "tight"

def listPathAllfiles(dirname):
    result = []
    for maindir, subdir, file_name_list in os.walk(dirname):
        for filename in file_name_list:
            apath = os.path.join(maindir, filename)
            result.append(apath)
    return result

if __name__ == '__main__':
    labelspath = r'E:\detection\15carplate\ccpd\labels'
    imagespath = r'E:\detection\15carplate\ccpd\images'

    labelsFiles = listPathAllfiles(labelspath)

    for lbf in labelsFiles[::-1]:
        labels = open(lbf, "r").readlines()
        labels = list(map(lambda x: x.strip().split(" "), labels))
        imgfileName = os.path.join(imagespath, os.path.basename(lbf)[:-4] + ".jpg")
        img = cv2.imdecode(np.fromfile(imgfileName, dtype=np.uint8), 1)  # img是矩阵

        for lbs in labels:
            lb = list(map(float, lbs))[1:]
            x1 = int((lb[0] - lb[2] / 2) * img.shape[1])
            y1 = int((lb[1] - lb[3] / 2) * img.shape[0])
            x2 = int((lb[0] + lb[2] / 2) * img.shape[1])
            y2 = int((lb[1] + lb[3] / 2) * img.shape[0])
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 5)

        ratio = 600 / min(img.shape[0:2])
        img = cv2.resize(img, dsize=(int(img.shape[1] * ratio), int(img.shape[0] * ratio)))

        cv2.imshow("1", img)
        cv2.waitKey()
        cv2.destroyAllWindows()

训练 yolov5

写 data yaml

path: E:\detection\15carplate  # dataset root dir
train: ccpd
val: ccpd

# Classes
nc: 1  # number of classes
names: [ 'plate' ]  # class names

开始训练:

python train.py --batch-size 4 --data ccpd.yaml --img 640 --epochs 10 --weight weights/yolov5m.pt 

声明:HEUE NOTE|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA 4.0协议进行授权

转载:转载请注明原文链接 - YOLOv5训练CCPD2019数据集 数据转化