-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrepareData.py
70 lines (51 loc) · 1.82 KB
/
PrepareData.py
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import os
import shutil
def createDir(data_dir):
if not os.path.isdir(data_dir + '/' + 'images'):
os.makedirs(data_dir + '/' + 'images')
if not os.path.isdir(data_dir + '/' + 'labels'):
os.makedirs(data_dir + '/' + 'labels')
testFile = 'TestSet1.txt'
data_dir = './SiftFlow_Oct30'
output_dir = './SiftFlow_Oct30_new'
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
train_dir = output_dir + '/' + 'train'
val_dir = output_dir + '/' + 'validation'
if not os.path.isdir(train_dir):
os.makedirs(train_dir)
if not os.path.isdir(val_dir):
os.makedirs(val_dir)
createDir(train_dir)
createDir(val_dir)
img_dir = '%s/images'% data_dir
labels_dir = '%s/labels'% data_dir
f = open(testFile)
lines = [line.rstrip('\n') for line in f]
testFilename = [filename.split('\\')[1] for filename in lines]
files = os.listdir(img_dir)
filenames = []
labels = []
count = -1
num_samples = len(files)
perm = np.random.permutation(num_samples-len(testFilename))
remove_idx = perm[0:56]
# Construct the list of JPEG files and labels.
for text in files:
name, _ = os.path.splitext(os.path.basename(text))
name = name + '.jpg'
src_file_path = '%s/%s' % (img_dir, text)
src_label_path = '%s/%s' % (labels_dir, text)
if name in testFilename:
dst_file_path = '%s/%s' % (val_dir + '/images', text)
dst_label_path = '%s/%s' % (val_dir + '/labels', text)
shutil.copy(src_file_path, dst_file_path)
shutil.copy(src_label_path, dst_label_path)
else:
count = count + 1
if not count in remove_idx:
dst_file_path = '%s/%s' % (train_dir + '/images', text)
dst_label_path = '%s/%s' % (train_dir + '/labels', text)
shutil.copy(src_file_path, dst_file_path)
shutil.copy(src_label_path, dst_label_path)