Spaces:
Sleeping
Sleeping
Víctor Sáez
commited on
Commit
·
363fa1b
1
Parent(s):
ae3ae3e
solve error in fonts
Browse files- app.py +9 -12
- requirements.txt +0 -0
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
5 |
-
from pathlib import Path
|
6 |
import transformers
|
7 |
|
8 |
# Global variables to cache models
|
@@ -38,13 +37,12 @@ def load_model(model_key):
|
|
38 |
return current_model, current_processor
|
39 |
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
font = ImageFont.truetype(str(font_path), size=8) # Reduced font size
|
48 |
|
49 |
# Set up translations for the app
|
50 |
translations = {
|
@@ -245,24 +243,23 @@ def detect_objects(image, language_selector, translated_model_selector, threshol
|
|
245 |
'box': box
|
246 |
})
|
247 |
|
248 |
-
# Calculate text position and size
|
249 |
try:
|
250 |
-
|
251 |
image_width = image.size[0]
|
252 |
# Calculate the font size for drawing labels, ensuring it scales with image width but is never smaller than 50 pixels.
|
253 |
font_size = max(image_width // 40, 12) # Adjust font size based on image width
|
254 |
-
font =
|
255 |
|
256 |
text_bbox = draw.textbbox((0, 0), display_text, font=font)
|
257 |
text_width = text_bbox[2] - text_bbox[0]
|
258 |
text_height = text_bbox[3] - text_bbox[1]
|
259 |
except:
|
260 |
# Fallback for older PIL versions
|
|
|
261 |
text_bbox = draw.textbbox((0, 0), display_text, font=font)
|
262 |
text_width = text_bbox[2] - text_bbox[0]
|
263 |
text_height = text_bbox[3] - text_bbox[1]
|
264 |
|
265 |
-
|
266 |
# Draw text background
|
267 |
text_bg = [
|
268 |
box[0], box[1] - text_height - 4,
|
|
|
2 |
import torch
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
|
|
5 |
import transformers
|
6 |
|
7 |
# Global variables to cache models
|
|
|
37 |
return current_model, current_processor
|
38 |
|
39 |
|
40 |
+
# Fixed font loading - this was the main issue
|
41 |
+
def get_font(size=12):
|
42 |
+
try:
|
43 |
+
return ImageFont.truetype("arial.ttf", size=size)
|
44 |
+
except:
|
45 |
+
return ImageFont.load_default()
|
|
|
46 |
|
47 |
# Set up translations for the app
|
48 |
translations = {
|
|
|
243 |
'box': box
|
244 |
})
|
245 |
|
246 |
+
# Calculate text position and size - FIXED FONT USAGE
|
247 |
try:
|
|
|
248 |
image_width = image.size[0]
|
249 |
# Calculate the font size for drawing labels, ensuring it scales with image width but is never smaller than 50 pixels.
|
250 |
font_size = max(image_width // 40, 12) # Adjust font size based on image width
|
251 |
+
font = get_font(font_size) # Use the fixed font function
|
252 |
|
253 |
text_bbox = draw.textbbox((0, 0), display_text, font=font)
|
254 |
text_width = text_bbox[2] - text_bbox[0]
|
255 |
text_height = text_bbox[3] - text_bbox[1]
|
256 |
except:
|
257 |
# Fallback for older PIL versions
|
258 |
+
font = get_font(12) # Use the fixed font function
|
259 |
text_bbox = draw.textbbox((0, 0), display_text, font=font)
|
260 |
text_width = text_bbox[2] - text_bbox[0]
|
261 |
text_height = text_bbox[3] - text_bbox[1]
|
262 |
|
|
|
263 |
# Draw text background
|
264 |
text_bg = [
|
265 |
box[0], box[1] - text_height - 4,
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|