@@ -33,9 +33,10 @@ def vector_to_coco_segment_info(canvas: np.ndarray,
33
33
bbox = [xmin , ymin , xmax - xmin , ymax - ymin ]), canvas
34
34
35
35
36
- def mask_to_coco_segment_info (canvas : np .ndarray , annotation , annotation_idx : int , category_id ):
36
+ def mask_to_coco_segment_info (canvas : np .ndarray , annotation ,
37
+ annotation_idx : int , category_id ):
37
38
color = id_to_rgb (annotation_idx )
38
- mask = annotation .value .draw (color = color )
39
+ mask = annotation .value .draw (color = color )
39
40
shapely = annotation .value .shapely
40
41
xmin , ymin , xmax , ymax = shapely .bounds
41
42
canvas = np .where (canvas == (0 , 0 , 0 ), mask , canvas )
@@ -45,7 +46,11 @@ def mask_to_coco_segment_info(canvas: np.ndarray, annotation, annotation_idx: in
45
46
bbox = [xmin , ymin , xmax - xmin , ymax - ymin ]), canvas
46
47
47
48
48
- def process_label (label : Label , idx : Union [int , str ], image_root , mask_root , all_stuff = False ):
49
+ def process_label (label : Label ,
50
+ idx : Union [int , str ],
51
+ image_root ,
52
+ mask_root ,
53
+ all_stuff = False ):
49
54
"""
50
55
Masks become stuff
51
56
Polygon and rectangle become thing
@@ -64,17 +69,19 @@ def process_label(label: Label, idx: Union[int, str], image_root, mask_root, all
64
69
categories [annotation .name ] = hash_category_name (annotation .name )
65
70
if isinstance (annotation .value , Mask ):
66
71
segment , canvas = (mask_to_coco_segment_info (
67
- canvas , annotation , class_idx + 1 ,categories [annotation .name ]))
72
+ canvas , annotation , class_idx + 1 ,
73
+ categories [annotation .name ]))
68
74
segments .append (segment )
69
75
is_thing [annotation .name ] = 0
70
76
71
77
elif isinstance (annotation .value , (Polygon , Rectangle )):
72
78
segment , canvas = vector_to_coco_segment_info (
73
- canvas ,
74
- annotation ,
75
- annotation_idx = (class_idx if all_stuff else annotation_idx ) + 1 ,
76
- image = image ,
77
- category_id = categories [annotation .name ])
79
+ canvas ,
80
+ annotation ,
81
+ annotation_idx = (class_idx if all_stuff else annotation_idx )
82
+ + 1 ,
83
+ image = image ,
84
+ category_id = categories [annotation .name ])
78
85
segments .append (segment )
79
86
is_thing [annotation .name ] = 1 - int (all_stuff )
80
87
@@ -94,15 +101,16 @@ class CocoPanopticDataset(BaseModel):
94
101
categories : List [Categories ]
95
102
96
103
@classmethod
97
- def from_common (cls , labels : LabelCollection , image_root , mask_root , all_stuff ):
104
+ def from_common (cls , labels : LabelCollection , image_root , mask_root ,
105
+ all_stuff ):
98
106
all_coco_annotations = []
99
107
coco_categories = {}
100
108
coco_things = {}
101
109
images = []
102
110
with ProcessPoolExecutor (max_workers = 8 ) as exc :
103
111
futures = [
104
- exc .submit (process_label , label , idx , image_root , mask_root , all_stuff )
105
- for idx , label in enumerate (labels )
112
+ exc .submit (process_label , label , idx , image_root , mask_root ,
113
+ all_stuff ) for idx , label in enumerate (labels )
106
114
]
107
115
for future in tqdm (as_completed (futures )):
108
116
image , annotation , categories , things = future .result ()
@@ -111,16 +119,17 @@ def from_common(cls, labels: LabelCollection, image_root, mask_root, all_stuff):
111
119
coco_categories .update (categories )
112
120
coco_things .update (things )
113
121
114
-
115
- category_mapping = {category_id : idx + 1 for idx , category_id in enumerate (coco_categories .values ())}
116
- categories = [
117
- Categories (id = category_mapping [idx ],
118
- name = name ,
119
- supercategory = 'all' ,
120
- isthing = coco_things .get (
121
- name , 1 ))
122
- for name , idx in coco_categories .items ()
123
- ]
122
+ category_mapping = {
123
+ category_id : idx + 1
124
+ for idx , category_id in enumerate (coco_categories .values ())
125
+ }
126
+ categories = [
127
+ Categories (id = category_mapping [idx ],
128
+ name = name ,
129
+ supercategory = 'all' ,
130
+ isthing = coco_things .get (name , 1 ))
131
+ for name , idx in coco_categories .items ()
132
+ ]
124
133
125
134
for annot in all_coco_annotations :
126
135
for segment in annot .segments_info :
0 commit comments