数据集标签格式转成YOLOv5要求的格式

import os
# label = ['Folding_Knife', 'Straight_Knife','Scissor','Utility_Knife','Multi-tool_Knife']
missed = 'data/'
H , W = 956, 1225
files = os.listdir(missed)
print(len(files))
for file in files:
    with open(missed + file, 'r+') as f1:
        boxes = f1.readlines()
        for box in boxes[:]:  # 注意这个地方
            box = box.split()
            if box[1] == "Folding_Knife":
                boxes = ["0"]
            elif box[1] == "Straight_Knife":
                boxes = ["1"]
            elif box[1] == "Scissor":
                boxes = ["2"]
            elif box[1] == "Utility_Knife":
                boxes = ["3"]
            elif box[1] == "tool_Knife":
                boxes = ["4"]
            else:
                break
            factor_H, factor_W = 1 / H, 1 / W
            xmin, ymin, xmax, ymax = float(box[2]), float(box[3]), float(box[4]), float(box[5])
            x = ((xmax - xmin) / 2 + xmin) * factor_W
            boxes.append(str(x))
            y = ((ymax - ymin) / 2 + ymin) * factor_H
            boxes.append(str(y))
            w = (xmax - xmin) * factor_W
            boxes.append(str(w))
            h = (ymax - ymin) * factor_H
            boxes.append(str(h))
            boxes = " ".join(boxes)
            f1.seek(0)
            f1.truncate()
            f1.writelines(boxes)
            print(file)

OPIXray数据集 改写YOLOv5标签格式,欢迎指正!

原格式 

数据集标签格式转成YOLOv5要求的格式_第1张图片

修改格式

数据集标签格式转成YOLOv5要求的格式_第2张图片

 

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