Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,11 @@ except Exception:
|
|
| 30 |
MAX_SIDE_PX = 80 # set >0 (e.g., 70) to filter detections with large side; -1 disables
|
| 31 |
SEG_DEFAULT_ALPHA = 0.45
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Fixed weights (no UI controls). If you want them editable, add Textbox components and wire them as inputs.
|
| 34 |
WEIGHTS_DETECTION = "weights/berries.pt"
|
| 35 |
WEIGHTS_SEGMENTATION = "weights/bunches.pt"
|
|
@@ -220,8 +225,7 @@ def _draw_boxes_overlay(image_rgb: np.ndarray, sahi_result, target_class: str, u
|
|
| 220 |
cls = getattr(item.category, "name", "unknown")
|
| 221 |
is_target = (cls == target_class) if use_target else False
|
| 222 |
|
| 223 |
-
|
| 224 |
-
color_bgr = (0, 0, 255) if is_target and use_target else (0, 200, 0)
|
| 225 |
|
| 226 |
# Draw on overlay (BGR)
|
| 227 |
cv2.rectangle(overlay, (x1, y1), (x2, y2), color_bgr, 2)
|
|
@@ -288,17 +292,17 @@ def _draw_seg_overlay(image_rgb: np.ndarray, yolo_result, target_class: str, use
|
|
| 288 |
if m.shape[:2] != (H, W):
|
| 289 |
m = cv2.resize(m, (W, H), interpolation=cv2.INTER_NEAREST)
|
| 290 |
|
| 291 |
-
overlay_bgr[m == 1] =
|
| 292 |
alpha[m == 1] = np.maximum(alpha[m == 1], fa255)
|
| 293 |
|
| 294 |
cnts, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 295 |
-
cv2.drawContours(overlay_bgr, cnts, -1,
|
| 296 |
cv2.drawContours(alpha, cnts, -1, 255, 2)
|
| 297 |
except Exception:
|
| 298 |
try:
|
| 299 |
xyxy = boxes.xyxy[i].detach().cpu().numpy().astype(int)
|
| 300 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 301 |
-
cv2.rectangle(overlay_bgr, (x1, y1), (x2, y2),
|
| 302 |
cv2.rectangle(alpha, (x1, y1), (x2, y2), 255, 2)
|
| 303 |
except Exception:
|
| 304 |
pass
|
|
@@ -306,7 +310,7 @@ def _draw_seg_overlay(image_rgb: np.ndarray, yolo_result, target_class: str, use
|
|
| 306 |
try:
|
| 307 |
xyxy = boxes.xyxy[i].detach().cpu().numpy().astype(int)
|
| 308 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 309 |
-
cv2.rectangle(overlay_bgr, (x1, y1), (x2, y2),
|
| 310 |
cv2.rectangle(alpha, (x1, y1), (x2, y2), 255, 2)
|
| 311 |
except Exception:
|
| 312 |
pass
|
|
|
|
| 30 |
MAX_SIDE_PX = 80 # set >0 (e.g., 70) to filter detections with large side; -1 disables
|
| 31 |
SEG_DEFAULT_ALPHA = 0.45
|
| 32 |
|
| 33 |
+
# High-contrast colors for green backgrounds (BGR order)
|
| 34 |
+
BERRIES_COLOR_BGR = (255, 0, 255) # magenta/pink for detection boxes
|
| 35 |
+
BUNCHES_FILL_COLOR_BGR = (255, 255, 0) # cyan for mask fill
|
| 36 |
+
BUNCHES_CONTOUR_COLOR_BGR = (255, 255, 255) # white for mask contours
|
| 37 |
+
|
| 38 |
# Fixed weights (no UI controls). If you want them editable, add Textbox components and wire them as inputs.
|
| 39 |
WEIGHTS_DETECTION = "weights/berries.pt"
|
| 40 |
WEIGHTS_SEGMENTATION = "weights/bunches.pt"
|
|
|
|
| 225 |
cls = getattr(item.category, "name", "unknown")
|
| 226 |
is_target = (cls == target_class) if use_target else False
|
| 227 |
|
| 228 |
+
color_bgr = BERRIES_COLOR_BGR
|
|
|
|
| 229 |
|
| 230 |
# Draw on overlay (BGR)
|
| 231 |
cv2.rectangle(overlay, (x1, y1), (x2, y2), color_bgr, 2)
|
|
|
|
| 292 |
if m.shape[:2] != (H, W):
|
| 293 |
m = cv2.resize(m, (W, H), interpolation=cv2.INTER_NEAREST)
|
| 294 |
|
| 295 |
+
overlay_bgr[m == 1] = BUNCHES_FILL_COLOR_BGR
|
| 296 |
alpha[m == 1] = np.maximum(alpha[m == 1], fa255)
|
| 297 |
|
| 298 |
cnts, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 299 |
+
cv2.drawContours(overlay_bgr, cnts, -1, BUNCHES_CONTOUR_COLOR_BGR, 2)
|
| 300 |
cv2.drawContours(alpha, cnts, -1, 255, 2)
|
| 301 |
except Exception:
|
| 302 |
try:
|
| 303 |
xyxy = boxes.xyxy[i].detach().cpu().numpy().astype(int)
|
| 304 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 305 |
+
cv2.rectangle(overlay_bgr, (x1, y1), (x2, y2), BUNCHES_CONTOUR_COLOR_BGR, 2)
|
| 306 |
cv2.rectangle(alpha, (x1, y1), (x2, y2), 255, 2)
|
| 307 |
except Exception:
|
| 308 |
pass
|
|
|
|
| 310 |
try:
|
| 311 |
xyxy = boxes.xyxy[i].detach().cpu().numpy().astype(int)
|
| 312 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 313 |
+
cv2.rectangle(overlay_bgr, (x1, y1), (x2, y2), BUNCHES_CONTOUR_COLOR_BGR, 2)
|
| 314 |
cv2.rectangle(alpha, (x1, y1), (x2, y2), 255, 2)
|
| 315 |
except Exception:
|
| 316 |
pass
|