Spaces:
Running
on
Zero
Running
on
Zero
Update models/detection/detector.py
Browse files- models/detection/detector.py +23 -4
models/detection/detector.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
-
from PIL import Image, ImageDraw
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
|
6 |
logger = logging.getLogger(__name__)
|
@@ -69,9 +69,28 @@ class ObjectDetector:
|
|
69 |
def draw(self, image: Image.Image, detections, alpha=0.5):
|
70 |
overlay = image.copy()
|
71 |
draw = ImageDraw.Draw(overlay)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
for det in detections:
|
73 |
bbox = det["bbox"]
|
74 |
label = f'{det["class_name"]} {det["confidence"]:.2f}'
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
|
6 |
logger = logging.getLogger(__name__)
|
|
|
69 |
def draw(self, image: Image.Image, detections, alpha=0.5):
|
70 |
overlay = image.copy()
|
71 |
draw = ImageDraw.Draw(overlay)
|
72 |
+
|
73 |
+
# Use a basic font (optional: can use truetype for better styling)
|
74 |
+
try:
|
75 |
+
font = ImageFont.truetype("arial.ttf", 16) # Try a common system font
|
76 |
+
except:
|
77 |
+
font = ImageFont.load_default()
|
78 |
+
|
79 |
for det in detections:
|
80 |
bbox = det["bbox"]
|
81 |
label = f'{det["class_name"]} {det["confidence"]:.2f}'
|
82 |
+
|
83 |
+
# Thicker rectangle (3–4 px)
|
84 |
+
for offset in range(3): # creates a thicker border
|
85 |
+
draw.rectangle(
|
86 |
+
[bbox[0]-offset, bbox[1]-offset, bbox[2]+offset, bbox[3]+offset],
|
87 |
+
outline="red"
|
88 |
+
)
|
89 |
+
|
90 |
+
# Draw label with background box
|
91 |
+
text_size = draw.textsize(label, font=font)
|
92 |
+
text_bg = [bbox[0], bbox[1] - text_size[1], bbox[0] + text_size[0] + 4, bbox[1]]
|
93 |
+
draw.rectangle(text_bg, fill="red")
|
94 |
+
draw.text((bbox[0] + 2, bbox[1] - text_size[1]), label, fill="white", font=font)
|
95 |
+
|
96 |
+
return Image.blend(image, overlay, alpha)
|