voc数据格式转yolo格式

1.voc转yolo、 数据集切分

# 缺陷坐标xml转txt
import os
import xml.etree.ElementTree as ET
import os
import random
import cv2

# classes = ["背景","边异常","角异常","白色点瑕疵","浅色块瑕疵","深色点块瑕疵","光圈瑕疵"]  # 输入类别名称,必须与xml标注名称一致
# classes1 = ["0","1","2"]
classes2 = ["normal", "abnormal"]

# def convert(size, box):
#     print(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)

def convert(size, box):
    print(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, 0.5, w, 1.0)



def convert_annotation(image_id):
    print(image_id)
    in_file = open(r'./data/Annotations/%s' % (image_id), 'rb')  # 读取xml文件路径
    out_file = open('./data/labels/%s.txt' % (image_id.split('.')[0]), 'w')  # 需要保存的txt格式文件路径
    tree = ET.parse(in_file)
    root = tree.getroot()
    # size = root.find('size')
    # w = int(size.find('width').text)
    # h = int(size.find('height').text)
    img_=cv2.imread(os.path.join('./data/images/', image_id.replace('xml', 'jpg')))
    w, h = img_.shape[1], img_.shape[0]


    for obj in root.iter('object'):
        cls = obj.find('name').text
        # if cls not in classes:
        #     continue
        # cls_id = classes.index(cls)
        # if cls in classes1:
        #     cls_id = classes1.index(cls)
        if cls in classes2:
            cls_id = classes2.index(cls)

        xmlbox = obj.find('bndbox')
        b = [float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text)]
        #判断框是否出界
        if b[0]>=w or b[2]>=h:
            continue
        if b[1]>=w:
            b[1]=w
        if b[3]>=h:
            b[3]=h

        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


image_ids_train = os.listdir('./data/Annotations')  # 读取xml文件名索引
for image_id in image_ids_train:
   print(image_id)
   convert_annotation(image_id)


trainval_percent = 0.2  # 可自行进行调节
train_percent = 1
xmlfilepath = './data/images'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftest = open('./data/test.txt', 'w')
ftrain = open('./data/train.txt', 'w')

for i in list:
    name = total_xml[i] + '\n'
    if i in trainval:
        if i in train:
            # ftest.write('data/images/' + name)
            ftest.write('data/images/'+name)
    else:
        # ftrain.write('data/images/' + name)
        ftrain.write('data/images/'+name)
ftrain.close()
ftest.close()

2、自定义yaml

train: ./data/train.txt  # 上面我们生成的train,根据自己的路径进行更改
val: ./data/test.txt  # 上面我们生成的test

# number of classes
nc: 1   #训练的类别

# class names
names: ["normal"]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抱枕无忧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值