提取自制VOC数据集中测试集的标签和图像

最新实现版本https://blog.csdn.net/qq_35153620/article/details/102657940

根据自制VOC检测数据集中ImageSets/Main/test.txt文件中的数据将测试集的标签和图像从数据集中提取出来

1.提取测试集标签文件:

将数据集的Annotations(存放标签文件夹)复制一个,重命名为train,写入anno_path

新建一个test文件夹

最后训练集的标签存放在trian文件夹中,测试集的标签存放在test中

import os
import shutil

test_path = '/data/VOCdevkit2007-car/VOC2007/ImageSets/Main/test.txt'
anno_path = '/data/VOCdevkit2007-car/VOC2007/train/'
new_path = '/data/VOCdevkit2007-car/VOC2007/test/'

def _main():
    fp = open(test_path, 'r')
    xml_list = fp.readlines()
    fp.close()
    i = 0
    for file in xml_list:
        xml_file = file.replace('\n', '')
        shutil.copyfile(anno_path + xml_file+'.xml', new_path + xml_file+'.xml')
        os.remove(anno_path + xml_file+'.xml')
        i =+ 1
        print xml_file+'.xml'
    print(i)

if __name__ == '__main__':
    _main()

 

 2.提取测试集的图像

image_path是数据集所有图像存放文件夹

new_path是测试集图像存储文件夹

import os
import shutil

train_path = '/data/VOCdevkit2007-car/VOC2007/ImageSets/Main/test.txt'
image_path = '/data/VOCdevkit2007-car/VOC2007/JPEGImages/'
new_path = '/data/VOCdevkit2007-car/VOC2007/test_image/'

def _main():
    fp = open(train_path, 'r')
    xml_list = fp.readlines()
    fp.close()
    for file in xml_list:
        filename = file.split('\n')
        print(filename[0])
        shutil.copyfile(image_path + filename[0]+'.jpg', new_path + filename[0] +'.jpg')
        #shutil.copyfile(anno_path + filename[0]+'.xml', new_anno_path + filename[0] +'.xml')
        #os.remove(anno_path + file[:9]+'.xml')
        print filename[0]+'.jpg'
    print len(xml_list)

if __name__ == '__main__':
    _main()

 

你可能感兴趣的:(深度学习,目标检测)