-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathucf_move_files.py
85 lines (70 loc) · 2.61 KB
/
ucf_move_files.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
After extracting the RAR, we run this to move all the files into
the appropriate train/test folders.
Should only run this file once!
"""
import os
import os.path
from shutil import copyfile
def get_train_test_lists(version='01'):
"""
Using one of the train/test files (01, 02, or 03), get the filename
breakdowns we'll later use to move everything.
"""
# Get our files based on version.
test_file = './ucfTrainTestlist/testlist' + version + '.txt'
train_file = './ucfTrainTestlist/trainlist' + version + '.txt'
# Build the test list.
with open(test_file) as fin:
test_list = [row.strip() for row in list(fin)]
# Build the train list. Extra step to remove the class index.
with open(train_file) as fin:
train_list = [row.strip() for row in list(fin)]
train_list = [row.split(' ')[0] for row in train_list]
# Set the groups in a dictionary.
file_groups = {
'train': train_list,
'test': test_list
}
return file_groups
def move_files(file_groups):
"""This assumes all of our files are currently in _this_ directory.
So move them to the appropriate spot. Only needs to happen once.
"""
# Do each of our groups.
for group, videos in file_groups.items():
#group = os.path.join('ucf_data', group)
# Do each of our videos.
for video in videos:
# Get the parts.
parts = video.split('/')
classname = parts[0]
filename = parts[1].replace('HandStandPushups', 'HandstandPushups')
file_path = os.path.join('UCF101', filename)
# Check if this class exists.
if not os.path.exists(group + '/' + classname):
print("Creating folder for %s/%s" % (group, classname))
os.makedirs(group + '/' + classname)
# Check if we have already moved this file, or at least that it
# exists to move.
if not os.path.exists(file_path):
print("Can't find %s to move. Skipping." % (filename))
continue
# Copy it.
dest = group + '/' + classname + '/' + filename
print("Copying %s to %s" % (file_path, dest))
copyfile(file_path, dest)
#os.rename(file_path, dest)
print("Done.")
def main():
"""
Go through each of our train/test text files and move the videos
to the right place.
"""
# Get the videos in groups so we can move them.
group_lists = get_train_test_lists()
# Move the files.
move_files(group_lists)
if __name__ == '__main__':
os.chdir('./ucf_data')
main()