天天看點

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()
           

繼續閱讀