DurgaDeepak commited on
Commit
03c9511
·
verified ·
1 Parent(s): 99ad111

Update models/detection/detector.py

Browse files
Files changed (1) hide show
  1. models/detection/detector.py +47 -18
models/detection/detector.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import logging
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  from huggingface_hub import hf_hub_download
5
 
@@ -67,36 +68,64 @@ class ObjectDetector:
67
  return detections
68
 
69
  def draw(self, image: Image.Image, detections, alpha=0.5):
 
 
 
 
 
 
 
 
 
 
 
70
  overlay = image.copy()
71
  draw = ImageDraw.Draw(overlay)
72
 
73
- # Load font
74
  try:
75
- font = ImageFont.truetype("arial.ttf", 16)
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
- # Draw thicker bounding box
84
- for offset in range(3):
85
  draw.rectangle(
86
- [bbox[0] - offset, bbox[1] - offset, bbox[2] + offset, bbox[3] + offset],
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)
 
 
1
  import os
2
  import logging
3
+ import random
4
  from PIL import Image, ImageDraw, ImageFont
5
  from huggingface_hub import hf_hub_download
6
 
 
68
  return detections
69
 
70
  def draw(self, image: Image.Image, detections, alpha=0.5):
71
+ """
72
+ Draws thicker, per-class-colored bounding boxes and labels.
73
+
74
+ Args:
75
+ image (PIL.Image.Image): Original image.
76
+ detections (List[Dict]): Each dict has "bbox", "class_name", "confidence".
77
+ alpha (float): Blend strength for overlay.
78
+ Returns:
79
+ PIL.Image.Image: Blended image with overlays.
80
+ """
81
+ # copy & overlay
82
  overlay = image.copy()
83
  draw = ImageDraw.Draw(overlay)
84
 
85
+ # try a TTF font, fallback to default
86
  try:
87
+ font = ImageFont.truetype("arial.ttf", 18)
88
  except:
89
  font = ImageFont.load_default()
90
 
91
+ # deterministic color per class
92
+ class_colors = {}
93
+ def get_color(cls):
94
+ if cls not in class_colors:
95
+ # seed by class name → same color every run
96
+ rnd = random.Random(cls)
97
+ class_colors[cls] = (
98
+ rnd.randint(100, 255),
99
+ rnd.randint(100, 255),
100
+ rnd.randint(100, 255),
101
+ )
102
+ return class_colors[cls]
103
+
104
  for det in detections:
105
+ x1, y1, x2, y2 = det["bbox"]
106
+ cls_name = det["class_name"]
107
+ conf = det["confidence"]
108
+ label = f"{cls_name} {conf:.2f}"
109
+ color = get_color(cls_name)
110
 
111
+ # thicker box: draw multiple offsets
112
+ for t in range(4):
113
  draw.rectangle(
114
+ (x1 - t, y1 - t, x2 + t, y2 + t),
115
+ outline=color
116
  )
117
 
118
+ # calculate text size
119
+ text_box = draw.textbbox((x1, y1), label, font=font)
120
+ tb_w = text_box[2] - text_box[0]
121
+ tb_h = text_box[3] - text_box[1]
122
 
123
+ # background rect for text
124
+ bg = (x1, y1 - tb_h, x1 + tb_w + 6, y1)
125
+ draw.rectangle(bg, fill=color)
126
 
127
+ # draw text (with small padding)
128
+ draw.text((x1 + 3, y1 - tb_h), label, font=font, fill="black")
129
 
130
+ # blend and return
131
+ return Image.blend(image, overlay, alpha)