【目标检测】CCRSDB数据集标注处理(转YOLO)

 CSUST Chinese Traffic Sign Detection Benchmark

交通标识数据集转YOLO格式

# 0 warning
# 1 mandatory
# 2 prohibitory
import cv2

with open("GroundTruth.txt", "r") as f:
    img_name1 = None
    for line in f.readlines():
        line = line.strip('\n') 
        line = line.split(';')
        # print(line)
        img_name = line[0]
        x1 = int(float(line[1]))
        y1 = int(float(line[2]))
        x2 = int(float(line[3]))
        y2 = int(float(line[4]))
        label = line[5]

        name = img_name.split('.')
        name = name[0]
        if label == "warning":
            label = 0
        elif label == "mandatory":
            label = 1
        elif label == "prohibitory":
            label = 2
        print(name, label)

        img = cv2.imread(f"Images/{name}.png")
        sp = img.shape
        h = sp[0]
        w = sp[1]

        x_ = (x1 + x2) / (2 * w)
        y_ = (y1 + y2) / (2 * h)
        w_ = (x2 - x1) / w
        h_ = (y2 - y1) / h

        strcontent = f'{label} {x_} {y_} {w_} {h_}'
        print(strcontent)

        f = open(f"labels/{name}.txt", 'a')
        f.write(strcontent)
        f.write('\n')

你可能感兴趣的:(目标检测,python)