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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
size_categories:
|
8 |
- 100K<n<1M
|
9 |
pretty_name: Coco
|
10 |
+
---
|
11 |
+
|
12 |
+
# Coco dataset loader based on tensorflow dataset coco
|
13 |
+
|
14 |
+
## Object Detection
|
15 |
+
|
16 |
+
```python
|
17 |
+
import os
|
18 |
+
from datasets import load_dataset
|
19 |
+
from PIL import Image, ImageFont, ImageDraw, ImageColor
|
20 |
+
|
21 |
+
def calc_lum(rgb):
|
22 |
+
return (0.2126*rgb[0] + 0.7152*rgb[1] + 0.0722*rgb[2])
|
23 |
+
|
24 |
+
COLOR_MAP = [ImageColor.getrgb(code) for name, code in ImageColor.colormap.items()]
|
25 |
+
|
26 |
+
def get_text_bbox(bb, tbb, margin, im_w, im_h, anchor="leftBottom"):
|
27 |
+
m = margin
|
28 |
+
l, t, r, b = bb
|
29 |
+
tl, tt, tr, tb = tbb
|
30 |
+
bbw, bbh = r - l, b - t
|
31 |
+
tbbw, tbbh = tr - tl, tb - tt
|
32 |
+
|
33 |
+
# bbox (left-top)
|
34 |
+
if anchor == "leftTop":
|
35 |
+
ax, ay = l, t
|
36 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
37 |
+
# align (text box: left-bottom)
|
38 |
+
x1, y1 = max(ax, 0), max(ay - tb - 2*m, 0)
|
39 |
+
x2, y2 = min(x1 + tr + 2*m, im_w), min(y1 + tb + 2*m, im_h)
|
40 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
41 |
+
else:
|
42 |
+
# align (text box: left-top)
|
43 |
+
x1, y1 = max(ax, 0), max(ay, 0)
|
44 |
+
x2, y2 = min(x1 + tr + 2*m, im_w), min(y1 + tb + 2*m, im_h)
|
45 |
+
return (( x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
46 |
+
elif anchor == "rightTop":
|
47 |
+
ax, ay = r, t
|
48 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
49 |
+
# align (text box: left-bottom)
|
50 |
+
x2, y1 = max(ax, 0), max(ay - tb - 2*m, 0)
|
51 |
+
x1, y2 = max(x2 - tr - 2*m, 0), min(y1 + tb + 2*m, im_h)
|
52 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
53 |
+
else:
|
54 |
+
# align (text box: left-top)
|
55 |
+
x2, y1 = max(ax, 0), max(ay, 0)
|
56 |
+
x1, y2 = max(x2 - tr - 2*m, 0), min(y1 + tb + 2*m, im_h)
|
57 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
58 |
+
elif anchor == "rightBottom":
|
59 |
+
ax, ay = r, b
|
60 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
61 |
+
# align (text box: left-top)
|
62 |
+
x2, y2 = min(ax, im_w), min(ay + tb + 2*m, im_h)
|
63 |
+
x1, y1 = max(x2 - tr - 2*m, 0), max(y2 - tb - 2*m, 0)
|
64 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
65 |
+
else:
|
66 |
+
# align (text box: left-bottom)
|
67 |
+
x2, y2 = min(ax, im_w), max(ay, 0)
|
68 |
+
x1, y1 = max(x2 - tr - 2*m, 0), max(y2 - tb - 2*m, 0)
|
69 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
70 |
+
elif anchor == "leftBottom":
|
71 |
+
ax, ay = l, b
|
72 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
73 |
+
# align (text box: left-top)
|
74 |
+
x1, y2 = min(ax, im_w), min(ay + tb + 2*m, im_h)
|
75 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
76 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
77 |
+
else:
|
78 |
+
# align (text box: left-bottom)
|
79 |
+
x1, y2 = min(ax, im_w), max(ay, 0)
|
80 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
81 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
82 |
+
elif anchor == "centerBottom":
|
83 |
+
ax, ay = (l+r)//2, b
|
84 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
85 |
+
# align (text box: left-top)
|
86 |
+
x1, y2 = min(ax - tr//2 - m, im_w), min(ay + tb + 2*m, im_h)
|
87 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
88 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
89 |
+
else:
|
90 |
+
# align (text box: left-bottom)
|
91 |
+
x1, y2 = min(ax - tr//2 - m, im_w), max(ay, 0)
|
92 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
93 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
94 |
+
|
95 |
+
def draw_bbox(image, objects, out_path, label_names=None, font="Roboto-Bold.ttf", fontsize=15, fill=True, opacity=60, width=2, margin=3, anchor="leftBottom"):
|
96 |
+
fnt = ImageFont.truetype(font, fontsize)
|
97 |
+
im_w, im_h = image.size
|
98 |
+
|
99 |
+
|
100 |
+
img = image.convert("RGBA")
|
101 |
+
overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
|
102 |
+
draw = ImageDraw.Draw(overlay)
|
103 |
+
for bb, lbl_id in zip(objects["bbox"], objects["label"]):
|
104 |
+
c = COLOR_MAP[min(lbl_id, len(COLOR_MAP)-1)]
|
105 |
+
fill_c = c + (opacity, ) if fill else None
|
106 |
+
draw.rectangle((bb[0], bb[1], bb[2], bb[3]), outline=c, fill=fill_c, width=width)
|
107 |
+
|
108 |
+
text = ""
|
109 |
+
if label_names is not None:
|
110 |
+
text = label_names[lbl_id]
|
111 |
+
tbb = fnt.getbbox(text)
|
112 |
+
btn_bbox, text_pos = get_text_bbox(bb, tbb, margin, im_w, im_h, anchor)
|
113 |
+
fc = (0, 0, 0) if calc_lum(c) > 150 else (255, 255, 255)
|
114 |
+
draw.rectangle(btn_bbox, outline=c, fill=c + (255, ))
|
115 |
+
draw.text(text_pos, text, font=fnt, fill=fc + (255, ))
|
116 |
+
|
117 |
+
img = Image.alpha_composite(img, overlay)
|
118 |
+
overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
|
119 |
+
draw = ImageDraw.Draw(overlay)
|
120 |
+
img = img.convert("RGB")
|
121 |
+
img.save(out_path)
|
122 |
+
|
123 |
+
raw_datasets = load_dataset(
|
124 |
+
"coco.py",
|
125 |
+
"2017",
|
126 |
+
cache_dir="./huggingface_datasets",
|
127 |
+
)
|
128 |
+
|
129 |
+
train_dataset = raw_datasets["train"]
|
130 |
+
label_list = raw_datasets["train"].features["objects"].feature['label'].names
|
131 |
+
|
132 |
+
for idx, item in zip(range(10), train_dataset):
|
133 |
+
draw_bbox(item["image"], item["objects"], item["image/filename"], label_list)
|
134 |
+
|
135 |
+
```
|
136 |
+
|
137 |
+
![sample1](./images/000000000009.jpg)
|
138 |
+
![sample2](./images/000000000025.jpg)
|
139 |
+
|
140 |
+
|
141 |
+
## Panoptic segmentation
|
142 |
+
|
143 |
+
```python
|
144 |
+
|
145 |
+
import numpy as np
|
146 |
+
from datasets import load_dataset
|
147 |
+
from PIL import Image, ImageFont, ImageDraw, ImageColor
|
148 |
+
from transformers.image_transforms import (
|
149 |
+
rgb_to_id,
|
150 |
+
)
|
151 |
+
|
152 |
+
def calc_lum(rgb):
|
153 |
+
return (0.2126*rgb[0] + 0.7152*rgb[1] + 0.0722*rgb[2])
|
154 |
+
|
155 |
+
COLOR_MAP = [ImageColor.getrgb(code) for name, code in ImageColor.colormap.items()]
|
156 |
+
|
157 |
+
def get_text_bbox(bb, tbb, margin, im_w, im_h, anchor="leftBottom"):
|
158 |
+
m = margin
|
159 |
+
l, t, r, b = bb
|
160 |
+
tl, tt, tr, tb = tbb
|
161 |
+
bbw, bbh = r - l, b - t
|
162 |
+
tbbw, tbbh = tr - tl, tb - tt
|
163 |
+
|
164 |
+
# bbox (left-top)
|
165 |
+
if anchor == "leftTop":
|
166 |
+
ax, ay = l, t
|
167 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
168 |
+
# align (text box: left-bottom)
|
169 |
+
x1, y1 = max(ax, 0), max(ay - tb - 2*m, 0)
|
170 |
+
x2, y2 = min(x1 + tr + 2*m, im_w), min(y1 + tb + 2*m, im_h)
|
171 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
172 |
+
else:
|
173 |
+
# align (text box: left-top)
|
174 |
+
x1, y1 = max(ax, 0), max(ay, 0)
|
175 |
+
x2, y2 = min(x1 + tr + 2*m, im_w), min(y1 + tb + 2*m, im_h)
|
176 |
+
return (( x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
177 |
+
elif anchor == "rightTop":
|
178 |
+
ax, ay = r, t
|
179 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
180 |
+
# align (text box: left-bottom)
|
181 |
+
x2, y1 = max(ax, 0), max(ay - tb - 2*m, 0)
|
182 |
+
x1, y2 = max(x2 - tr - 2*m, 0), min(y1 + tb + 2*m, im_h)
|
183 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
184 |
+
else:
|
185 |
+
# align (text box: left-top)
|
186 |
+
x2, y1 = max(ax, 0), max(ay, 0)
|
187 |
+
x1, y2 = max(x2 - tr - 2*m, 0), min(y1 + tb + 2*m, im_h)
|
188 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
189 |
+
elif anchor == "rightBottom":
|
190 |
+
ax, ay = r, b
|
191 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
192 |
+
# align (text box: left-top)
|
193 |
+
x2, y2 = min(ax, im_w), min(ay + tb + 2*m, im_h)
|
194 |
+
x1, y1 = max(x2 - tr - 2*m, 0), max(y2 - tb - 2*m, 0)
|
195 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
196 |
+
else:
|
197 |
+
# align (text box: left-bottom)
|
198 |
+
x2, y2 = min(ax, im_w), max(ay, 0)
|
199 |
+
x1, y1 = max(x2 - tr - 2*m, 0), max(y2 - tb - 2*m, 0)
|
200 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
201 |
+
elif anchor == "leftBottom":
|
202 |
+
ax, ay = l, b
|
203 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
204 |
+
# align (text box: left-top)
|
205 |
+
x1, y2 = min(ax, im_w), min(ay + tb + 2*m, im_h)
|
206 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
207 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
208 |
+
else:
|
209 |
+
# align (text box: left-bottom)
|
210 |
+
x1, y2 = min(ax, im_w), max(ay, 0)
|
211 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
212 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
213 |
+
elif anchor == "centerBottom":
|
214 |
+
ax, ay = (l+r)//2, b
|
215 |
+
if tbbw*3 > bbw or tbbh*4 > bbh:
|
216 |
+
# align (text box: left-top)
|
217 |
+
x1, y2 = min(ax - tr//2 - m, im_w), min(ay + tb + 2*m, im_h)
|
218 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
219 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
220 |
+
else:
|
221 |
+
# align (text box: left-bottom)
|
222 |
+
x1, y2 = min(ax - tr//2 - m, im_w), max(ay, 0)
|
223 |
+
x2, y1 = min(x1 + tr + 2*m, im_w), max(y2 - tb - 2*m, 0)
|
224 |
+
return ((x1, y1, x2, y2), (max(x1+m, 0), max(y1+m, 0)))
|
225 |
+
|
226 |
+
# Copied from transformers.models.detr.image_processing_detr.masks_to_boxes
|
227 |
+
def masks_to_boxes(masks: np.ndarray) -> np.ndarray:
|
228 |
+
"""
|
229 |
+
Compute the bounding boxes around the provided panoptic segmentation masks.
|
230 |
+
Args:
|
231 |
+
masks: masks in format `[number_masks, height, width]` where N is the number of masks
|
232 |
+
Returns:
|
233 |
+
boxes: bounding boxes in format `[number_masks, 4]` in xyxy format
|
234 |
+
"""
|
235 |
+
if masks.size == 0:
|
236 |
+
return np.zeros((0, 4))
|
237 |
+
|
238 |
+
h, w = masks.shape[-2:]
|
239 |
+
y = np.arange(0, h, dtype=np.float32)
|
240 |
+
x = np.arange(0, w, dtype=np.float32)
|
241 |
+
# see https://github.com/pytorch/pytorch/issues/50276
|
242 |
+
y, x = np.meshgrid(y, x, indexing="ij")
|
243 |
+
|
244 |
+
x_mask = masks * np.expand_dims(x, axis=0)
|
245 |
+
x_max = x_mask.reshape(x_mask.shape[0], -1).max(-1)
|
246 |
+
x = np.ma.array(x_mask, mask=~(np.array(masks, dtype=bool)))
|
247 |
+
x_min = x.filled(fill_value=1e8)
|
248 |
+
x_min = x_min.reshape(x_min.shape[0], -1).min(-1)
|
249 |
+
|
250 |
+
y_mask = masks * np.expand_dims(y, axis=0)
|
251 |
+
y_max = y_mask.reshape(x_mask.shape[0], -1).max(-1)
|
252 |
+
y = np.ma.array(y_mask, mask=~(np.array(masks, dtype=bool)))
|
253 |
+
y_min = y.filled(fill_value=1e8)
|
254 |
+
y_min = y_min.reshape(y_min.shape[0], -1).min(-1)
|
255 |
+
|
256 |
+
return np.stack([x_min, y_min, x_max, y_max], 1)
|
257 |
+
|
258 |
+
def draw_seg(image, panoptic_image, oids, labels, out_path, label_names=None, font="Roboto-Bold.ttf", fontsize=15, opacity=160, anchor="leftBottom"):
|
259 |
+
fnt = ImageFont.truetype(font, fontsize)
|
260 |
+
im_w, im_h = image.size
|
261 |
+
|
262 |
+
masks = np.asarray(panoptic_image, dtype=np.uint32)
|
263 |
+
masks = rgb_to_id(masks)
|
264 |
+
|
265 |
+
oids = np.array(oids, dtype=np.uint32)
|
266 |
+
masks = masks == oids[:, None, None]
|
267 |
+
masks = masks.astype(np.uint8)
|
268 |
+
|
269 |
+
bboxes = masks_to_boxes(masks)
|
270 |
+
|
271 |
+
img = image.convert("RGBA")
|
272 |
+
|
273 |
+
for label, mask, bbox in zip(labels, masks, bboxes):
|
274 |
+
c = COLOR_MAP[min(label, len(COLOR_MAP)-1)]
|
275 |
+
cf = np.array(c + (opacity, )).astype(np.uint8)
|
276 |
+
cmask = mask[:, :, None] * cf[None, None, :]
|
277 |
+
cmask = Image.fromarray(cmask)
|
278 |
+
img = Image.alpha_composite(img, cmask)
|
279 |
+
|
280 |
+
if label_names is not None:
|
281 |
+
text = label_names[label]
|
282 |
+
tbb = fnt.getbbox(text)
|
283 |
+
btn_bbox, text_pos = get_text_bbox(bbox, tbb, 3, im_w, im_h, anchor=anchor)
|
284 |
+
|
285 |
+
overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
|
286 |
+
draw = ImageDraw.Draw(overlay)
|
287 |
+
|
288 |
+
fc = (0, 0, 0) if calc_lum(c) > 150 else (255, 255, 255)
|
289 |
+
|
290 |
+
draw.rectangle(btn_bbox, outline=c, fill=c + (255, ))
|
291 |
+
draw.text(text_pos, text, font=fnt, fill=fc + (255, ))
|
292 |
+
|
293 |
+
img = Image.alpha_composite(img, overlay)
|
294 |
+
|
295 |
+
img = img.convert("RGB")
|
296 |
+
img.save(out_path)
|
297 |
+
|
298 |
+
|
299 |
+
|
300 |
+
raw_datasets = load_dataset(
|
301 |
+
"coco.py",
|
302 |
+
"2017_panoptic",
|
303 |
+
cache_dir="./huggingface_datasets",
|
304 |
+
# data_dir="./data",
|
305 |
+
)
|
306 |
+
|
307 |
+
train_dataset = raw_datasets["train"]
|
308 |
+
label_list = raw_datasets["train"].features["panoptic_objects"].feature['label'].names
|
309 |
+
|
310 |
+
for idx, item in zip(range(10), train_dataset):
|
311 |
+
draw_seg(
|
312 |
+
item["image"],
|
313 |
+
item["panoptic_image"],
|
314 |
+
item["panoptic_objects"]["id"],
|
315 |
+
item["panoptic_objects"]["label"],
|
316 |
+
"panoptic_" + item["image/filename"],
|
317 |
+
label_list)
|
318 |
+
|
319 |
+
```
|
320 |
+
|
321 |
+
![sample1](./images/panoptic_000000000009.jpg)
|
322 |
+
![sample2](./images/panoptic_000000000025.jpg)
|
323 |
+
|
324 |
+
|
325 |
+
|