generate_train_data.py 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import os
import cPickle

# read class file
dd = {}
f = open('ucfTrainTestlist/classInd.txt')
for line in f.readlines():
    label, name = line.split()
    dd[name.lower()] = int(label) - 1
f.close()

C
CrossLee1 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

def generate_pkl(mode):
    # generate pkl
    path = '%s/' % mode
    savepath = '%s_pkl/' % mode
    if not os.path.exists(savepath):
        os.makedirs(savepath)

    fw = open('%s.list' % mode, 'w')
    for folder in os.listdir(path):
        vidid = folder.split('_', 1)[1]
        this_label = dd[folder.split('_')[1].lower()]
        this_feat = []
        for img in sorted(os.listdir(path + folder)):
            fout = open(path + folder + '/' + img, 'rb')
            this_feat.append(fout.read())
            fout.close()

        res = [vidid, this_label, this_feat]

        outp = open(savepath + vidid + '.pkl', 'wb')
        cPickle.dump(res, outp, protocol=cPickle.HIGHEST_PROTOCOL)
        outp.close()

        fw.write('data/%s/%s.pkl\n' % (savepath, vidid))
    fw.close()


generate_pkl('train')
generate_pkl('test')