code
stringlengths
66
870k
docstring
stringlengths
19
26.7k
func_name
stringlengths
1
138
language
stringclasses
1 value
repo
stringlengths
7
68
path
stringlengths
5
324
url
stringlengths
46
389
license
stringclasses
7 values
def get_kinetics_frames(kinetics_anotation_file: str) -> dict: """Given the AVA-kinetics anotation file, return a lookup to map the video id and the the set of timestamps involved of this video id. Args: kinetics_anotation_file (str): Path to the AVA-like anotation file for the kinetics subset. Returns: dict: the dict keys are the kinetics videos' video id. The values are the set of timestamps involved. """ with open(kinetics_anotation_file) as f: anotated_frames = [i.split(',') for i in f.readlines()] anotated_frames = [i for i in anotated_frames if len(i) == 7] anotated_frames = [(i[0], int(float(i[1]))) for i in anotated_frames] frame_lookup = defaultdict(set) for video_id, timestamp in anotated_frames: frame_lookup[video_id].add(timestamp) return frame_lookup
Given the AVA-kinetics anotation file, return a lookup to map the video id and the the set of timestamps involved of this video id. Args: kinetics_anotation_file (str): Path to the AVA-like anotation file for the kinetics subset. Returns: dict: the dict keys are the kinetics videos' video id. The values are the set of timestamps involved.
get_kinetics_frames
python
open-mmlab/mmaction2
tools/data/ava_kinetics/cut_kinetics.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/ava_kinetics/cut_kinetics.py
Apache-2.0
def filter_missing_videos(kinetics_list: str, frame_lookup: dict) -> dict: """Given the kinetics700 dataset list, remove the video ids from the lookup that are missing videos or frames. Args: kinetics_list (str): Path to the kinetics700 dataset list. The content of the list should be: ``` Path_to_video1 label_1\n Path_to_video2 label_2\n ... Path_to_videon label_n\n ``` The start and end of the video must be contained in the filename. For example: ``` class602/o3lCwWyyc_s_000012_000022.mp4\n ``` frame_lookup (dict): the dict from `get_kinetics_frames`. Returns: dict: the dict keys are the kinetics videos' video id. The values are the a list of tuples: (start_of_the_video, end_of_the_video, video_path) """ video_lookup = defaultdict(set) with open(kinetics_list) as f: for line in f.readlines(): video_path = line.split(' ')[0] # remove label information video_name = video_path.split('/')[-1] # get the file name video_name = video_name.split('.')[0] # remove file extensions video_name = video_name.split('_') video_id = '_'.join(video_name[:-2]) if video_id not in frame_lookup: continue start, end = int(video_name[-2]), int(video_name[-1]) frames = frame_lookup[video_id] frames = [frame for frame in frames if start < frame < end] if len(frames) == 0: continue start, end = max(start, min(frames) - 2), min(end, max(frames) + 2) video_lookup[video_id].add((start, end, video_path)) # Some frame ids exist in multiple videos in the Kinetics dataset. # The reason is the part of one video may fall into different categories. # Remove the duplicated records for video in video_lookup: if len(video_lookup[video]) == 1: continue info_list = list(video_lookup[video]) removed_list = [] for i, info_i in enumerate(info_list): start_i, end_i, _ = info_i for j in range(i + 1, len(info_list)): start_j, end_j, _ = info_list[j] if start_i <= start_j and end_j <= end_i: removed_list.append(j) elif start_j <= start_i and end_i <= end_j: removed_list.append(i) new_list = [] for i, info in enumerate(info_list): if i not in removed_list: new_list.append(info) video_lookup[video] = set(new_list) return video_lookup
Given the kinetics700 dataset list, remove the video ids from the lookup that are missing videos or frames. Args: kinetics_list (str): Path to the kinetics700 dataset list. The content of the list should be: ``` Path_to_video1 label_1 Path_to_video2 label_2 ... Path_to_videon label_n ``` The start and end of the video must be contained in the filename. For example: ``` class602/o3lCwWyyc_s_000012_000022.mp4 ``` frame_lookup (dict): the dict from `get_kinetics_frames`. Returns: dict: the dict keys are the kinetics videos' video id. The values are the a list of tuples: (start_of_the_video, end_of_the_video, video_path)
filter_missing_videos
python
open-mmlab/mmaction2
tools/data/ava_kinetics/cut_kinetics.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/ava_kinetics/cut_kinetics.py
Apache-2.0
def remove_failed_video(video_path: str) -> None: """Given the path to the video, delete the video if it cannot be read or if the actual length of the video is 0.75 seconds shorter than expected.""" try: v = decord.VideoReader(video_path) fps = v.get_avg_fps() num_frames = len(v) x = video_path.split('.')[0].split('_') time = int(x[-1]) - int(x[-2]) if num_frames < (time - 3 / 4) * fps: os.remove(video_path) except: # noqa: E722 os.remove(video_path) return
Given the path to the video, delete the video if it cannot be read or if the actual length of the video is 0.75 seconds shorter than expected.
remove_failed_video
python
open-mmlab/mmaction2
tools/data/ava_kinetics/cut_kinetics.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/ava_kinetics/cut_kinetics.py
Apache-2.0
def download(video_identifier, output_filename, num_attempts=5, url_base='https://www.youtube.com/watch?v='): """Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored. """ # Defensive argument checking. assert isinstance(video_identifier, str), 'video_identifier must be string' assert isinstance(output_filename, str), 'output_filename must be string' assert len(video_identifier) == 11, 'video_identifier must have length 11' status = False if not os.path.exists(output_filename): command = [ 'youtube-dl', '--quiet', '--no-warnings', '--no-check-certificate', '-f', 'mp4', '-o', '"%s"' % output_filename, '"%s"' % (url_base + video_identifier) ] command = ' '.join(command) print(command) attempts = 0 while True: try: subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: attempts += 1 if attempts == num_attempts: return status, 'Fail' else: break # Check if the video was successfully saved. status = os.path.exists(output_filename) return status, 'Downloaded'
Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored.
download
python
open-mmlab/mmaction2
tools/data/gym/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/gym/download.py
Apache-2.0
def construct_video_filename(item, trim_format, output_dir): """Given a dataset row, this function constructs the output filename for a given video.""" youtube_id, start_time, end_time = item start_time, end_time = int(start_time * 10), int(end_time * 10) basename = '%s_%s_%s.mp4' % (youtube_id, trim_format % start_time, trim_format % end_time) output_filename = os.path.join(output_dir, basename) return output_filename
Given a dataset row, this function constructs the output filename for a given video.
construct_video_filename
python
open-mmlab/mmaction2
tools/data/hvu/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/hvu/download.py
Apache-2.0
def download_clip(video_identifier, output_filename, start_time, end_time, tmp_dir='/tmp/hvu/.tmp_dir', num_attempts=5, url_base='https://www.youtube.com/watch?v='): """Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored. start_time: float Indicates the beginning time in seconds from where the video will be trimmed. end_time: float Indicates the ending time in seconds of the trimmed video. """ # Defensive argument checking. assert isinstance(video_identifier, str), 'video_identifier must be string' assert isinstance(output_filename, str), 'output_filename must be string' assert len(video_identifier) == 11, 'video_identifier must have length 11' status = False tmp_filename = os.path.join(tmp_dir, '%s.%%(ext)s' % uuid.uuid4()) if not os.path.exists(output_filename): if not os.path.exists(tmp_filename): command = [ 'youtube-dl', '--quiet', '--no-warnings', '--no-check-certificate', '-f', 'mp4', '-o', '"%s"' % tmp_filename, '"%s"' % (url_base + video_identifier) ] command = ' '.join(command) print(command) attempts = 0 while True: try: subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: attempts += 1 if attempts == num_attempts: return status, 'Downloading Failed' else: break tmp_filename = glob.glob('%s*' % tmp_filename.split('.')[0])[0] # Construct command to trim the videos (ffmpeg required). command = [ 'ffmpeg', '-i', '"%s"' % tmp_filename, '-ss', str(start_time), '-t', str(end_time - start_time), '-c:v', 'libx264', '-c:a', 'copy', '-threads', '1', '-loglevel', 'panic', '"%s"' % output_filename ] command = ' '.join(command) try: subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: return status, 'Trimming Failed' # Check if the video was successfully saved. status = os.path.exists(output_filename) os.remove(tmp_filename) return status, 'Downloaded'
Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored. start_time: float Indicates the beginning time in seconds from where the video will be trimmed. end_time: float Indicates the ending time in seconds of the trimmed video.
download_clip
python
open-mmlab/mmaction2
tools/data/hvu/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/hvu/download.py
Apache-2.0
def parse_hvu_annotations(input_csv): """Returns a parsed DataFrame. arguments: --------- input_csv: str Path to CSV file containing the following columns: 'Tags, youtube_id, time_start, time_end' returns: ------- dataset: List of tuples. Each tuple consists of (youtube_id, time_start, time_end). The type of time is float. """ lines = open(input_csv).readlines() lines = [x.strip().split(',')[1:] for x in lines[1:]] lines = [(x[0], float(x[1]), float(x[2])) for x in lines] return lines
Returns a parsed DataFrame. arguments: --------- input_csv: str Path to CSV file containing the following columns: 'Tags, youtube_id, time_start, time_end' returns: ------- dataset: List of tuples. Each tuple consists of (youtube_id, time_start, time_end). The type of time is float.
parse_hvu_annotations
python
open-mmlab/mmaction2
tools/data/hvu/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/hvu/download.py
Apache-2.0
def parse_directory(path, rgb_prefix='img_', flow_x_prefix='flow_x_', flow_y_prefix='flow_y_', level=1): """Parse directories holding extracted frames from standard benchmarks. Args: path (str): Directory path to parse frames. rgb_prefix (str): Prefix of generated rgb frames name. default: 'img_'. flow_x_prefix (str): Prefix of generated flow x name. default: `flow_x_`. flow_y_prefix (str): Prefix of generated flow y name. default: `flow_y_`. level (int): Directory level for glob searching. Options are 1 and 2. default: 1. Returns: dict: frame info dict with video id as key and tuple(path(str), rgb_num(int), flow_x_num(int)) as value. """ print(f'parse frames under directory {path}') if level == 1: # Only search for one-level directory def locate_directory(x): return osp.basename(x) frame_dirs = glob.glob(osp.join(path, '*')) elif level == 2: # search for two-level directory def locate_directory(x): return osp.join(osp.basename(osp.dirname(x)), osp.basename(x)) frame_dirs = glob.glob(osp.join(path, '*', '*')) else: raise ValueError('level can be only 1 or 2') def count_files(directory, prefix_list): """Count file number with a given directory and prefix. Args: directory (str): Data directory to be search. prefix_list (list): List or prefix. Returns: list (int): Number list of the file with the prefix. """ lst = os.listdir(directory) cnt_list = [len(fnmatch.filter(lst, x + '*')) for x in prefix_list] return cnt_list # check RGB frame_dict = {} for i, frame_dir in enumerate(frame_dirs): total_num = count_files(frame_dir, (rgb_prefix, flow_x_prefix, flow_y_prefix)) dir_name = locate_directory(frame_dir) num_x = total_num[1] num_y = total_num[2] if num_x != num_y: raise ValueError(f'x and y direction have different number ' f'of flow images in video directory: {frame_dir}') if i % 200 == 0: print(f'{i} videos parsed') frame_dict[dir_name] = (frame_dir, total_num[0], num_x) print('frame directory analysis done') return frame_dict
Parse directories holding extracted frames from standard benchmarks. Args: path (str): Directory path to parse frames. rgb_prefix (str): Prefix of generated rgb frames name. default: 'img_'. flow_x_prefix (str): Prefix of generated flow x name. default: `flow_x_`. flow_y_prefix (str): Prefix of generated flow y name. default: `flow_y_`. level (int): Directory level for glob searching. Options are 1 and 2. default: 1. Returns: dict: frame info dict with video id as key and tuple(path(str), rgb_num(int), flow_x_num(int)) as value.
parse_directory
python
open-mmlab/mmaction2
tools/data/hvu/generate_file_list.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/hvu/generate_file_list.py
Apache-2.0
def count_files(directory, prefix_list): """Count file number with a given directory and prefix. Args: directory (str): Data directory to be search. prefix_list (list): List or prefix. Returns: list (int): Number list of the file with the prefix. """ lst = os.listdir(directory) cnt_list = [len(fnmatch.filter(lst, x + '*')) for x in prefix_list] return cnt_list
Count file number with a given directory and prefix. Args: directory (str): Data directory to be search. prefix_list (list): List or prefix. Returns: list (int): Number list of the file with the prefix.
count_files
python
open-mmlab/mmaction2
tools/data/hvu/generate_file_list.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/hvu/generate_file_list.py
Apache-2.0
def create_video_folders(dataset, output_dir, tmp_dir): """Creates a directory for each label name in the dataset.""" if 'label-name' not in dataset.columns: this_dir = os.path.join(output_dir, 'test') if not os.path.exists(this_dir): os.makedirs(this_dir) # I should return a dict but ... return this_dir if not os.path.exists(output_dir): os.makedirs(output_dir) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) label_to_dir = {} for label_name in dataset['label-name'].unique(): this_dir = os.path.join(output_dir, label_name) if not os.path.exists(this_dir): os.makedirs(this_dir) label_to_dir[label_name] = this_dir return label_to_dir
Creates a directory for each label name in the dataset.
create_video_folders
python
open-mmlab/mmaction2
tools/data/kinetics/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/kinetics/download.py
Apache-2.0
def construct_video_filename(row, label_to_dir, trim_format='%06d'): """Given a dataset row, this function constructs the output filename for a given video.""" basename = '%s_%s_%s.mp4' % (row['video-id'], trim_format % row['start-time'], trim_format % row['end-time']) if not isinstance(label_to_dir, dict): dirname = label_to_dir else: dirname = label_to_dir[row['label-name']] output_filename = os.path.join(dirname, basename) return output_filename
Given a dataset row, this function constructs the output filename for a given video.
construct_video_filename
python
open-mmlab/mmaction2
tools/data/kinetics/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/kinetics/download.py
Apache-2.0
def download_clip(video_identifier, output_filename, start_time, end_time, tmp_dir='/tmp/kinetics/.tmp_dir', num_attempts=5, url_base='https://www.youtube.com/watch?v='): """Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored. start_time: float Indicates the beginning time in seconds from where the video will be trimmed. end_time: float Indicates the ending time in seconds of the trimmed video. """ # Defensive argument checking. assert isinstance(video_identifier, str), 'video_identifier must be string' assert isinstance(output_filename, str), 'output_filename must be string' assert len(video_identifier) == 11, 'video_identifier must have length 11' status = False # Construct command line for getting the direct video link. tmp_filename = os.path.join(tmp_dir, '%s.%%(ext)s' % uuid.uuid4()) if not os.path.exists(output_filename): if not os.path.exists(tmp_filename): command = [ 'youtube-dl', '--quiet', '--no-warnings', '--no-check-certificate', '-f', 'mp4', '-o', '"%s"' % tmp_filename, '"%s"' % (url_base + video_identifier) ] command = ' '.join(command) print(command) attempts = 0 while True: try: subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as err: attempts += 1 if attempts == num_attempts: return status, err.output else: break tmp_filename = glob.glob('%s*' % tmp_filename.split('.')[0])[0] # Construct command to trim the videos (ffmpeg required). command = [ 'ffmpeg', '-i', '"%s"' % tmp_filename, '-ss', str(start_time), '-t', str(end_time - start_time), '-c:v', 'libx264', '-c:a', 'copy', '-threads', '1', '-loglevel', 'panic', '"%s"' % output_filename ] command = ' '.join(command) try: subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as err: return status, err.output # Check if the video was successfully saved. status = os.path.exists(output_filename) os.remove(tmp_filename) return status, 'Downloaded'
Download a video from youtube if exists and is not blocked. arguments: --------- video_identifier: str Unique YouTube video identifier (11 characters) output_filename: str File path where the video will be stored. start_time: float Indicates the beginning time in seconds from where the video will be trimmed. end_time: float Indicates the ending time in seconds of the trimmed video.
download_clip
python
open-mmlab/mmaction2
tools/data/kinetics/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/kinetics/download.py
Apache-2.0
def parse_kinetics_annotations(input_csv, ignore_is_cc=False): """Returns a parsed DataFrame. arguments: --------- input_csv: str Path to CSV file containing the following columns: 'YouTube Identifier,Start time,End time,Class label' returns: ------- dataset: DataFrame Pandas with the following columns: 'video-id', 'start-time', 'end-time', 'label-name' """ df = pd.read_csv(input_csv) if 'youtube_id' in df.columns: columns = OrderedDict([('youtube_id', 'video-id'), ('time_start', 'start-time'), ('time_end', 'end-time'), ('label', 'label-name')]) df.rename(columns=columns, inplace=True) if ignore_is_cc: df = df.loc[:, df.columns.tolist()[:-1]] return df
Returns a parsed DataFrame. arguments: --------- input_csv: str Path to CSV file containing the following columns: 'YouTube Identifier,Start time,End time,Class label' returns: ------- dataset: DataFrame Pandas with the following columns: 'video-id', 'start-time', 'end-time', 'label-name'
parse_kinetics_annotations
python
open-mmlab/mmaction2
tools/data/kinetics/download.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/kinetics/download.py
Apache-2.0
def _compress_images(input_output_pair, size=224): """Scale and downsample an input image to a given fps and size (shorter side size). This also removes the audio from the image. """ input_image_path, output_image_path = input_output_pair try: resize_image(input_image_path, output_image_path, size) except Exception as e: print(f'Caught Exception {e}')
Scale and downsample an input image to a given fps and size (shorter side size). This also removes the audio from the image.
_compress_images
python
open-mmlab/mmaction2
tools/data/msrvtt/compress.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/msrvtt/compress.py
Apache-2.0
def _compress_videos(input_output_pair, size=224, fps=3): """Scale and downsample an input video to a given fps and size (shorter side size). This also removes the audio from the video. """ input_file_path, output_file_path = input_output_pair try: command = [ 'ffmpeg', '-y', # (optional) overwrite output file if it exists '-i', input_file_path, '-filter:v', # no audio f"scale='if(gt(a,1),trunc(oh*a/2)*2,{size})':'if(gt(a,1),{size},trunc(ow*a/2)*2)'", # noqa: E501 '-map', '0:v', # no audio '-r', str(fps), # frames per second # '-g', str(16), output_file_path, ] subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) except Exception as e: raise e
Scale and downsample an input video to a given fps and size (shorter side size). This also removes the audio from the video.
_compress_videos
python
open-mmlab/mmaction2
tools/data/msrvtt/compress.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/msrvtt/compress.py
Apache-2.0
def format_det_result(): """convert test results to specified format in MultiSports competition.""" test_results = load(args.test_result) annos = load(args.anno_path) test_videos = annos['test_videos'][0] resolutions = annos['resolution'] frm_dets = [] for pred in track(test_results, description='formating...'): video_key = pred['video_id'].split('.mp4')[0] frm_num = pred['timestamp'] bboxes = pred['pred_instances']['bboxes'] cls_scores = pred['pred_instances']['scores'] for bbox, cls_score in zip(bboxes, cls_scores): video_idx = test_videos.index(video_key) pred_label = np.argmax(cls_score) score = cls_score[pred_label] h, w = resolutions[video_key] bbox *= np.array([w, h, w, h]) instance_result = np.array( [video_idx, frm_num, pred_label, score, *bbox]) frm_dets.append(instance_result) frm_dets = np.array(frm_dets) video_tubes = link_tubes(annos, frm_dets, K=1) dump(frm_dets, args.frm_out_path) dump(video_tubes, args.tube_out_path)
convert test results to specified format in MultiSports competition.
format_det_result
python
open-mmlab/mmaction2
tools/data/multisports/format_det_result.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/data/multisports/format_det_result.py
Apache-2.0
def mmaction2torchserve( config_file: str, checkpoint_file: str, output_folder: str, model_name: str, label_file: str, model_version: str = '1.0', force: bool = False, ): """Converts MMAction2 model (config + checkpoint) to TorchServe `.mar`. Args: config_file (str): In MMAction2 config format. checkpoint_file (str): In MMAction2 checkpoint format. output_folder (str): Folder where `{model_name}.mar` will be created. The file created will be in TorchServe archive format. label_file (str): A txt file which contains the action category names. model_name (str | None): If not None, used for naming the `{model_name}.mar` file that will be created under `output_folder`. If None, `{Path(checkpoint_file).stem}` will be used. model_version (str): Model's version. force (bool): If True, if there is an existing `{model_name}.mar` file under `output_folder` it will be overwritten. """ mkdir_or_exist(output_folder) config = Config.fromfile(config_file) with TemporaryDirectory() as tmpdir: config.dump(f'{tmpdir}/config.py') shutil.copy(label_file, f'{tmpdir}/label_map.txt') args = Namespace( **{ 'model_file': f'{tmpdir}/config.py', 'serialized_file': checkpoint_file, 'handler': f'{Path(__file__).parent}/mmaction_handler.py', 'model_name': model_name or Path(checkpoint_file).stem, 'version': model_version, 'export_path': output_folder, 'force': force, 'requirements_file': None, 'extra_files': f'{tmpdir}/label_map.txt', 'runtime': 'python', 'archive_format': 'default' }) manifest = ModelExportUtils.generate_manifest_json(args) package_model(args, manifest)
Converts MMAction2 model (config + checkpoint) to TorchServe `.mar`. Args: config_file (str): In MMAction2 config format. checkpoint_file (str): In MMAction2 checkpoint format. output_folder (str): Folder where `{model_name}.mar` will be created. The file created will be in TorchServe archive format. label_file (str): A txt file which contains the action category names. model_name (str | None): If not None, used for naming the `{model_name}.mar` file that will be created under `output_folder`. If None, `{Path(checkpoint_file).stem}` will be used. model_version (str): Model's version. force (bool): If True, if there is an existing `{model_name}.mar` file under `output_folder` it will be overwritten.
mmaction2torchserve
python
open-mmlab/mmaction2
tools/deployment/mmaction2torchserve.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/deployment/mmaction2torchserve.py
Apache-2.0
def load_video_infos(ann_file): """Load the video annotations. Args: ann_file (str): A json file path of the annotation file. Returns: list[dict]: A list containing annotations for videos. """ video_infos = [] anno_database = mmengine.load(ann_file) for video_name in anno_database: video_info = anno_database[video_name] video_info['video_name'] = video_name video_infos.append(video_info) return video_infos
Load the video annotations. Args: ann_file (str): A json file path of the annotation file. Returns: list[dict]: A list containing annotations for videos.
load_video_infos
python
open-mmlab/mmaction2
tools/misc/bsn_proposal_generation.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/bsn_proposal_generation.py
Apache-2.0
def generate_proposals(ann_file, tem_results_dir, pgm_proposals_dir, pgm_proposals_thread, **kwargs): """Generate proposals using multi-process. Args: ann_file (str): A json file path of the annotation file for all videos to be processed. tem_results_dir (str): Directory to read tem results pgm_proposals_dir (str): Directory to save generated proposals. pgm_proposals_thread (int): Total number of threads. kwargs (dict): Keyword arguments for "generate_candidate_proposals". """ video_infos = load_video_infos(ann_file) num_videos = len(video_infos) num_videos_per_thread = num_videos // pgm_proposals_thread processes = [] manager = mp.Manager() result_dict = manager.dict() kwargs['result_dict'] = result_dict for tid in range(pgm_proposals_thread - 1): tmp_video_list = range(tid * num_videos_per_thread, (tid + 1) * num_videos_per_thread) p = mp.Process( target=generate_candidate_proposals, args=( tmp_video_list, video_infos, tem_results_dir, ), kwargs=kwargs) p.start() processes.append(p) tmp_video_list = range((pgm_proposals_thread - 1) * num_videos_per_thread, num_videos) p = mp.Process( target=generate_candidate_proposals, args=( tmp_video_list, video_infos, tem_results_dir, ), kwargs=kwargs) p.start() processes.append(p) for p in processes: p.join() # save results os.makedirs(pgm_proposals_dir, exist_ok=True) prog_bar = mmengine.ProgressBar(num_videos) header = 'tmin,tmax,tmin_score,tmax_score,score,match_iou,match_ioa' for video_name in result_dict: proposals = result_dict[video_name] proposal_path = osp.join(pgm_proposals_dir, video_name + '.csv') np.savetxt( proposal_path, proposals, header=header, delimiter=',', comments='') prog_bar.update()
Generate proposals using multi-process. Args: ann_file (str): A json file path of the annotation file for all videos to be processed. tem_results_dir (str): Directory to read tem results pgm_proposals_dir (str): Directory to save generated proposals. pgm_proposals_thread (int): Total number of threads. kwargs (dict): Keyword arguments for "generate_candidate_proposals".
generate_proposals
python
open-mmlab/mmaction2
tools/misc/bsn_proposal_generation.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/bsn_proposal_generation.py
Apache-2.0
def generate_features(ann_file, tem_results_dir, pgm_proposals_dir, pgm_features_dir, pgm_features_thread, **kwargs): """Generate proposals features using multi-process. Args: ann_file (str): A json file path of the annotation file for all videos to be processed. tem_results_dir (str): Directory to read tem results. pgm_proposals_dir (str): Directory to read generated proposals. pgm_features_dir (str): Directory to save generated features. pgm_features_thread (int): Total number of threads. kwargs (dict): Keyword arguments for "generate_bsp_feature". """ video_infos = load_video_infos(ann_file) num_videos = len(video_infos) num_videos_per_thread = num_videos // pgm_features_thread processes = [] manager = mp.Manager() feature_return_dict = manager.dict() kwargs['result_dict'] = feature_return_dict for tid in range(pgm_features_thread - 1): tmp_video_list = range(tid * num_videos_per_thread, (tid + 1) * num_videos_per_thread) p = mp.Process( target=generate_bsp_feature, args=( tmp_video_list, video_infos, tem_results_dir, pgm_proposals_dir, ), kwargs=kwargs) p.start() processes.append(p) tmp_video_list = range((pgm_features_thread - 1) * num_videos_per_thread, num_videos) p = mp.Process( target=generate_bsp_feature, args=( tmp_video_list, video_infos, tem_results_dir, pgm_proposals_dir, ), kwargs=kwargs) p.start() processes.append(p) for p in processes: p.join() # save results os.makedirs(pgm_features_dir, exist_ok=True) prog_bar = mmengine.ProgressBar(num_videos) for video_name in feature_return_dict.keys(): bsp_feature = feature_return_dict[video_name] feature_path = osp.join(pgm_features_dir, video_name + '.npy') np.save(feature_path, bsp_feature) prog_bar.update()
Generate proposals features using multi-process. Args: ann_file (str): A json file path of the annotation file for all videos to be processed. tem_results_dir (str): Directory to read tem results. pgm_proposals_dir (str): Directory to read generated proposals. pgm_features_dir (str): Directory to save generated features. pgm_features_thread (int): Total number of threads. kwargs (dict): Keyword arguments for "generate_bsp_feature".
generate_features
python
open-mmlab/mmaction2
tools/misc/bsn_proposal_generation.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/bsn_proposal_generation.py
Apache-2.0
def flow_to_img(raw_flow, bound=20.): """Convert flow to gray image. Args: raw_flow (np.ndarray[float]): Estimated flow with the shape (w, h). bound (float): Bound for the flow-to-image normalization. Default: 20. Returns: np.ndarray[uint8]: The result list of np.ndarray[uint8], with shape (w, h). """ flow = np.clip(raw_flow, -bound, bound) flow += bound flow *= (255 / float(2 * bound)) flow = flow.astype(np.uint8) return flow
Convert flow to gray image. Args: raw_flow (np.ndarray[float]): Estimated flow with the shape (w, h). bound (float): Bound for the flow-to-image normalization. Default: 20. Returns: np.ndarray[uint8]: The result list of np.ndarray[uint8], with shape (w, h).
flow_to_img
python
open-mmlab/mmaction2
tools/misc/flow_extraction.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/flow_extraction.py
Apache-2.0
def generate_flow(frames, method='tvl1'): """Estimate flow with given frames. Args: frames (list[np.ndarray[uint8]]): List of rgb frames, with shape (w, h, 3). method (str): Use which method to generate flow. Options are 'tvl1' and 'farneback'. Default: 'tvl1'. Returns: list[np.ndarray[float]]: The result list of np.ndarray[float], with shape (w, h, 2). """ assert method in ['tvl1', 'farneback'] gray_frames = [cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) for frame in frames] if method == 'tvl1': tvl1 = cv2.optflow.DualTVL1OpticalFlow_create() def op(x, y): return tvl1.calc(x, y, None) elif method == 'farneback': def op(x, y): return cv2.calcOpticalFlowFarneback(x, y, None, 0.5, 3, 15, 3, 5, 1.2, 0) gray_st = gray_frames[:-1] gray_ed = gray_frames[1:] flow = [op(x, y) for x, y in zip(gray_st, gray_ed)] return flow
Estimate flow with given frames. Args: frames (list[np.ndarray[uint8]]): List of rgb frames, with shape (w, h, 3). method (str): Use which method to generate flow. Options are 'tvl1' and 'farneback'. Default: 'tvl1'. Returns: list[np.ndarray[float]]: The result list of np.ndarray[float], with shape (w, h, 2).
generate_flow
python
open-mmlab/mmaction2
tools/misc/flow_extraction.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/flow_extraction.py
Apache-2.0
def extract_dense_flow(path, dest, bound=20., save_rgb=False, start_idx=0, rgb_tmpl='img_{:05d}.jpg', flow_tmpl='{}_{:05d}.jpg', method='tvl1'): """Extract dense flow given video or frames, save them as gray-scale images. Args: path (str): Location of the input video. dest (str): The directory to store the extracted flow images. bound (float): Bound for the flow-to-image normalization. Default: 20. save_rgb (bool): Save extracted RGB frames. Default: False. start_idx (int): The starting frame index if use frames as input, the first image is path.format(start_idx). Default: 0. rgb_tmpl (str): The template of RGB frame names, Default: 'img_{:05d}.jpg'. flow_tmpl (str): The template of Flow frame names, Default: '{}_{:05d}.jpg'. method (str): Use which method to generate flow. Options are 'tvl1' and 'farneback'. Default: 'tvl1'. """ frames = [] assert osp.exists(path) video = cv2.VideoCapture(path) flag, f = video.read() while flag: frames.append(f) flag, f = video.read() flow = generate_flow(frames, method=method) flow_x = [flow_to_img(x[:, :, 0], bound) for x in flow] flow_y = [flow_to_img(x[:, :, 1], bound) for x in flow] if not osp.exists(dest): os.system('mkdir -p ' + dest) flow_x_names = [ osp.join(dest, flow_tmpl.format('x', ind + start_idx)) for ind in range(len(flow_x)) ] flow_y_names = [ osp.join(dest, flow_tmpl.format('y', ind + start_idx)) for ind in range(len(flow_y)) ] num_frames = len(flow) for i in range(num_frames): cv2.imwrite(flow_x_names[i], flow_x[i]) cv2.imwrite(flow_y_names[i], flow_y[i]) if save_rgb: img_names = [ osp.join(dest, rgb_tmpl.format(ind + start_idx)) for ind in range(len(frames)) ] for frame, name in zip(frames, img_names): cv2.imwrite(name, frame)
Extract dense flow given video or frames, save them as gray-scale images. Args: path (str): Location of the input video. dest (str): The directory to store the extracted flow images. bound (float): Bound for the flow-to-image normalization. Default: 20. save_rgb (bool): Save extracted RGB frames. Default: False. start_idx (int): The starting frame index if use frames as input, the first image is path.format(start_idx). Default: 0. rgb_tmpl (str): The template of RGB frame names, Default: 'img_{:05d}.jpg'. flow_tmpl (str): The template of Flow frame names, Default: '{}_{:05d}.jpg'. method (str): Use which method to generate flow. Options are 'tvl1' and 'farneback'. Default: 'tvl1'.
extract_dense_flow
python
open-mmlab/mmaction2
tools/misc/flow_extraction.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/misc/flow_extraction.py
Apache-2.0
def make_grid(videos, names, rescale_factor=None): """Concat list of pictures into a single big picture, align height here.""" vis = Visualizer() ori_shapes = [vid[0].shape[:2] for vid in videos] if rescale_factor is not None: videos = [[mmcv.imrescale(img, rescale_factor) for img in video] for video in videos] max_height = int(max(vid[0].shape[0] for vid in videos) * 1.4) min_width = min(vid[0].shape[1] for vid in videos) horizontal_gap = min_width // 10 img_scale = _get_adaptive_scale((max_height, min_width)) texts = [] text_positions = [] start_x = 0 for i, vid in enumerate(videos): for j, img in enumerate(vid): pad_height = (max_height - img.shape[0]) // 2 pad_width = horizontal_gap // 2 # make border videos[i][j] = cv2.copyMakeBorder( img, pad_height, max_height - img.shape[0] - pad_height + int(img_scale * 30 * 2), pad_width, pad_width, cv2.BORDER_CONSTANT, value=(255, 255, 255)) texts.append(f'{names[i]}\n{ori_shapes[i]}') text_positions.append( [start_x + img.shape[1] // 2 + pad_width, max_height]) start_x += img.shape[1] + horizontal_gap out_frames = [] for i in range(len(videos[0])): imgs = [vid[i] for vid in videos] display_img = np.concatenate(imgs, axis=1) vis.set_image(display_img) img_scale = _get_adaptive_scale(display_img.shape[:2]) vis.draw_texts( texts, positions=np.array(text_positions), font_sizes=img_scale * 7, colors='black', horizontal_alignments='center', font_families='monospace') out_frames.append(vis.get_image()) return out_frames
Concat list of pictures into a single big picture, align height here.
make_grid
python
open-mmlab/mmaction2
tools/visualizations/browse_dataset.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/visualizations/browse_dataset.py
Apache-2.0
def build_inputs(model: nn.Module, video_path: str, use_frames: bool = False) -> Dict: """build inputs for GradCAM. Note that, building inputs for GradCAM is exactly the same as building inputs for Recognizer test stage. Codes from `inference_recognizer`. Args: model (nn.Module): Recognizer model. video_path (str): video file/url or rawframes directory. use_frames (bool): whether to use rawframes as input. Defaults to False. Returns: dict: Both GradCAM inputs and Recognizer test stage inputs, including two keys, ``inputs`` and ``data_samples``. """ if not (osp.exists(video_path) or video_path.startswith('http')): raise RuntimeError(f"'{video_path}' is missing") if osp.isfile(video_path) and use_frames: raise RuntimeError( f"'{video_path}' is a video file, not a rawframe directory") if osp.isdir(video_path) and not use_frames: raise RuntimeError( f"'{video_path}' is a rawframe directory, not a video file") cfg = model.cfg # build the data pipeline test_pipeline = cfg.test_pipeline test_pipeline = Compose(test_pipeline) # prepare data if use_frames: filename_tmpl = cfg.test_dataloader.dataset.get( 'filename_tmpl', 'img_{:05}.jpg') start_index = cfg.test_dataloader.dataset.get('start_index', 1) data = dict( frame_dir=video_path, total_frames=len(os.listdir(video_path)), label=-1, start_index=start_index, filename_tmpl=filename_tmpl, modality='RGB') else: start_index = cfg.test_dataloader.dataset.get('start_index', 0) data = dict( filename=video_path, label=-1, start_index=start_index, modality='RGB') data = test_pipeline(data) data = pseudo_collate([data]) return data
build inputs for GradCAM. Note that, building inputs for GradCAM is exactly the same as building inputs for Recognizer test stage. Codes from `inference_recognizer`. Args: model (nn.Module): Recognizer model. video_path (str): video file/url or rawframes directory. use_frames (bool): whether to use rawframes as input. Defaults to False. Returns: dict: Both GradCAM inputs and Recognizer test stage inputs, including two keys, ``inputs`` and ``data_samples``.
build_inputs
python
open-mmlab/mmaction2
tools/visualizations/vis_cam.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/visualizations/vis_cam.py
Apache-2.0
def _resize_frames(frame_list: List[np.ndarray], scale: Optional[Tuple[int]] = None, keep_ratio: bool = True, interpolation: str = 'bilinear') -> List[np.ndarray]: """Resize frames according to given scale. Codes are modified from `mmaction/datasets/transforms/processing.py`, `Resize` class. Args: frame_list (list[np.ndarray]): Frames to be resized. scale (tuple[int]): If keep_ratio is True, it serves as scaling factor or maximum size: the image will be rescaled as large as possible within the scale. Otherwise, it serves as (w, h) of output size. keep_ratio (bool): If set to True, Images will be resized without changing the aspect ratio. Otherwise, it will resize images to a given size. Defaults to True. interpolation (str): Algorithm used for interpolation: 'nearest' | 'bilinear'. Defaults to ``'bilinear'``. Returns: list[np.ndarray]: Resized frames. """ if scale is None or (scale[0] == -1 and scale[1] == -1): return frame_list scale = tuple(scale) max_long_edge = max(scale) max_short_edge = min(scale) if max_short_edge == -1: scale = (np.inf, max_long_edge) img_h, img_w, _ = frame_list[0].shape if keep_ratio: new_w, new_h = mmcv.rescale_size((img_w, img_h), scale) else: new_w, new_h = scale frame_list = [ mmcv.imresize(img, (new_w, new_h), interpolation=interpolation) for img in frame_list ] return frame_list
Resize frames according to given scale. Codes are modified from `mmaction/datasets/transforms/processing.py`, `Resize` class. Args: frame_list (list[np.ndarray]): Frames to be resized. scale (tuple[int]): If keep_ratio is True, it serves as scaling factor or maximum size: the image will be rescaled as large as possible within the scale. Otherwise, it serves as (w, h) of output size. keep_ratio (bool): If set to True, Images will be resized without changing the aspect ratio. Otherwise, it will resize images to a given size. Defaults to True. interpolation (str): Algorithm used for interpolation: 'nearest' | 'bilinear'. Defaults to ``'bilinear'``. Returns: list[np.ndarray]: Resized frames.
_resize_frames
python
open-mmlab/mmaction2
tools/visualizations/vis_cam.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/visualizations/vis_cam.py
Apache-2.0
def plot_curve(lr_list, args, param_name, iters_per_epoch, by_epoch=True): """Plot learning rate vs iter graph.""" try: import seaborn as sns sns.set_style(args.style) except ImportError: pass wind_w, wind_h = args.window_size.split('*') wind_w, wind_h = int(wind_w), int(wind_h) plt.figure(figsize=(wind_w, wind_h)) ax: plt.Axes = plt.subplot() ax.plot(lr_list, linewidth=1) if by_epoch: ax.xaxis.tick_top() ax.set_xlabel('Iters') ax.xaxis.set_label_position('top') sec_ax = ax.secondary_xaxis( 'bottom', functions=(lambda x: x / iters_per_epoch, lambda y: y * iters_per_epoch)) sec_ax.set_xlabel('Epochs') else: plt.xlabel('Iters') plt.ylabel(param_name) if args.title is None: plt.title(f'{osp.basename(args.config)} {param_name} curve') else: plt.title(args.title)
Plot learning rate vs iter graph.
plot_curve
python
open-mmlab/mmaction2
tools/visualizations/vis_scheduler.py
https://github.com/open-mmlab/mmaction2/blob/master/tools/visualizations/vis_scheduler.py
Apache-2.0