File size: 2,662 Bytes
d670799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import os.path as osp

from mmaction.localization import load_localize_proposal_file
from tools.data.parse_file_list import parse_directory


def process_norm_proposal_file(norm_proposal_file, frame_dict):
    """Process the normalized proposal file and denormalize it.



    Args:

        norm_proposal_file (str): Name of normalized proposal file.

        frame_dict (dict): Information of frame folders.

    """
    proposal_file = norm_proposal_file.replace('normalized_', '')
    norm_proposals = load_localize_proposal_file(norm_proposal_file)

    processed_proposal_list = []
    for idx, norm_proposal in enumerate(norm_proposals):
        video_id = norm_proposal[0]
        frame_info = frame_dict[video_id]
        num_frames = frame_info[1]
        frame_path = osp.basename(frame_info[0])

        gt = [[
            int(x[0]),
            int(float(x[1]) * num_frames),
            int(float(x[2]) * num_frames)
        ] for x in norm_proposal[2]]

        proposal = [[
            int(x[0]),
            float(x[1]),
            float(x[2]),
            int(float(x[3]) * num_frames),
            int(float(x[4]) * num_frames)
        ] for x in norm_proposal[3]]

        gt_dump = '\n'.join(['{} {} {}'.format(*x) for x in gt])
        gt_dump += '\n' if len(gt) else ''
        proposal_dump = '\n'.join(
            ['{} {:.04f} {:.04f} {} {}'.format(*x) for x in proposal])
        proposal_dump += '\n' if len(proposal) else ''

        processed_proposal_list.append(
            f'# {idx}\n{frame_path}\n{num_frames}\n1'
            f'\n{len(gt)}\n{gt_dump}{len(proposal)}\n{proposal_dump}')

    with open(proposal_file, 'w') as f:
        f.writelines(processed_proposal_list)


def parse_args():
    parser = argparse.ArgumentParser(description='Denormalize proposal file')
    parser.add_argument(
        'dataset',
        type=str,
        choices=['thumos14'],
        help='dataset to be denormalize proposal file')
    parser.add_argument(
        '--norm-proposal-file',
        type=str,
        help='normalized proposal file to be denormalize')
    parser.add_argument(
        '--data-prefix',
        type=str,
        help='path to a directory where rawframes are held')
    args = parser.parse_args()
    return args


def main():
    args = parse_args()

    print(f'Converting from {args.norm_proposal_file}.')
    frame_dict = parse_directory(args.data_prefix)
    process_norm_proposal_file(args.norm_proposal_file, frame_dict)


if __name__ == '__main__':
    main()