π§βπ» [Add] drawer to visualize dataset image
Browse files- utils/drawer.py +32 -0
utils/drawer.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ImageDraw, ImageFont
|
2 |
+
|
3 |
+
|
4 |
+
def draw_bboxes(img, bboxes):
|
5 |
+
"""
|
6 |
+
Draw bounding boxes on an image.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
- image_path (str): Path to the image file.
|
10 |
+
- bboxes (list of lists/tuples): Bounding boxes with [x_min, y_min, x_max, y_max, class_id].
|
11 |
+
"""
|
12 |
+
# Load an image
|
13 |
+
draw = ImageDraw.Draw(img)
|
14 |
+
|
15 |
+
# Font for class_id (optional)
|
16 |
+
try:
|
17 |
+
font = ImageFont.truetype("arial.ttf", 15)
|
18 |
+
except IOError:
|
19 |
+
font = ImageFont.load_default()
|
20 |
+
width, height = img.size
|
21 |
+
|
22 |
+
for bbox in bboxes:
|
23 |
+
class_id, x_min, y_min, x_max, y_max = bbox
|
24 |
+
x_min = x_min * width
|
25 |
+
x_max = x_max * width
|
26 |
+
y_min = y_min * height
|
27 |
+
y_max = y_max * height
|
28 |
+
shape = [(x_min, y_min), (x_max, y_max)]
|
29 |
+
draw.rectangle(shape, outline="red", width=2)
|
30 |
+
draw.text((x_min, y_min), str(class_id), font=font, fill="blue")
|
31 |
+
|
32 |
+
img.save("output.jpg")
|