Spaces:
Sleeping
Sleeping
Eric P. Nusbaum
commited on
Commit
·
bf53212
1
Parent(s):
e7ef62f
Update Space
Browse files
app.py
CHANGED
@@ -53,14 +53,6 @@ def draw_boxes(image: Image.Image, outputs: dict):
|
|
53 |
|
54 |
# Dynamic font size based on image dimensions
|
55 |
image_width, image_height = image.size
|
56 |
-
font_size = max(20, image_width // 50) # Increased minimum font size
|
57 |
-
try:
|
58 |
-
# Attempt to load a truetype font; adjust the path if necessary
|
59 |
-
font = ImageFont.truetype("arial.ttf", size=font_size)
|
60 |
-
except IOError:
|
61 |
-
# Fallback to default font if truetype font is not found
|
62 |
-
font = ImageFont.load_default()
|
63 |
-
|
64 |
boxes = outputs.get('detected_boxes', [])
|
65 |
classes = outputs.get('detected_classes', [])
|
66 |
scores = outputs.get('detected_scores', [])
|
@@ -88,12 +80,8 @@ def draw_boxes(image: Image.Image, outputs: dict):
|
|
88 |
#And width of the entire bounding box
|
89 |
|
90 |
text_width = right - left
|
91 |
-
text_height = (bottom - top) //
|
92 |
|
93 |
-
# Calculate text size using textbbox
|
94 |
-
#text_bbox = draw.textbbox((0, 0), text, font=font)
|
95 |
-
#text_width = text_bbox[2] - text_bbox[0]
|
96 |
-
#text_height = text_bbox[3] - text_bbox[1]
|
97 |
|
98 |
# Calculate label background position
|
99 |
# Ensure the label box does not go above the image
|
@@ -106,11 +94,18 @@ def draw_boxes(image: Image.Image, outputs: dict):
|
|
106 |
fill=(255, 0, 0, 160) # Semi-transparent red
|
107 |
)
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
# Draw text
|
110 |
draw.text(
|
111 |
(label_left + 5, label_top + 5),
|
112 |
text,
|
113 |
-
fill="
|
114 |
font=font
|
115 |
)
|
116 |
|
|
|
53 |
|
54 |
# Dynamic font size based on image dimensions
|
55 |
image_width, image_height = image.size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
boxes = outputs.get('detected_boxes', [])
|
57 |
classes = outputs.get('detected_classes', [])
|
58 |
scores = outputs.get('detected_scores', [])
|
|
|
80 |
#And width of the entire bounding box
|
81 |
|
82 |
text_width = right - left
|
83 |
+
text_height = (bottom - top) // 20 # 5% of the bounding box height
|
84 |
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Calculate label background position
|
87 |
# Ensure the label box does not go above the image
|
|
|
94 |
fill=(255, 0, 0, 160) # Semi-transparent red
|
95 |
)
|
96 |
|
97 |
+
#Font Size should fill the text box we just drew vertically
|
98 |
+
font_size = 1
|
99 |
+
font = ImageFont.load_default()
|
100 |
+
while font.getsize(text)[1] < text_height:
|
101 |
+
font_size += 1
|
102 |
+
font = ImageFont.load_default().font_variant(size=font_size)
|
103 |
+
|
104 |
# Draw text
|
105 |
draw.text(
|
106 |
(label_left + 5, label_top + 5),
|
107 |
text,
|
108 |
+
fill="black",
|
109 |
font=font
|
110 |
)
|
111 |
|