车牌识别数据集(蓝牌、黄牌、绿牌)及相关转换代码

自己整理了一些用在车牌识别的数据集,已经人工一张一张的筛选过了,过滤掉模糊的图片、处理有歧义的区域,可以直接采用。
label为labelme的json格式,目标框是polygon多边形,好处是不同角度的车牌都能完全贴合,如图。
车牌识别数据集(蓝牌、黄牌、绿牌)及相关转换代码_第1张图片
整理了三类车牌,分别是蓝牌、绿牌、黄牌(直接点击即可下载)。
若是要进行yolo的目标检测训练,可用以下代码进行转换为yolo的darknet格式:

import json
import os
import shutil
import cv2
import os
from numpy.lib.twodim_base import triu_indices_from
import pandas as pd
from glob import glob
import codecs

print(cv2.__version__)


def getBoundingBox(points):              
    xmin = points[0][0]
    xmax = points[0][0]
    ymin = points[0][1]
    ymax = points[0][1]
    for p in points:
        if p[0] > xmax:
            xmax = p[0]
        elif p[0] < xmin:
            xmin = p[0]
        if p[1] > ymax:
            ymax = p[1]
        elif p[1] < ymin:
            ymin = p[1]
    return [int(xmin), int(xmax), int(ymin), int(ymax)]


def json2txt(json_path, midTxt_path):
    json_data = json.load(open(json_path))         
    img_h = json_data["imageHeight"]
    img_w = json_data["imageWidth"]
    shape_data = json_data["shapes"]
    shape_data_len = len(shape_data)
    img_name = os.path.split(json_path)[-1].split(".json")[0]    
    name = img_name + '.jpg'                            
    data = ''
    for i in range(shape_data_len):
        lable_name = shape_data[i]["label"]             
        points = shape_data[i]["points"]                
        [xmin, xmax, ymin, ymax] = getBoundingBox(points)
        if xmin <= 0:
            xmin = 0
        if ymin <= 0:
            ymin = 0
        if xmax >= img_w:  
            xmax = img_w - 1
        if ymax >= img_h:
            ymax = img_h - 1
        b = name + ' ' + lable_name + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax)
        print(b)
        data += b + '\n'
    with open(midTxt_path + '/' + img_name + ".txt", 'w', encoding='utf-8') as f:    
        f.writelines(data)          

def txt2darknet(midTxt_path, img_path):
    data = pd.DataFrame()
    filelist = os.listdir(midTxt_path) 
    for file in filelist:                                                   
        file_path = os.path.join(midTxt_path, file)
        filename = os.path.splitext(file)[0]
        imgName = filename + '.jpg'
        imgPath = os.path.join(img_path, imgName)
        # for path in img_path:
        #     imgPath = os.path.join(path, imgName)
        #     if not os.path.exists(imgPath):
        #         continue
        #     else:
        #         break
        
        if not os.path.exists(imgPath):
            imgName = filename + '.png'
            imgPath = os.path.join(img_path, imgName)
            if not os.path.exists(imgPath):
                imgName = filename + '.jpeg'
                imgPath = os.path.join(img_path, imgName)
        img = cv2.imread(imgPath)
        print(imgPath)
        [img_h, img_w, _] = img.shape
        data = ""
        with codecs.open(file_path, 'r', encoding='utf-8',errors='ignore') as f1:
            for line in f1.readlines():
                line = line.strip('\n')
                a = line.split(' ')
                if int(a[5]) - int(a[3]) <= 15 or int(a[4]) - int(a[2]) <= 15:
                    img[int(a[3]):int(a[5]), int(a[2]):int(a[4]), :] = (0,0,0)
                    continue
                if a[1] == 'other' or a[1] == 'del':
                    img[int(a[3]):int(a[5]), int(a[2]):int(a[4]), :] = (0,0,0)
                    continue
                if a[1] == 'plate_p':            # blue
                    a[1] = '0'
                elif a[1] == 'green_plate':      # green
                    a[1] = '1'
                elif a[1] == 'yellow_plate_s':   # yellow
                    a[1] = '2'

                x1 = float(a[2])
                y1 = float(a[3])
                w = float(a[4]) - float(a[2])
                h = float(a[5]) - float(a[3])

                # if w <= 15 and h <= 15: continue

                center_x = float(a[2]) + w / 2
                center_y = float(a[3]) + h / 2
                a[2] = str(center_x / img_w)
                a[3] = str(center_y / img_h)
                a[4] = str(w / img_w)
                a[5] = str(h / img_h)
                b = a[1] + ' ' + a[2] + ' ' + a[3] + ' ' + a[4] + ' ' + a[5]
                print(b)
                data += b + '\n'
        with open(saved_path + '/' + filename + ".txt", 'w', encoding='utf-8') as f2:    
            f2.writelines(data)

