Update README.md
Browse files
README.md
CHANGED
|
@@ -39,53 +39,100 @@ task_categories:
|
|
| 39 |
## Use the dataset
|
| 40 |
|
| 41 |
```py
|
|
|
|
| 42 |
from datasets import load_dataset
|
|
|
|
|
|
|
| 43 |
ds = load_dataset("ariG23498/coco2017", streaming=True, split="validation")
|
| 44 |
|
| 45 |
sample = next(iter(ds))
|
| 46 |
|
| 47 |
-
from PIL import Image, ImageDraw
|
| 48 |
|
| 49 |
def draw_bboxes_on_image(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
bboxes = objects.get("bbox", [])
|
| 58 |
categories = objects.get("categories", [])
|
| 59 |
|
| 60 |
-
for
|
| 61 |
x, y, width, height = bbox
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
# Draw
|
| 66 |
draw.rectangle([x_min, y_min, x_max, y_max], outline=box_color, width=2)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if category_names and category_id in category_names:
|
| 72 |
-
label = category_names[category_id]
|
| 73 |
-
|
| 74 |
-
# Draw the category label
|
| 75 |
-
text_bbox = draw.textbbox((x_min, y_min), label) # Use textbbox to get text size
|
| 76 |
text_width = text_bbox[2] - text_bbox[0]
|
| 77 |
text_height = text_bbox[3] - text_bbox[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
image=sample["image"],
|
| 87 |
objects=sample["objects"],
|
| 88 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
```
|
| 90 |
|
| 91 |
## Get the categories
|
|
|
|
| 39 |
## Use the dataset
|
| 40 |
|
| 41 |
```py
|
| 42 |
+
from random import randint
|
| 43 |
from datasets import load_dataset
|
| 44 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 45 |
+
|
| 46 |
ds = load_dataset("ariG23498/coco2017", streaming=True, split="validation")
|
| 47 |
|
| 48 |
sample = next(iter(ds))
|
| 49 |
|
|
|
|
| 50 |
|
| 51 |
def draw_bboxes_on_image(
|
| 52 |
+
image: Image.Image,
|
| 53 |
+
objects: dict,
|
| 54 |
+
category_names: dict = None,
|
| 55 |
+
box_color: str = "red",
|
| 56 |
+
text_color: str = "white"
|
| 57 |
+
) -> Image.Image:
|
| 58 |
+
image_copy = image.copy()
|
| 59 |
+
draw = ImageDraw.Draw(image_copy)
|
| 60 |
+
font = ImageFont.load_default()
|
| 61 |
+
|
| 62 |
bboxes = objects.get("bbox", [])
|
| 63 |
categories = objects.get("categories", [])
|
| 64 |
|
| 65 |
+
for bbox, category_id in zip(bboxes, categories):
|
| 66 |
x, y, width, height = bbox
|
| 67 |
+
x_min, y_min = x, y
|
| 68 |
+
x_max, y_max = x + width, y + height
|
| 69 |
|
| 70 |
+
# Draw bounding box
|
| 71 |
draw.rectangle([x_min, y_min, x_max, y_max], outline=box_color, width=2)
|
| 72 |
|
| 73 |
+
# Prepare label
|
| 74 |
+
label = category_names.get(category_id, str(category_id)) if category_names else str(category_id)
|
| 75 |
+
text_bbox = draw.textbbox((0, 0), label, font=font)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
text_width = text_bbox[2] - text_bbox[0]
|
| 77 |
text_height = text_bbox[3] - text_bbox[1]
|
| 78 |
+
label_top = max(y_min - text_height - 4, 0)
|
| 79 |
+
|
| 80 |
+
# Draw label background and text
|
| 81 |
+
draw.rectangle(
|
| 82 |
+
[x_min, label_top, x_min + text_width + 4, label_top + text_height + 2],
|
| 83 |
+
fill=box_color
|
| 84 |
+
)
|
| 85 |
+
draw.text((x_min + 2, label_top + 1), label, fill=text_color, font=font)
|
| 86 |
+
|
| 87 |
+
return image_copy
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def draw_segmaps_on_image(
|
| 92 |
+
image: Image.Image,
|
| 93 |
+
objects: dict,
|
| 94 |
+
category_names: dict = None,
|
| 95 |
+
alpha: float = 0.4,
|
| 96 |
+
text_color: str = "white"
|
| 97 |
+
) -> Image.Image:
|
| 98 |
+
base_image = image.convert("RGBA").copy()
|
| 99 |
+
overlay = Image.new("RGBA", base_image.size, (255, 255, 255, 0))
|
| 100 |
+
draw = ImageDraw.Draw(overlay)
|
| 101 |
+
font = ImageFont.load_default()
|
| 102 |
|
| 103 |
+
segmentations = objects.get("segmentation", [])
|
| 104 |
+
categories = objects.get("categories", [])
|
| 105 |
+
|
| 106 |
+
for segmentation, category_id in zip(segmentations, categories):
|
| 107 |
+
polygons = segmentation if isinstance(segmentation[0], list) else [segmentation]
|
| 108 |
+
label = category_names.get(category_id, str(category_id)) if category_names else str(category_id)
|
| 109 |
+
|
| 110 |
+
for polygon in polygons:
|
| 111 |
+
if len(polygon) >= 6:
|
| 112 |
+
points = [(polygon[i], polygon[i + 1]) for i in range(0, len(polygon), 2)]
|
| 113 |
+
|
| 114 |
+
# Draw filled polygon
|
| 115 |
+
segmap_color = (randint(125, 255), randint(0, 125), randint(0, 255))
|
| 116 |
+
rgba_fill = (*segmap_color, int(255 * alpha))
|
| 117 |
+
draw.polygon(points, fill=rgba_fill)
|
| 118 |
|
| 119 |
+
# Draw label at first vertex
|
| 120 |
+
x0, y0 = points[0]
|
| 121 |
+
draw.text((x0 + 2, y0 + 2), label, fill=text_color, font=font)
|
| 122 |
|
| 123 |
+
return Image.alpha_composite(base_image, overlay).convert("RGB")
|
| 124 |
+
|
| 125 |
+
# For Bounding Boxes
|
| 126 |
+
od_image = draw_bboxes_on_image(
|
| 127 |
image=sample["image"],
|
| 128 |
objects=sample["objects"],
|
| 129 |
)
|
| 130 |
+
|
| 131 |
+
# For Segmentation Maps
|
| 132 |
+
segmap_image = draw_segmaps_on_image(
|
| 133 |
+
image=sample["image"],
|
| 134 |
+
objects=sample["objects"]
|
| 135 |
+
)
|
| 136 |
```
|
| 137 |
|
| 138 |
## Get the categories
|