From 41c1efbf565f49f8883b4b6b1f2221d9b0241402 Mon Sep 17 00:00:00 2001 From: James Betker Date: Wed, 27 May 2020 17:09:11 -0600 Subject: [PATCH] Add dynamic video processing script --- codes/options/options.py | 41 +++++----- codes/process_video.py | 162 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 20 deletions(-) create mode 100644 codes/process_video.py diff --git a/codes/options/options.py b/codes/options/options.py index 935fc8a4..090addb1 100644 --- a/codes/options/options.py +++ b/codes/options/options.py @@ -20,26 +20,27 @@ def parse(opt_path, is_train=True): scale = opt['scale'] # datasets - for phase, dataset in opt['datasets'].items(): - phase = phase.split('_')[0] - dataset['phase'] = phase - if opt['distortion'] == 'sr' or opt['distortion'] == 'downsample': - dataset['scale'] = scale - is_lmdb = False - ''' LMDB is not supported at this point with the mods I've been making. - if dataset.get('dataroot_GT', None) is not None: - dataset['dataroot_GT'] = osp.expanduser(dataset['dataroot_GT']) - if dataset['dataroot_GT'].endswith('lmdb'): - is_lmdb = True - if dataset.get('dataroot_LQ', None) is not None: - dataset['dataroot_LQ'] = osp.expanduser(dataset['dataroot_LQ']) - if dataset['dataroot_LQ'].endswith('lmdb'): - is_lmdb = True - ''' - dataset['data_type'] = 'lmdb' if is_lmdb else 'img' - if dataset['mode'].endswith('mc'): # for memcached - dataset['data_type'] = 'mc' - dataset['mode'] = dataset['mode'].replace('_mc', '') + if 'datasets' in opt.keys(): + for phase, dataset in opt['datasets'].items(): + phase = phase.split('_')[0] + dataset['phase'] = phase + if opt['distortion'] == 'sr' or opt['distortion'] == 'downsample': + dataset['scale'] = scale + is_lmdb = False + ''' LMDB is not supported at this point with the mods I've been making. + if dataset.get('dataroot_GT', None) is not None: + dataset['dataroot_GT'] = osp.expanduser(dataset['dataroot_GT']) + if dataset['dataroot_GT'].endswith('lmdb'): + is_lmdb = True + if dataset.get('dataroot_LQ', None) is not None: + dataset['dataroot_LQ'] = osp.expanduser(dataset['dataroot_LQ']) + if dataset['dataroot_LQ'].endswith('lmdb'): + is_lmdb = True + ''' + dataset['data_type'] = 'lmdb' if is_lmdb else 'img' + if dataset['mode'].endswith('mc'): # for memcached + dataset['data_type'] = 'mc' + dataset['mode'] = dataset['mode'].replace('_mc', '') # path for key, path in opt['path'].items(): diff --git a/codes/process_video.py b/codes/process_video.py new file mode 100644 index 00000000..f442a334 --- /dev/null +++ b/codes/process_video.py @@ -0,0 +1,162 @@ +import argparse +import argparse +import logging +import os.path as osp +import os +import subprocess +import time + +import torch +import torch.utils.data as data +import torchvision.transforms.functional as F +from PIL import Image +from tqdm import tqdm + +import options.options as option +import utils.util as util +from data import create_dataloader +from models import create_model +import glob + + +class FfmpegBackedVideoDataset(data.Dataset): + '''Pulls frames from a video one at a time using FFMPEG.''' + + def __init__(self, opt, working_dir): + super(FfmpegBackedVideoDataset, self).__init__() + self.opt = opt + self.video = self.opt['video_file'] + self.working_dir = working_dir + self.frame_rate = self.opt['frame_rate'] + self.start_at = self.opt['start_at_seconds'] + self.end_at = self.opt['end_at_seconds'] + self.frame_count = (self.end_at - self.start_at) * self.frame_rate + # The number of (original) video frames that will be stored on the filesystem at a time. + self.max_working_files = 20 + + self.data_type = self.opt['data_type'] + self.vertical_splits = self.opt['vertical_splits'] if 'vertical_splits' in opt.keys() else 1 + + def get_time_for_it(self, it): + secs = it / self.frame_rate + self.start_at + mins = int(secs / 60) + secs = secs - (mins * 60) + return '%02d:%06.3f' % (mins, secs) + + def __getitem__(self, index): + if self.vertical_splits > 0: + actual_index = int(index / self.vertical_splits) + else: + actual_index = index + + # Extract the frame. Command template: `ffmpeg -ss 17:00.0323 -i