json_path = "/data/license_plate/blue"
midTxt_path = "/data/license_plate/blue/mid"
img_path = "/data/license_plate/blue"
saved_path = '/data/license_plate/save'

if not os.path.exists(midTxt_path):
    os.mkdir(midTxt_path)

filelist = os.listdir(json_path)                   
for file in filelist:
    old_dir = os.path.join(json_path, file)         
    if os.path.isdir(old_dir):
        continue                                    
    filetype = os.path.splitext(file)[1]            
    if(filetype != ".json"): continue               
    json2txt(old_dir, midTxt_path)

txt2darknet(midTxt_path, img_path)
shutil.rmtree(midTxt_path)

如果想要矩形rectangle形状的目标框的json格式,再用以下代码转换:

# -*- coding: utf-8 -*-
import json
import cv2
from glob import glob
import os


txt_path = '/license_plate/save/'   # darknet格式
saved_path = '/data/license_plate/json/'
img_path = '/data/license_plate/blue/images/'


files = glob(txt_path + "*.txt")               
# files = os.listdir(txt_path)
# print(files)
files = [i.split('/')[-1].split('.txt')[0] for i in files]
print(files)

for file in files:
    print(file)
    txt_file = txt_path + file + '.txt'
    img_file = img_path + file + '.jpg'
    if not os.path.exists(img_file):
        img_file = img_path + file + '.png'
        if not os.path.exists(img_file):
            img_file = img_path + file + '.jpeg'
    print(img_file)
    img = cv2.imread(img_file)
    # print(img)
    imgw = img.shape[1]
    imgh = img.shape[0]
    xi = []
    yi = []
    xa = []
    ya = []
    Label = []

    with open(txt_file, 'r') as f:             
        for line in f.readlines():
            line = line.strip('\n')            
            a = line.split(' ')  
            label = 'other'
            if a[0] == '0':
                label = 'plate_p'   
            elif a[0] == '1':
                label = 'green_plate'   
            elif a[0] == '2':
                label = 'yellow_plate_s'  
            
            Label.append(label)
            print(Label)

            centerx=float(a[1])*imgw
            centery=float(a[2])*imgh
            w=float(a[3])*imgw
            h=float(a[4])*imgh
            xmin = centerx - w/2
            xmax= centerx + w/2
            ymin= centery - h/2
            ymax = centery + h/2

            xi.append(xmin)
            yi.append(ymin)
            xa.append(xmax)
            ya.append(ymax)

    # for j in range(0, len(files)):
    labelme_formate = {
        "version": "4.2.9",
        "flags": {},
        "lineColor": [0, 255, 0, 128],
        "fillColor": [255, 0, 0, 128],
        "imagePath": os.path.split(img_file)[-1],
        "imageHeight": imgh,
        "imageWidth": imgw
    }
    labelme_formate['imageData'] = None
    shapes = []
    for i in range(0, len(xi)):
        s = {"label": Label[i], "line_color": None, "fill_color": None, "shape_type": "rectangle"}
        points = [
            [xi[i], yi[i]],
            [xa[i], ya[i]]
        ]
        s['points'] = points
        shapes.append(s)

    labelme_formate['shapes'] = shapes
    json.dump(labelme_formate, open(saved_path + file + ".json", 'w'), ensure_ascii=False, indent=2)
    print(saved_path + file + ".json")

欢迎私信一起学习交流!

你可能感兴趣的:(python,机器学习,深度学习,YOLO,人工智能)