10
10
import sys
11
11
import uuid
12
12
13
+ import imgviz
13
14
import numpy as np
14
- import PIL .Image
15
15
16
16
import labelme
17
17
@@ -29,13 +29,18 @@ def main():
29
29
parser .add_argument ("input_dir" , help = "input annotated directory" )
30
30
parser .add_argument ("output_dir" , help = "output dataset directory" )
31
31
parser .add_argument ("--labels" , help = "labels file" , required = True )
32
+ parser .add_argument (
33
+ "--noviz" , help = "no visualization" , action = "store_true"
34
+ )
32
35
args = parser .parse_args ()
33
36
34
37
if osp .exists (args .output_dir ):
35
38
print ("Output directory already exists:" , args .output_dir )
36
39
sys .exit (1 )
37
40
os .makedirs (args .output_dir )
38
41
os .makedirs (osp .join (args .output_dir , "JPEGImages" ))
42
+ if not args .noviz :
43
+ os .makedirs (osp .join (args .output_dir , "Visualization" ))
39
44
print ("Creating dataset:" , args .output_dir )
40
45
41
46
now = datetime .datetime .now ()
@@ -85,7 +90,7 @@ def main():
85
90
out_img_file = osp .join (args .output_dir , "JPEGImages" , base + ".jpg" )
86
91
87
92
img = labelme .utils .img_data_to_arr (label_file .imageData )
88
- PIL . Image . fromarray ( img ). convert ( "RGB" ). save ( out_img_file )
93
+ imgviz . io . imsave ( out_img_file , img )
89
94
data ["images" ].append (
90
95
dict (
91
96
license = 0 ,
@@ -153,6 +158,27 @@ def main():
153
158
)
154
159
)
155
160
161
+ if not args .noviz :
162
+ labels , captions , masks = zip (
163
+ * [
164
+ (class_name_to_id [cnm ], cnm , msk )
165
+ for (cnm , gid ), msk in masks .items ()
166
+ if cnm in class_name_to_id
167
+ ]
168
+ )
169
+ viz = imgviz .instances2rgb (
170
+ image = img ,
171
+ labels = labels ,
172
+ masks = masks ,
173
+ captions = captions ,
174
+ font_size = 15 ,
175
+ line_width = 2 ,
176
+ )
177
+ out_viz_file = osp .join (
178
+ args .output_dir , "Visualization" , base + ".jpg"
179
+ )
180
+ imgviz .io .imsave (out_viz_file , viz )
181
+
156
182
with open (out_ann_file , "w" ) as f :
157
183
json .dump (data , f )
158
184
0 commit comments