-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathannotatedphotowriter.py
49 lines (37 loc) · 1.85 KB
/
annotatedphotowriter.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
from annotator import annotate
from basecomponent import BaseComponent
import imageio
import cv2
import os.path
class AnnotatedPhotoWriter(BaseComponent):
def __init__(self, cfg):
BaseComponent.__init__(self, cfg)
def execute(self, input_data, input_directory, output_directory):
if not input_data['isphoto']:
return {}
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)
# 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)
output_filepath = os.path.join(output_filedir,
inp_filename + '-annotated.' + self.cfg['params']['format'])
if self.cfg['params'].get('size'):
final_img = cv2.resize(img, (self.cfg['params']['size']['width'], self.cfg['params']['size']['height']))
else:
final_img = img
print(output_filepath)
imageio.imwrite(output_filepath, final_img)
return {'file':output_filepath}