Anigor66 commited on
Commit
2b2d916
·
1 Parent(s): 81144e1
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -448,17 +448,34 @@ def generate_auto_masks(image, request_json):
448
  print(" - Thresholds are still too strict")
449
  print(" - Image size is too small or too large")
450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  # Convert masks to JSON-serializable format
452
  masks_output = []
453
  for m in masks:
454
  mask_data = {
455
- 'segmentation': m['segmentation'].astype(np.uint8).tolist(),
456
- 'area': int(m['area']),
457
- 'bbox': [int(x) for x in m['bbox']], # [x, y, width, height]
458
- 'predicted_iou': float(m['predicted_iou']),
459
- 'point_coords': [[int(p[0]), int(p[1])] for p in m['point_coords']] if m['point_coords'] is not None else [],
460
- 'stability_score': float(m['stability_score']),
461
- 'crop_box': [int(x) for x in m['crop_box']] # [x, y, width, height]
 
 
 
 
462
  }
463
  masks_output.append(mask_data)
464
 
 
448
  print(" - Thresholds are still too strict")
449
  print(" - Image size is too small or too large")
450
 
451
+ # Optionally limit number of masks returned to keep JSON payload reasonable
452
+ max_masks = int(params.get("max_masks", 10))
453
+ if max_masks > 0 and len(masks) > max_masks:
454
+ # Sort by predicted IoU (descending) and keep top-K
455
+ print(f"Limiting masks from {len(masks)} to top {max_masks} by predicted_iou")
456
+ masks = sorted(
457
+ masks,
458
+ key=lambda m: float(m.get("predicted_iou", 0.0)),
459
+ reverse=True,
460
+ )[:max_masks]
461
+
462
+ print(f"Preparing {len(masks)} masks to return to client...")
463
+
464
  # Convert masks to JSON-serializable format
465
  masks_output = []
466
  for m in masks:
467
  mask_data = {
468
+ "segmentation": m["segmentation"].astype(np.uint8).tolist(),
469
+ "area": int(m["area"]),
470
+ "bbox": [int(x) for x in m["bbox"]], # [x, y, width, height]
471
+ "predicted_iou": float(m["predicted_iou"]),
472
+ "point_coords": [
473
+ [int(p[0]), int(p[1])] for p in m["point_coords"]
474
+ ]
475
+ if m["point_coords"] is not None
476
+ else [],
477
+ "stability_score": float(m["stability_score"]),
478
+ "crop_box": [int(x) for x in m["crop_box"]], # [x, y, width, height]
479
  }
480
  masks_output.append(mask_data)
481