DurgaDeepak commited on
Commit
7374951
·
verified ·
1 Parent(s): 99328a4

Update models/detection/detector.py

Browse files
Files changed (1) hide show
  1. models/detection/detector.py +9 -4
models/detection/detector.py CHANGED
@@ -70,7 +70,7 @@ class ObjectDetector:
70
  overlay = image.copy()
71
  draw = ImageDraw.Draw(overlay)
72
 
73
- # Use a font (try arial, fallback to default)
74
  try:
75
  font = ImageFont.truetype("arial.ttf", 16)
76
  except:
@@ -87,11 +87,16 @@ class ObjectDetector:
87
  outline="red"
88
  )
89
 
90
- # 🧠 Use font.getsize instead of draw.textsize
91
- text_width, text_height = font.getsize(label)
92
- text_bg = [bbox[0], bbox[1] - text_height, bbox[0] + text_width + 4, bbox[1]]
 
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)