CCPD数据集处理(目标检测和文本识别)


前言

  最近,用到了CCPD数据集,但是开源的CCPD数据集并不是拿来就能用的,所以自己就参考图片格式讲解写了个脚本用来生成支持目标检测和文本识别的label文件。以下均已CCPD2019为例。


一、CCPD数据集介绍

  CCPD数据集主要采集于安徽某停车场一段时间内的数据,所有图片尺寸固定为720×1160(w×h),大约包含25w+的各种场景图片,如下图所示:
在这里插入图片描述
同时,也给我们基于CCPD-Base切分好了训练集、测试集、验证集,文件路径存放在splits下,如图所示:
在这里插入图片描述
剩余几个文本对应的是各个场景下图片的相对路径,方便我们自己去划分,建议大家根据自己的需要进行数据集的划分。

二、下载地址

  下载地址为:CCPD

三、格式解读

  图片解压完成后是这个样子,
在这里插入图片描述
我们可以看到这里面的文件名都是一堆数字和符号组成,那这些都代表什么呢?我们就以第一个文件名:“01-86_91-298&341_449&414-458&394_308&410_304&357_454&341-0_0_14_28_24_26_29-124-24.jpg”为例,按照“-”将这些符号和数字分开,每一段代表一个意思:
 1、01:车牌占整个界面比例;(一般没用,可忽略)
 2、86_91:车牌的水平角度和垂直角度
 3、298&341_449&414:车牌标注框左上角和右下角的坐标
 4、458&394_308&410_304&357_454&341:车牌四个顶点的坐标,顺序为右下、左下、左上、右上
 5、0_0_14_28_24_26_29:这个代表着和省份(第一位)、地市(第二位)、车牌号(剩余部分)的映射关系
 6、124:亮度,值越大亮度越高(仅供参考)
 7、24:模糊度,值越小越模糊(仅供参考)

四、生成对应lable

  这一趴我们直接上代码,需要自己在代码中指定自己的路径,如图片文件路径和生成txt的保存路径;其次,还需要根据自己的任务打开对应的开关,如车牌定位便将use_landmarks设置为True,其他类似:

import os
import glob
import cv2
from pathlib import Path
import numpy as np

root_dir=""
txt_dir=""
use_landmarks=False #用于车牌定位,如MTCNN、YOLO5Face
use_xywh=False #用于车牌检测,如YOLO等
use_ocr=False #用于识别
os.makedirs(txt_dir,exist_ok=True)

#返回中心点坐标、宽、高以及四个顶点的位置信息共12个信息,可用于mtcnn或yolo5Face检测定位车牌
def ccpd_landmarks(points_list,imgw,imgh):
    '''
    Args:
        points_list: 车牌四个顶点的位置信息
        imgw: 图片宽度
        imgh: 图片高度
    Returns:
    '''
    annotation=np.zeros((1,12))
    points_list_=[]
    #得到四个端点
    for points in points_list.split("_"):
        points_=list(map(int,points.split("&")))
        points_list_.append(points_)
    points_list_=np.array(points_list_)

    #获的box的坐标
    xmin=min(points_list_[:,0])
    xmax=max(points_list_[:,0])
    ymin=min(points_list_[:,1])
    ymax=max(points_list_[:,1])
    dw=1./imgw
    dh=1./imgh
    w,h=xmax-xmin,ymax-ymin
    #生成对应的信息列表
    annotation[0,0]=((xmin+xmax)/2.-1)*dw #cx
    annotation[0,1]=((ymin+ymax)/2.-1)*dh #cy
    annotation[0,2]=w*dw #w
    annotation[0,3]=h*dh #h
    annotation[0,4]=points_list_[2][0]/imgw #左上x
    annotation[0,5]=points_list_[2][1]/imgh #左上y
    annotation[0,6]=points_list_[3][0]/imgw #右上x
    annotation[0,7]=points_list_[3][1]/imgh #右上y
    annotation[0,8]=points_list_[0][0]/imgw #右下x
    annotation[0,9]=points_list_[0][1]/imgh #右上y
    annotation[0,10]=points_list_[1][0]/imgw #左下x
    annotation[0,11]=points_list_[1][1]/imgh #左下y
    return annotation


