Caltech 转VOC 数据集

1.利用seq文件生成jpg文件: 这一步利用python 生成的图片即为JPEGimages

2.利用vbb文件生成json文件:利用matlab脚本完成 这一步生成Annotations

3.利用json生成Main/train.txt, Main/val.txt 等,注意这一步要利用2中的JSON文件第一步生成的图片不一定包含行人 可能就没有对应的Json文件:

生成txt的python脚本:

import os
import random
 
def folder_struct(level, path):
    global allFileNum
 
    dirList = []
    fileList = []
    files = os.listdir(path)
    dirList.append(str(level))
 
    for f in files:
        if(os.path.isdir(path + '/' + f)):
            if f[0] != '.':
                dirList.append(f)
        if (os.path.isfile(path + '/' + f)):
            fileList.append(f)
 
 
    i_dl = 0
    for dl in dirList:
        if i_dl == 0:
            i_dl = i_dl + 1
        else:
            #print '-' * (int(dirList[0])), dl
            folder_struct((int(dirList[0]) + 1), path+'/'+dl)
    print dirList
    # print fileList
    # print dirList
    for fl in fileList:
        #print fl[12:17], fl[17:21]
        file_info = (fl[12:17] + '/' + fl[17:21])
        print file_info
        generate_txt(file_info)
    pass
 

# !!!!如果Annotations 下面仍有多个文件夹未合并则调用folder_struct
def generate_txt():
    trainval_percent = 0.66
    train_percent = 0.5
    folder_root = '/data/CaltechVOC/'
    xmlfilepath = folder_root + 'Annotations/'
    txtsavepath = folder_root + 'ImageSets/Main'
    try:
        total_xml = os.listdir(xmlfilepath)
    #print total_xml
 
        num = len(total_xml)
 
        #print num
        list = range(num)
        tv = int(num * trainval_percent)
        tr = int(tv * train_percent)
        trainval = random.sample(list, tv)
        train = random.sample(trainval, tr)
 
        ftrainval = open(folder_root + 'ImageSets/Main/trainval.txt', 'aw')
        ftest     = open(folder_root + 'ImageSets/Main/test.txt'    , 'aw')
        ftrain    = open(folder_root + 'ImageSets/Main/train.txt'   , 'aw')
        fval      = open(folder_root + 'ImageSets/Main/val.txt'     , 'aw')
        
        # 这里是不用的
        folder_name = xmlfilepath[-10:] + '/'
 
        print folder_name
        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()
    except Exception:
        pass

generate_txt()

 

你可能感兴趣的:(机器视觉)