voc数据集合转变成yolo数据集(xml_转变成txt)

第一步:获取所有类名
第二部:验证图像
第三步:写入txt

import xml.etree.ElementTree as ET
import pickle
import os
# os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
from os import listdir, getcwd

import cv2


class_name=['aa',
             'bb',
             'cc'
             ]


def convert(size, box): # size:(原图w,原图h) , box:(xmin,xmax,ymin,ymax)
    dw = 1./size[0]     # 1/w
    dh = 1./size[1]     # 1/h
    x = (box[0] + box[1])/2.0   # 物体在图中的中心点x坐标
    y = (box[2] + box[3])/2.0   # 物体在图中的中心点y坐标
    w = box[1] - box[0]         # 物体实际像素宽度
    h = box[3] - box[2]         # 物体实际像素高度
    x = x*dw    # 物体中心点x的坐标比(相当于 x/原图w)
    w = w*dw    # 物体宽度的宽度比(相当于 w/原图w)
    y = y*dh    # 物体中心点y的坐标比(相当于 y/原图h)
    h = h*dh    # 物体宽度的宽度比(相当于 h/原图h)
    return (x, y, w, h)    # 返回 相对于原图的物体中心点的x坐标比,y坐标比,宽度比,高度比,取值范围[0-1]
def cv_show(img,box,cls):

    cv2.rectangle(img, (int(box[0]), int(box[2])), (int(box[1]), int(box[3])), (255, 0, 0), 2)
    cv2.putText(img, cls, (int(box[0]), int(box[2])-5), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)

def convert_annotation(xml_path,picture_path,class_name):
    '''
    将对应文件名的xml文件转化为label文件,xml文件包含了对应的bunding框以及图片长款大小等信息,
    通过对其解析,然后进行归一化最终读到label文件中去,也就是说
    一张图片文件对应一个xml文件,然后通过解析和归一化,能够将对应的信息保存到唯一一个label文件中去
    labal文件中的格式:calss x y w h  同时,一张图片对应的类别有多个,所以对应的bunding的信息也有多个
    '''
    # 对应的通过year 找到相应的文件夹,并且打开相应image_id的xml文件,其对应bund文件
    # in_file = open('data/Annotations/%s.xml' % (image_id), encoding='utf-8')
    # print(in_file.name)
    # 准备在对应的image_id 中写入对应的label,分别为
    # <object-class> <x> <y> <width> <height>
    # out_file = open('data/labels/%s.txt' % (image_id), 'w', encoding='utf-8')
    # print(out_file.name)
    # 解析xml文件
    # img=cv2.imread(picture_path)

    tree = ET.parse(xml_path)
    # 获得对应的键值对
    root = tree.getroot()
    # 获得图片的尺寸大小
    size = root.find('size')
    # print(size)
    # 获得宽
    w = int(size.find('width').text)

    # 获得高
    h = int(size.find('height').text)
    # print(h)
    # 遍历目标obj
    image_id=xml_path.split("\\")[-1].split(".")[0]
    out_file = open('Data3/labels/%s.txt' % (image_id), 'w', encoding='utf-8')
    for obj in root.iter('object'):
        # 获得difficult ??
        # difficult = obj.find('difficult').text
        # 获得类别 =string 类型
        cls = obj.find('name').text

        # 如果类别不是对应在我们预定好的class文件中,或difficult==1则跳过

        # 通过类别名称找到id

        # if cls in class_name:
        #     continue
        # else:
        #     class_name.append(cls)
        # 找到bndbox 对象
        xmlbox = obj.find('bndbox')
        # # 获取对应的bndbox的数组 = ['xmin','xmax','ymin','ymax']
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        print( cls)
        # cv_show(img,b,cls)
        cls_id = class_name.index(cls)
        print(cls_id)
        # # 带入进行归一化操作
        # # w = 宽, h = 高, b= bndbox的数组 = ['xmin','xmax','ymin','ymax']
        bb = convert((w, h), b)
        # bb 对应的是归一化后的(x,y,w,h)
        # 生成 calss x y w h 在label文件中
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    # cv2.namedWindow('img', 0)
    # cv2.resizeWindow('img', int(img.shape[0]), int(img.shape[1]))
    # cv2.imshow('img', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    # print(class_name)
# class_name = []
for i in os.listdir("./Data3/xml"):
    xml_path=os.path.join("./Data3/xml",i)
    # convert_annotation(xml_path)
    picture_path=xml_path.split("\\")[-1].replace("xml","bmp")
    picture_path=os.path.join("./Data3/PIC",picture_path)
    convert_annotation(xml_path,picture_path,class_name)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值