CCPD开源车牌数据集生成标注文件txt

root_path为图片路径
out_file为生成 txt标注文件的保存路径
仅适用于检测车牌框(只有plate一个类)

import os
import re
import cv2
root_path = r'/home/joy/SXH/tensorflow-yolov4-tflite/data/CCPD2019/images/'
file_name = os.listdir(root_path)


def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return x, y, w, h


for image_id in file_name:
    id = image_id.split('.')[0]
    out_file = open('/home/joy/SXH/tensorflow-yolov4-tflite/data/CCPD2019/images/%s.txt' % id,
                    'w')  # 需要保存的txt格式文件路径
    img = cv2.imread((root_path + image_id))
    height = img.shape[0]
    width = img.shape[1]
    point = image_id.split('.')[0].split('-')[3]
    # Xmin = point.split('_')[2].split('&')[0]
    num = re.findall('\d+\d*', point)  # 正则表达式 从字符串中提取数值
    Xmin = min(num[0::2])  # list[start:stop:step]
    Ymin = min(num[1::2])
    Xmax = max(num[0::2])
    Ymax = max(num[1::2])
    b = (float(Xmin), float(Xmax), float(Ymin),
         float(Ymax))
    bb = convert((width, height), b)
    out_file.write('0' + " " + " ".join([str(a) for a in bb]) + '\n')
print('end')

你可能感兴趣的:(学妹的教程,深度学习,python)