Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -61,23 +61,26 @@ if not mirror_examples and os.path.exists("car.jpeg"):
|
|
| 61 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 62 |
CONF = 0.45
|
| 63 |
|
| 64 |
-
def apply_mask_overlay(img_rgb, mask_bool, color=(0, 215, 255), alpha=0.
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
#
|
|
|
|
|
|
|
|
|
|
| 74 |
mask_img = (mask_bool * 255).astype(np.uint8)
|
| 75 |
contours, _ = cv2.findContours(mask_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
cv2.drawContours(blended, contours, -1, color, 2, cv2.LINE_AA)
|
| 79 |
-
|
| 80 |
-
cv2.drawContours(blended, contours, -1, (255, 255, 255), 1, cv2.LINE_AA)
|
| 81 |
|
| 82 |
return blended
|
| 83 |
|
|
@@ -86,16 +89,33 @@ 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 |
-
cv2.rectangle(out, (x1, y1), (x2, y2), color,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
(
|
| 94 |
-
cv2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
cv2.putText(out, label, (x1 +
|
| 98 |
-
cv2.
|
| 99 |
return out
|
| 100 |
|
| 101 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 61 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 62 |
CONF = 0.45
|
| 63 |
|
| 64 |
+
def apply_mask_overlay(img_rgb, mask_bool, color=(0, 215, 255), alpha=0.4):
|
| 65 |
+
# 1. Cinematic Background (Bokeh/Blur effect + darkened)
|
| 66 |
+
blurred_bg = cv2.GaussianBlur(img_rgb, (35, 35), 0)
|
| 67 |
+
background = cv2.addWeighted(blurred_bg, 0.6, np.zeros_like(img_rgb), 0.4, 0)
|
| 68 |
|
| 69 |
+
# 2. Keep the segmented mask area perfectly sharp and tint it
|
| 70 |
+
tinted_sharp = img_rgb.copy()
|
| 71 |
+
tinted_sharp[mask_bool] = color
|
| 72 |
+
tinted_sharp = cv2.addWeighted(tinted_sharp, alpha, img_rgb, 1 - alpha, 0)
|
| 73 |
|
| 74 |
+
# 3. Combine: Blurred background + Sharp highlighted object
|
| 75 |
+
blended = np.where(mask_bool[:, :, None], tinted_sharp, background)
|
| 76 |
+
|
| 77 |
+
# 4. Multi-layered Glowing Edge for the mask
|
| 78 |
mask_img = (mask_bool * 255).astype(np.uint8)
|
| 79 |
contours, _ = cv2.findContours(mask_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 80 |
|
| 81 |
+
cv2.drawContours(blended, contours, -1, (0, 100, 150), 6, cv2.LINE_AA) # Deep outer shadow/glow
|
| 82 |
+
cv2.drawContours(blended, contours, -1, color, 2, cv2.LINE_AA) # Neon main line
|
| 83 |
+
cv2.drawContours(blended, contours, -1, (255, 255, 255), 1, cv2.LINE_AA) # Bright inner core
|
|
|
|
| 84 |
|
| 85 |
return blended
|
| 86 |
|
|
|
|
| 89 |
for box, label in zip(boxes, labels):
|
| 90 |
x1, y1, x2, y2 = map(int, box)
|
| 91 |
|
| 92 |
+
# Faint inner bounding box line
|
| 93 |
+
cv2.rectangle(out, (x1, y1), (x2, y2), color, 1)
|
| 94 |
+
|
| 95 |
+
# HUD-Style Corner Brackets
|
| 96 |
+
length = int(min(x2 - x1, y2 - y1) * 0.15)
|
| 97 |
+
thick = 3
|
| 98 |
|
| 99 |
+
# Top-Left
|
| 100 |
+
cv2.line(out, (x1, y1), (x1 + length, y1), color, thick, cv2.LINE_AA)
|
| 101 |
+
cv2.line(out, (x1, y1), (x1, y1 + length), color, thick, cv2.LINE_AA)
|
| 102 |
+
# Top-Right
|
| 103 |
+
cv2.line(out, (x2, y1), (x2 - length, y1), color, thick, cv2.LINE_AA)
|
| 104 |
+
cv2.line(out, (x2, y1), (x2, y1 + length), color, thick, cv2.LINE_AA)
|
| 105 |
+
# Bottom-Left
|
| 106 |
+
cv2.line(out, (x1, y2), (x1 + length, y2), color, thick, cv2.LINE_AA)
|
| 107 |
+
cv2.line(out, (x1, y2), (x1, y2 - length), color, thick, cv2.LINE_AA)
|
| 108 |
+
# Bottom-Right
|
| 109 |
+
cv2.line(out, (x2, y2), (x2 - length, y2), color, thick, cv2.LINE_AA)
|
| 110 |
+
cv2.line(out, (x2, y2), (x2, y2 - length), color, thick, cv2.LINE_AA)
|
| 111 |
+
|
| 112 |
+
# Sleek Floating Badge for text (Tesla/Palantir UI style)
|
| 113 |
+
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_DUPLEX, 0.5, 1)
|
| 114 |
+
cv2.rectangle(out, (x1, max(y1 - 32, 0)), (x1 + w + 16, max(y1 - 32, 0) + h + 12), color, -1)
|
| 115 |
|
| 116 |
+
# Black text on Cyan badge gives a very premium, high-contrast look
|
| 117 |
+
cv2.putText(out, label, (x1 + 8, max(y1 - 32, 0) + h + 8),
|
| 118 |
+
cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
|
| 119 |
return out
|
| 120 |
|
| 121 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|