#仅返回图片的中心点坐标以及宽高信息,可用于YOLO系列检测车牌
def ccpd_xywh(points_list,imgw,imgh):
    '''
        Args:
            points_list: 车牌四个顶点的位置信息
            imgw: 图片宽度
            imgh: 图片高度
        Returns:
        '''
    annotation = np.zeros((1, 4))
    points_list_ = []
    # 得到四个端点
    for points in points_list.split("_"):
        points_ = list(map(int, points.split("&")))
        points_list_.append(points_)
    points_list_ = np.array(points_list_)

    #获的box的坐标
    xmin=min(points_list_[:,0])
    xmax=max(points_list_[:,0])
    ymin=min(points_list_[:,1])
    ymax=max(points_list_[:,1])
    dw=1./imgw
    dh=1./imgh
    w,h=xmax-xmin,ymax-ymin
    # 生成对应的信息列表
    annotation[0, 0] = ((xmin + xmax) / 2. - 1) * dw  # cx
    annotation[0, 1] = ((ymin + ymax) / 2. - 1) * dh  # cy
    annotation[0, 2] = w * dw  # w
    annotation[0, 3] = h * dh  # h
    return annotation

#返回对应的文本信息,可用于ocr识别车牌
def ccpd_ocr(plate):
    '''
    Args:
        plate: 文件名中的数字
    Returns:
    '''
    #车牌字典
    #省
    provinces = ["皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
                 "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "警", "学", "O"]
    #具体信息
    word_lists = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                 'W','X', 'Y', 'Z', 'O', '1', '2', '3', '4', '5', '6', '7', '8', '9','0']


    label_list=plate.split("_")
    print(len(label_list))
    result=""
    result+=provinces[int(label_list[0])]
    result+=word_lists[int(label_list[1])]
    result+=word_lists[int(label_list[2])]+word_lists[int(label_list[3])]+word_lists[int(label_list[4])]+\
            word_lists[int(label_list[5])]+word_lists[int(label_list[6])]
    return result


def ccpd2label(all_list):
    for imgpath in all_list:
        img=cv2.imread(imgpath)
        h,w=img.shape[:2]

        imgname=Path(imgpath).name
        points_list,plate_list=imgname.split("-")[3],imgname.split("-")[4]#四个关键点的坐标[右下、左下、左上、右上],车牌号映射表
        if use_landmarks:
            annotation=ccpd_landmarks(points_list,w,h) #获得要写入txt的信息
            txtname = imgname.replace(".jpg", ".txt")
            txt_path = os.path.join(txt_dir, txtname)
            str_label = "0"  # 类别默认为0
            with open(txt_path, "a+") as fw:
                for i in range(len(annotation[0])):
                    str_label = str_label + " " + str(annotation[0][i])
                fw.write(str_label)
        elif use_xywh:
            annotation=ccpd_xywh(points_list,w,h) #获得要写入txt的信息
            txtname = imgname.replace(".jpg", ".txt")
            txt_path = os.path.join(txt_dir, txtname)
            str_label = "0"  # 类别默认为0
            with open(txt_path, "a+") as fw:
                for i in range(len(annotation[0])):
                    str_label = str_label + " " + str(annotation[0][i])
                fw.write(str_label)
        elif use_ocr:
            ocr_label=ccpd_ocr(plate_list)
            txtname=imgname.replace(".jpg",".txt")
            txt_path=os.path.join(txt_dir,txtname)
            with open(txt_path,"a+") as fw:
                fw.write(ocr_label)

if __name__ == '__main__':
    image_list=glob.glob(root_dir+os.sep+"*.jpg")
    ccpd2label(image_list)

参考博客:https://blog.csdn.net/luohenyj/article/details/117752120


总结

以上就是本篇文章的全部内容,如有疑问,欢迎评论去交流或加入QQ群:995760755交流。

  • 5
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值