|
7 | 7 | import torch |
8 | 8 | from common_utils import get_tmp_dir |
9 | 9 | import pickle |
| 10 | +import random |
| 11 | +from itertools import cycle |
| 12 | +from torchvision.io.video import write_video |
10 | 13 |
|
11 | 14 |
|
12 | 15 | @contextlib.contextmanager |
@@ -265,3 +268,47 @@ def voc_root(): |
265 | 268 | f.write('test') |
266 | 269 |
|
267 | 270 | yield tmp_dir |
| 271 | + |
| 272 | + |
| 273 | +@contextlib.contextmanager |
| 274 | +def ucf101_root(): |
| 275 | + with get_tmp_dir() as tmp_dir: |
| 276 | + ucf_dir = os.path.join(tmp_dir, 'UCF-101') |
| 277 | + video_dir = os.path.join(ucf_dir, 'video') |
| 278 | + annotations = os.path.join(ucf_dir, 'annotations') |
| 279 | + |
| 280 | + os.makedirs(ucf_dir) |
| 281 | + os.makedirs(video_dir) |
| 282 | + os.makedirs(annotations) |
| 283 | + |
| 284 | + fold_files = [] |
| 285 | + for split in {'train', 'test'}: |
| 286 | + for fold in range(1, 4): |
| 287 | + fold_file = '{:s}list{:02d}.txt'.format(split, fold) |
| 288 | + fold_files.append(os.path.join(annotations, fold_file)) |
| 289 | + |
| 290 | + file_handles = [open(x, 'w') for x in fold_files] |
| 291 | + file_iter = cycle(file_handles) |
| 292 | + |
| 293 | + for i in range(0, 2): |
| 294 | + current_class = 'class_{0}'.format(i + 1) |
| 295 | + class_dir = os.path.join(video_dir, current_class) |
| 296 | + os.makedirs(class_dir) |
| 297 | + for group in range(0, 3): |
| 298 | + for clip in range(0, 4): |
| 299 | + # Save sample file |
| 300 | + clip_name = 'v_{0}_g{1}_c{2}.avi'.format( |
| 301 | + current_class, group, clip) |
| 302 | + clip_path = os.path.join(class_dir, clip_name) |
| 303 | + length = random.randrange(10, 21) |
| 304 | + this_clip = torch.randint( |
| 305 | + 0, 256, (length * 25, 320, 240, 3), dtype=torch.uint8) |
| 306 | + write_video(clip_path, this_clip, 25) |
| 307 | + # Add to annotations |
| 308 | + ann_file = next(file_iter) |
| 309 | + ann_file.write('{0}\n'.format( |
| 310 | + os.path.join(current_class, clip_name))) |
| 311 | + # Close all file descriptors |
| 312 | + for f in file_handles: |
| 313 | + f.close() |
| 314 | + yield (video_dir, annotations) |
0 commit comments