Spaces:
Running
on
Zero
Running
on
Zero
Update models/detection/detector.py
Browse files
models/detection/detector.py
CHANGED
@@ -70,7 +70,7 @@ class ObjectDetector:
|
|
70 |
overlay = image.copy()
|
71 |
draw = ImageDraw.Draw(overlay)
|
72 |
|
73 |
-
#
|
74 |
try:
|
75 |
font = ImageFont.truetype("arial.ttf", 16)
|
76 |
except:
|
@@ -87,11 +87,16 @@ class ObjectDetector:
|
|
87 |
outline="red"
|
88 |
)
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
|
|
93 |
|
|
|
|
|
94 |
draw.rectangle(text_bg, fill="red")
|
|
|
|
|
95 |
draw.text((bbox[0] + 2, bbox[1] - text_height), label, fill="white", font=font)
|
96 |
|
97 |
return Image.blend(image, overlay, alpha)
|
|
|
70 |
overlay = image.copy()
|
71 |
draw = ImageDraw.Draw(overlay)
|
72 |
|
73 |
+
# Load font
|
74 |
try:
|
75 |
font = ImageFont.truetype("arial.ttf", 16)
|
76 |
except:
|
|
|
87 |
outline="red"
|
88 |
)
|
89 |
|
90 |
+
# Calculate text size using textbbox
|
91 |
+
text_bbox = draw.textbbox((bbox[0], bbox[1]), label, font=font)
|
92 |
+
text_width = text_bbox[2] - text_bbox[0]
|
93 |
+
text_height = text_bbox[3] - text_bbox[1]
|
94 |
|
95 |
+
# Define background rectangle for text
|
96 |
+
text_bg = [bbox[0], bbox[1] - text_height, bbox[0] + text_width + 4, bbox[1]]
|
97 |
draw.rectangle(text_bg, fill="red")
|
98 |
+
|
99 |
+
# Draw text
|
100 |
draw.text((bbox[0] + 2, bbox[1] - text_height), label, fill="white", font=font)
|
101 |
|
102 |
return Image.blend(image, overlay, alpha)
|