Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
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 |
-
#
|
| 90 |
-
|
| 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=
|
| 131 |
boxes, labels = [], []
|
| 132 |
|
| 133 |
if result.masks is not None:
|
| 134 |
-
for
|
| 135 |
-
result.masks.
|
| 136 |
result.boxes.cls, result.boxes.conf
|
| 137 |
):
|
| 138 |
if int(cls) not in target_classes:
|
| 139 |
continue
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
boxes.append(box.cpu().tolist())
|
| 144 |
labels.append(f"glass {conf:.2f}")
|
| 145 |
|
| 146 |
-
|
|
|
|
| 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"
|