Spaces:
Sleeping
Sleeping
🔨 [Update] Drawer, return PIL image and unsacle
Browse files- yolo/utils/drawer.py +9 -5
yolo/utils/drawer.py
CHANGED
|
@@ -7,7 +7,9 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 7 |
from torchvision.transforms.functional import to_pil_image
|
| 8 |
|
| 9 |
|
| 10 |
-
def draw_bboxes(
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
Draw bounding boxes on an image.
|
| 13 |
|
|
@@ -30,16 +32,18 @@ def draw_bboxes(img: Union[Image.Image, torch.Tensor], bboxes: List[List[Union[i
|
|
| 30 |
|
| 31 |
for bbox in bboxes:
|
| 32 |
class_id, x_min, y_min, x_max, y_max = bbox
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
shape = [(x_min, y_min), (x_max, y_max)]
|
| 38 |
draw.rectangle(shape, outline="red", width=3)
|
| 39 |
draw.text((x_min, y_min), str(int(class_id)), font=font, fill="blue")
|
| 40 |
|
| 41 |
img.save("visualize.jpg") # Save the image with annotations
|
| 42 |
logger.info("Saved visualize image at visualize.png")
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
def draw_model(*, model_cfg=None, model=None, v7_base=False):
|
|
|
|
| 7 |
from torchvision.transforms.functional import to_pil_image
|
| 8 |
|
| 9 |
|
| 10 |
+
def draw_bboxes(
|
| 11 |
+
img: Union[Image.Image, torch.Tensor], bboxes: List[List[Union[int, float]]], *, scaled_bbox: bool = True
|
| 12 |
+
):
|
| 13 |
"""
|
| 14 |
Draw bounding boxes on an image.
|
| 15 |
|
|
|
|
| 32 |
|
| 33 |
for bbox in bboxes:
|
| 34 |
class_id, x_min, y_min, x_max, y_max = bbox
|
| 35 |
+
if scaled_bbox:
|
| 36 |
+
x_min = x_min * width
|
| 37 |
+
x_max = x_max * width
|
| 38 |
+
y_min = y_min * height
|
| 39 |
+
y_max = y_max * height
|
| 40 |
shape = [(x_min, y_min), (x_max, y_max)]
|
| 41 |
draw.rectangle(shape, outline="red", width=3)
|
| 42 |
draw.text((x_min, y_min), str(int(class_id)), font=font, fill="blue")
|
| 43 |
|
| 44 |
img.save("visualize.jpg") # Save the image with annotations
|
| 45 |
logger.info("Saved visualize image at visualize.png")
|
| 46 |
+
return img
|
| 47 |
|
| 48 |
|
| 49 |
def draw_model(*, model_cfg=None, model=None, v7_base=False):
|