This repository was archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathconverter.py
65 lines (51 loc) · 2.07 KB
/
converter.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
import os
import json
import argparse
import os.path as osp
from progressbar import progressbar
from concurrent.futures import ProcessPoolExecutor
from ..utils import get_logger
from .kf1 import KitwareAnnotation
def _get_video_list(annotation_dir):
path = osp.join(annotation_dir, 'list-of-annotated-meva-clips.txt')
with open(path) as f:
video_list = [l.strip() for l in f][2:]
return video_list
def _worker(job):
video_name, annotation_dir = job
annotation = KitwareAnnotation(video_name, annotation_dir)
return annotation.get_activities_official()
def _get_official_format(video_list, annotation_dir):
jobs = [(video_name, annotation_dir) for video_name in video_list]
pool = ProcessPoolExecutor()
activities = []
for result in progressbar(pool.map(_worker, jobs)):
activities.extend(result)
reference = {'filesProcessed': video_list, 'activities': activities}
file_index = {video_name: {'framerate': 30.0, 'selected': {0: 1, 9000: 0}}
for video_name in video_list}
return reference, file_index
def _write_files(data_dict, output_dir):
os.makedirs(output_dir, exist_ok=True)
logger = get_logger(__name__)
for filename, data in data_dict.items():
path = osp.join(output_dir, filename + '.json')
if osp.exists(path):
logger.warning('Overwriting file %s', path)
with open(path, 'w') as f:
json.dump(data, f)
def convert_annotation(annotation_dir, output_dir):
video_list = _get_video_list(annotation_dir)
reference, file_index = _get_official_format(video_list, annotation_dir)
data_dict = {'reference': reference, 'file-index': file_index}
_write_files(data_dict, output_dir)
def main():
parser = argparse.ArgumentParser(
'Annotation Converter for KF1, from Kitware YML format to '
'ActEV Scorer JSON format.')
parser.add_argument('annotation_dir')
parser.add_argument('output_dir')
args = parser.parse_args()
convert_annotation(args.annotation_dir, args.output_dir)
if __name__ == "__main__":
main()