-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathannotatedvideowriter.py
72 lines (49 loc) · 2.67 KB
/
annotatedvideowriter.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
66
67
68
69
70
71
72
from annotator import annotate
from basecomponent import BaseComponent
import imageio
import cv2
import os.path
class AnnotatedVideoWriter(BaseComponent):
'''
This is a outputter component that writes annotated frames to an output video.
Unlike other components, design of this component has to be stateful - it opens output
video stream when it receives first frame and then keeps it open writing each frame to the video
till completed notification is received.
'''
def __init__(self, cfg):
BaseComponent.__init__(self, cfg)
self.output_video = None
self.output_filepath = None
def execute(self, input_data, input_directory, output_directory):
if not input_data['isvideo']:
return {}
# Open output video stream if this is first frame.
if input_data['frame'] == 0:
# The output directory structure should match input directory structure.
relpath_of_input_file = os.path.relpath(input_data['file'], input_directory)
relparent_of_input_file = os.path.dirname(relpath_of_input_file)
inp_filename,inp_extension = os.path.splitext(os.path.basename(relpath_of_input_file))
output_filedir = os.path.join(output_directory, relparent_of_input_file)
if not os.path.exists(output_filedir):
os.makedirs(output_filedir)
self.output_filepath = os.path.join(output_filedir,
inp_filename + '-annotated.' + self.cfg['params']['format'])
self.output_video = imageio.get_writer(self.output_filepath, 'ffmpeg')
img = input_data['img'].copy()
for comp in self.cfg['inputs']:
comp_outputs = input_data.get(comp)
comp_reports = comp_outputs['reports']
if not comp_reports:
print("Warning: pipeline file specifies {} as input for {} but {} is not outputting any location reports".format(
comp, self.name, comp
))
continue
annotate(img, comp_reports)
final_img = cv2.resize(img, (self.cfg['params']['size']['width'], self.cfg['params']['size']['height']))
self.output_video.append_data(final_img)
return {'file': self.output_filepath}
def completed(self, input_data, input_directory, output_directory):
if self.output_video:
self.output_video.close()
self.output_video = None
self.output_filepath = None