制作pascal voc格式的自己的数据集

一、介绍VOC2007格式

1、VOC2007文件夹中有以下5个文件夹:
(1)Annotations文件夹
该文件下存放的是xml格式的标签文件,每个xml文件都对应于JPEGImages文件夹的一张图片。
(2)JPEGImages文件夹
该文件夹下存放的是数据集所有的图片。
(3)ImageSets文件夹
该文件夹下存放了三个文件,分别是Layout、Main、Segmentation。在这里我们只用Main文件,其他两个暂且不管。

以下两个文件夹是与图像分割相关的。不用管。

(4)SegmentationClass文件
(5)SegmentationObject文件。

2、Main文件夹下包含了每个类别的train.txt、val.txt、trainval.txt
train.txt文件存放了训练使用的数据

val.txt文件存放了验证使用的数据

trainval.txt文件是train.txt和val.txt两个文件的合并

(train.txt和val.txt两个文件没有交集)

二、开始制作

1、

按照上图格式,新建一个VOCdevkit文件夹,在里面再新建三个文件夹,分别为:Annotations、ImageSets、JPEGImages,其中,在ImageSets文件夹中新建一个Main文件夹。

2、将数据集中所有的图片全部放到JPEGImages文件夹中。(注:图片格式需为JPEG或者JPG格式)

然后将所有图片的名字修改成000001.jpg这样统一的格式。修改代码如下:

# -*- coding:utf8 -*-
import os

class BatchRename():

    def __init__(self):
        self.path = './VOCdevkit/Annotations'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 1
        n = 6
        for item in filelist:
            if item.endswith('.jpg'):
                n = 6 - len(str(i))
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(0) * n + str(i) + '.jpg')
                try:
                    os.rename(src, dst)
                    print('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except:
                    continue
        print('total %d to rename & converted %d imgs' % (total_num, i))
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

3、如果还没有进行图片标注,这里需要先使用标注工具进行标注。(最好是还没标注,不然xml文件也得全部进行重命名,可能出现图片和xml文件不对应的情况。)

这里要提醒大家,在标注过程中一定要严格:

①标注文件(图片,xml,标注工具等)存放路径一定不要出现中文。

②标签一定不要出现中文。最好用小写母。‘-’最好使用‘_’代替。

③xml文件用utf-8编码。

需要标注的图片在JPEGImages文件中,保存xml的文件夹选择为Annotations。

4、接下来制作ImageSets/Main文件下的四个txt文件(train.txt、val.txt、trainval.txt、test.txt)。

test.txt:测试集 
train.txt:训练集 
val.txt:验证集 
trainval.txt:训练和验证集

生成四个txt文件的代码如下:(路径以及比例需要自己根据自己的情况设置)

# 数据集划分
import os
import random

root_dir = './VOCdevkit'

## 0.7train 0.1val 0.2test
trainval_percent = 0.8
train_percent = 0.7
xmlfilepath = root_dir + 'Annotations'
txtsavepath = root_dir + 'ImageSets/Main'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)  # 100
list = range(num)
tv = int(num * trainval_percent)  # 80
tr = int(tv * train_percent)  # 80*0.7=56
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open(root_dir + 'ImageSets/Main/trainval.txt', 'w')
ftest = open(root_dir + 'ImageSets/Main/test.txt', 'w')
ftrain = open(root_dir + 'ImageSets/Main/train.txt', 'w')
fval = open(root_dir + 'ImageSets/Main/val.txt', 'w')
for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftrainval.write(name)
        if i in train:
            ftrain.write(name)
        else:
            fval.write(name)
    else:
        ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

***********************后期有任何问题还会随时更新**********************

发布了75 篇原创文章 · 获赞 9 · 访问量 9562

猜你喜欢

转载自blog.csdn.net/ly_twt/article/details/105772354
今日推荐