Spaces:
Running
on
Zero
Running
on
Zero
Update models/detection/detector.py
Browse files- models/detection/detector.py +47 -18
models/detection/detector.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
|
@@ -67,36 +68,64 @@ class ObjectDetector:
|
|
67 |
return detections
|
68 |
|
69 |
def draw(self, image: Image.Image, detections, alpha=0.5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
overlay = image.copy()
|
71 |
draw = ImageDraw.Draw(overlay)
|
72 |
|
73 |
-
#
|
74 |
try:
|
75 |
-
font = ImageFont.truetype("arial.ttf",
|
76 |
except:
|
77 |
font = ImageFont.load_default()
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
for det in detections:
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
82 |
|
83 |
-
#
|
84 |
-
for
|
85 |
draw.rectangle(
|
86 |
-
|
87 |
-
outline=
|
88 |
)
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
|
95 |
-
#
|
96 |
-
|
97 |
-
draw.rectangle(
|
98 |
|
99 |
-
#
|
100 |
-
draw.text((
|
101 |
|
102 |
-
|
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import random
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
|
|
|
68 |
return detections
|
69 |
|
70 |
def draw(self, image: Image.Image, detections, alpha=0.5):
|
71 |
+
"""
|
72 |
+
Draws thicker, per-class-colored bounding boxes and labels.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
image (PIL.Image.Image): Original image.
|
76 |
+
detections (List[Dict]): Each dict has "bbox", "class_name", "confidence".
|
77 |
+
alpha (float): Blend strength for overlay.
|
78 |
+
Returns:
|
79 |
+
PIL.Image.Image: Blended image with overlays.
|
80 |
+
"""
|
81 |
+
# copy & overlay
|
82 |
overlay = image.copy()
|
83 |
draw = ImageDraw.Draw(overlay)
|
84 |
|
85 |
+
# try a TTF font, fallback to default
|
86 |
try:
|
87 |
+
font = ImageFont.truetype("arial.ttf", 18)
|
88 |
except:
|
89 |
font = ImageFont.load_default()
|
90 |
|
91 |
+
# deterministic color per class
|
92 |
+
class_colors = {}
|
93 |
+
def get_color(cls):
|
94 |
+
if cls not in class_colors:
|
95 |
+
# seed by class name → same color every run
|
96 |
+
rnd = random.Random(cls)
|
97 |
+
class_colors[cls] = (
|
98 |
+
rnd.randint(100, 255),
|
99 |
+
rnd.randint(100, 255),
|
100 |
+
rnd.randint(100, 255),
|
101 |
+
)
|
102 |
+
return class_colors[cls]
|
103 |
+
|
104 |
for det in detections:
|
105 |
+
x1, y1, x2, y2 = det["bbox"]
|
106 |
+
cls_name = det["class_name"]
|
107 |
+
conf = det["confidence"]
|
108 |
+
label = f"{cls_name} {conf:.2f}"
|
109 |
+
color = get_color(cls_name)
|
110 |
|
111 |
+
# thicker box: draw multiple offsets
|
112 |
+
for t in range(4):
|
113 |
draw.rectangle(
|
114 |
+
(x1 - t, y1 - t, x2 + t, y2 + t),
|
115 |
+
outline=color
|
116 |
)
|
117 |
|
118 |
+
# calculate text size
|
119 |
+
text_box = draw.textbbox((x1, y1), label, font=font)
|
120 |
+
tb_w = text_box[2] - text_box[0]
|
121 |
+
tb_h = text_box[3] - text_box[1]
|
122 |
|
123 |
+
# background rect for text
|
124 |
+
bg = (x1, y1 - tb_h, x1 + tb_w + 6, y1)
|
125 |
+
draw.rectangle(bg, fill=color)
|
126 |
|
127 |
+
# draw text (with small padding)
|
128 |
+
draw.text((x1 + 3, y1 - tb_h), label, font=font, fill="black")
|
129 |
|
130 |
+
# blend and return
|
131 |
+
return Image.blend(image, overlay, alpha)
|