Ayesha-Majeed commited on
Commit
54601eb
·
verified ·
1 Parent(s): b4e1358

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -26
app.py CHANGED
@@ -86,26 +86,9 @@ def draw_boxes(img_rgb, boxes, labels, color=(0, 215, 255)):
86
  for box, label in zip(boxes, labels):
87
  x1, y1, x2, y2 = map(int, box)
88
 
89
- # Fancy stylized corners for the bounding box
90
- length = int(min(x2 - x1, y2 - y1) * 0.2)
91
- thickness = 3
92
 
93
- # Draw faint full rectangle
94
- cv2.rectangle(out, (x1, y1), (x2, y2), color, 1)
95
-
96
- # Top-left corner
97
- cv2.line(out, (x1, y1), (x1 + length, y1), color, thickness, cv2.LINE_AA)
98
- cv2.line(out, (x1, y1), (x1, y1 + length), color, thickness, cv2.LINE_AA)
99
- # Top-right corner
100
- cv2.line(out, (x2, y1), (x2 - length, y1), color, thickness, cv2.LINE_AA)
101
- cv2.line(out, (x2, y1), (x2, y1 + length), color, thickness, cv2.LINE_AA)
102
- # Bottom-left corner
103
- cv2.line(out, (x1, y2), (x1 + length, y2), color, thickness, cv2.LINE_AA)
104
- cv2.line(out, (x1, y2), (x1, y2 - length), color, thickness, cv2.LINE_AA)
105
- # Bottom-right corner
106
- cv2.line(out, (x2, y2), (x2 - length, y2), color, thickness, cv2.LINE_AA)
107
- cv2.line(out, (x2, y2), (x2, y2 - length), color, thickness, cv2.LINE_AA)
108
-
109
  # Draw a solid background for the text label for better readability
110
  (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
111
  cv2.rectangle(out, (x1, max(y1 - 25, 0)), (x1 + w + 10, max(y1 - 25, 0) + h + 10), color, -1)
@@ -127,23 +110,26 @@ def run_yolo_generic(img_rgb, model_path, target_classes, color):
127
 
128
  result = results[0]
129
  h, w = img_rgb.shape[:2]
130
- combined_mask = np.zeros((h, w), dtype=bool)
131
  boxes, labels = [], []
132
 
133
  if result.masks is not None:
134
- for mask, box, cls, conf in zip(
135
- result.masks.data, result.boxes.xyxy,
136
  result.boxes.cls, result.boxes.conf
137
  ):
138
  if int(cls) not in target_classes:
139
  continue
140
- mask_np = mask.cpu().numpy()
141
- mask_r = cv2.resize(mask_np, (w, h), interpolation=cv2.INTER_NEAREST)
142
- combined_mask |= mask_r > 0.5
 
 
143
  boxes.append(box.cpu().tolist())
144
  labels.append(f"glass {conf:.2f}")
145
 
146
- out = apply_mask_overlay(img_rgb, combined_mask, color=color)
 
147
  out = draw_boxes(out, boxes, labels, color=color)
148
  bw_mask = (combined_mask * 255).astype(np.uint8)
149
  return out, bw_mask, f"Found: {len(boxes)} | Inference Time: {elapsed:.2f}s"
 
86
  for box, label in zip(boxes, labels):
87
  x1, y1, x2, y2 = map(int, box)
88
 
89
+ # Solid continuous bounding box
90
+ cv2.rectangle(out, (x1, y1), (x2, y2), color, 2)
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # Draw a solid background for the text label for better readability
93
  (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
94
  cv2.rectangle(out, (x1, max(y1 - 25, 0)), (x1 + w + 10, max(y1 - 25, 0) + h + 10), color, -1)
 
110
 
111
  result = results[0]
112
  h, w = img_rgb.shape[:2]
113
+ combined_mask = np.zeros((h, w), dtype=np.uint8)
114
  boxes, labels = [], []
115
 
116
  if result.masks is not None:
117
+ for pts, box, cls, conf in zip(
118
+ result.masks.xy, result.boxes.xyxy,
119
  result.boxes.cls, result.boxes.conf
120
  ):
121
  if int(cls) not in target_classes:
122
  continue
123
+
124
+ # Use polygon points perfectly mapped to original image dimensions
125
+ if len(pts) > 0:
126
+ cv2.fillPoly(combined_mask, [np.int32(pts)], 1)
127
+
128
  boxes.append(box.cpu().tolist())
129
  labels.append(f"glass {conf:.2f}")
130
 
131
+ combined_mask_bool = combined_mask > 0
132
+ out = apply_mask_overlay(img_rgb, combined_mask_bool, color=color)
133
  out = draw_boxes(out, boxes, labels, color=color)
134
  bw_mask = (combined_mask * 255).astype(np.uint8)
135
  return out, bw_mask, f"Found: {len(boxes)} | Inference Time: {elapsed:.2f}s"