制作自己的VOC2007格式数据

本质就是建立三个文件夹,Annotation用于存放xml标记文件,JPEGImages用于存放image数据,ImageSets文件夹下的Main文件夹存放train.txt,val.txt,trainval.txt,test.txt四个list。代码如下:

import os

def dir2txt(dir, txt):
    files = os.listdir(dir)
    fw = open(txt, 'w')
    for file in files:
        line = file.strip().split('.')[0]+'\n'
        fw.writelines(line)
        print line
    fw.close()

def txt2xml(txt, xml):
    import cv2

    s1 = """    <object>
        <name>{0}</name>
        <pose>Unspecified</pose>
        <truncated>{1}</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>{2}</xmin>
            <ymin>{3}</ymin>
            <xmax>{4}</xmax>
            <ymax>{5}</ymax>
        </bndbox>
    </object>"""
    s2 = """<annotation>
    <folder>VisDrone</folder>
    <filename>{0}</filename>
    <source>
        <database>VisDrone</database>
        <annotation>VisDrone</annotation>
        <image>VisDrone</image>
        <flickrid>NULL</flickrid>
    </source>
    <owner>
        <flickrid>NULL</flickrid>
        <name>VisDrone</name>
    </owner>
    <size>
        <width>{1}</width>
        <height>{2}</height>
        <depth>{3}</depth>
    </size>
    <segmented>0</segmented>{4}
</annotation>
"""

    key_value = ['ignored', 'pedestrian', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor', 'others']

    text_ = txt
    flabel = open(text_, 'r')
    lb = flabel.readlines()
    flabel.close()
    ob2 = ""
    for i in range(len(lb)):
        y2 = lb[i].split(',')
        y3 = [int(i) for i in y2[:4]]
        y3[2] = y3[2]+y3[0]
        y3[3] = y3[3]+y3[1]
        cls = key_value[int(y2[5])]
        trn = y2[6]
        ob2 += '\n' + s1.format(cls, trn, y3[0], y3[1], y3[2], y3[3])
    imgname = text_.strip().split('.')[0].split('/')[-1] + '.jpg'
    imgpath = text_.strip().split('annotations')[0]+'images/'+imgname
    img = cv2.imread(imgpath)
    height, width, depth = img.shape
    savename = xml
    f = open(savename, 'w')
    ob1 = s2.format(imgname, width, height, depth, ob2)
    f.write(ob1)
    f.close()

if __name__ == '__main__':

    '''Write the txt'''
    # dir = '/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-val/images/'
    # txt = '/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/val.txt'
    # dir2txt(dir, txt)
    # dir = '/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-train/images/'
    # txt = '/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/train.txt'
    # dir2txt(dir, txt)
    # dir = '/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-test-challenge/images/'
    # txt = '/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/test.txt'
    # dir2txt(dir, txt)
    # fw = open('/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/trainval.txt','w')
    # f1 = open('/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/val.txt','r')
    # f2 = open('/data/yutingzhao/VisDronedevkit/VisDrone/ImageSets/Main/train.txt','r')
    # for line in f1.readlines():
    #     fw.writelines(line)
    # f1.close()
    # for line in f2.readlines():
    #     fw.writelines(line)
    # f2.close()
    # fw.close()

    '''Write the xml'''
    xml_fold = "/data/yutingzhao/VisDronedevkit/VisDrone/Annotations/"

    train_fold = os.listdir("/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-train/annotations/")
    for txt in train_fold:
        xml = xml_fold+txt.strip().split('.')[0]+'.xml'
        txt = "/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-train/annotations/"+txt
        txt2xml(txt, xml)
        print xml

    # val_fold = os.listdir("/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-val/annotations/")
    # for txt in val_fold:
    #     xml = xml_fold+txt.strip().split('.')[0]+'.xml'
    #     txt = "/data/yutingzhao/VisDronedevkit/VisDrone/VisDrone2018-DET-val/annotations/"+txt
    #     txt2xml(txt, xml)
    #     print xml
需要按照自己的数据集自行更改。
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VOC格式数据集的制作涉及到对图像和标注信息的整理和组织。首先,需要准备图像数据集,这些图像应该包含你感兴趣的目标物体。可以使用不同的方法来获取这些图像,包括从互联网上下载或者自己采集。然后,需要对图像进行标注,标注的方式可以是使用矩形框标注物体的位置或者进行像素级的标注。接下来,需要将图像和标注信息整理成VOC格式数据集。 VOC格式数据集的文件夹结构如下所示: - VOCdevkit/ - VOC2007/ 或者 VOC2012/ (根据你使用的数据集年份选择一个) - Annotations/ (包含所有图像的标注XML文件) - ImageSets/ (包含用于训练、验证和测试的图像索引文件) - Main/ (包含训练、验证和测试集的图像索引文件) - JPEGImages/ (包含所有的图像文件) 在VOC格式数据集中,每个图像都有一个对应的XML文件,用于存储标注信息。XML文件包含物体的类别、边界框的坐标等信息。图像索引文件用于指示哪些图像用于训练、验证和测试。 要制作VOC格式数据集,你需要按照上述的文件夹结构组织你的数据集。对于每个图像,你需要创建一个对应的XML文件来存储标注信息。在XML文件中,你需要指定物体的类别和边界框的位置。 为了训练和测试模型,还需要创建图像索引文件,将图像分为训练、验证和测试集。 在制作VOC格式数据集时,需要保持文件格式和组织形式的统一,以确保后续的代码处理的统一性。可以根据自己的需求和任务对数据集进行定制和重构。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【机器学习】 - 目标检测 - VOC格式数据集介绍与自己制作](https://blog.csdn.net/qq_41289920/article/details/105940011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值