Subh775 commited on
Commit
8eb3166
·
verified ·
1 Parent(s): 7622e4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -336,7 +336,6 @@
336
  # debug=False
337
  # )
338
 
339
-
340
  import os
341
  import io
342
  import base64
@@ -458,10 +457,17 @@ def encode_pil_to_dataurl(pil_img: Image.Image, fmt="PNG") -> str:
458
  return "data:image/{};base64,".format(fmt.lower()) + base64.b64encode(buf.getvalue()).decode("ascii")
459
 
460
 
461
- def annotate_segmentation(image: Image.Image, detections: sv.Detections) -> Image.Image:
 
462
  """
463
  Annotate image with segmentation masks using supervision library.
464
  This matches the visualization from rfdetr_seg_infer.py script.
 
 
 
 
 
 
465
  """
466
  try:
467
  # Define color palette
@@ -478,29 +484,37 @@ def annotate_segmentation(image: Image.Image, detections: sv.Detections) -> Imag
478
  # Create annotators
479
  mask_annotator = sv.MaskAnnotator(color=palette)
480
  polygon_annotator = sv.PolygonAnnotator(color=sv.Color.WHITE)
481
- label_annotator = sv.LabelAnnotator(
482
- color=palette,
483
- text_color=sv.Color.BLACK,
484
- text_scale=text_scale,
485
- text_position=sv.Position.CENTER_OF_MASS
486
- )
487
-
488
- # Create labels with confidence scores
489
- labels = [
490
- f"Tulsi {float(conf):.2f}"
491
- for conf in detections.confidence
492
- ]
493
-
494
- print(f"[INFO] Annotating {len(labels)} detections")
495
-
496
- # Apply annotations step by step
497
  out = image.copy()
498
  print("[INFO] Applying mask annotation...")
499
  out = mask_annotator.annotate(out, detections)
500
  print("[INFO] Applying polygon annotation...")
501
  out = polygon_annotator.annotate(out, detections)
502
- print("[INFO] Applying label annotation...")
503
- out = label_annotator.annotate(out, detections, labels)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
 
505
  print("[INFO] Annotation complete")
506
  return out
@@ -537,7 +551,7 @@ def predict():
537
  """
538
  Accepts:
539
  - multipart/form-data with file field "file"
540
- - or JSON {"image": "<data:url...>", "conf": 0.25, "show_labels": true, "show_confidence": true}
541
  Returns JSON:
542
  {"annotated": "<data:image/png;base64,...>", "confidences": [..], "count": N}
543
  """
@@ -588,6 +602,7 @@ def predict():
588
  show_confidence = payload.get("show_confidence", True)
589
 
590
  print(f"[INFO] Image size: {img.size}, Confidence threshold: {conf_threshold}")
 
591
 
592
  # Optionally downscale large images to reduce memory usage
593
  MAX_SIZE = 1024
@@ -628,7 +643,7 @@ def predict():
628
 
629
  # Annotate image using supervision library
630
  print("[INFO] Starting annotation...")
631
- annotated_pil = annotate_segmentation(img, detections)
632
 
633
  # Extract confidence scores
634
  confidences = [float(conf) for conf in detections.confidence]
 
336
  # debug=False
337
  # )
338
 
 
339
  import os
340
  import io
341
  import base64
 
457
  return "data:image/{};base64,".format(fmt.lower()) + base64.b64encode(buf.getvalue()).decode("ascii")
458
 
459
 
460
+ def annotate_segmentation(image: Image.Image, detections: sv.Detections,
461
+ show_labels: bool = True, show_confidence: bool = True) -> Image.Image:
462
  """
463
  Annotate image with segmentation masks using supervision library.
464
  This matches the visualization from rfdetr_seg_infer.py script.
465
+
466
+ Args:
467
+ image: Input PIL Image
468
+ detections: Supervision Detections object
469
+ show_labels: Whether to show "Tulsi" label text
470
+ show_confidence: Whether to show confidence scores
471
  """
472
  try:
473
  # Define color palette
 
484
  # Create annotators
485
  mask_annotator = sv.MaskAnnotator(color=palette)
486
  polygon_annotator = sv.PolygonAnnotator(color=sv.Color.WHITE)
487
+
488
+ # Apply base annotations (masks and polygons always shown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  out = image.copy()
490
  print("[INFO] Applying mask annotation...")
491
  out = mask_annotator.annotate(out, detections)
492
  print("[INFO] Applying polygon annotation...")
493
  out = polygon_annotator.annotate(out, detections)
494
+
495
+ # Only add labels if at least one option is enabled
496
+ if show_labels or show_confidence:
497
+ label_annotator = sv.LabelAnnotator(
498
+ color=palette,
499
+ text_color=sv.Color.BLACK,
500
+ text_scale=text_scale,
501
+ text_position=sv.Position.CENTER_OF_MASS
502
+ )
503
+
504
+ # Create labels based on options
505
+ labels = []
506
+ for conf in detections.confidence:
507
+ label_parts = []
508
+ if show_labels:
509
+ label_parts.append("Tulsi")
510
+ if show_confidence:
511
+ label_parts.append(f"{float(conf):.2f}")
512
+ labels.append(" ".join(label_parts))
513
+
514
+ print(f"[INFO] Applying label annotation with {len(labels)} labels...")
515
+ out = label_annotator.annotate(out, detections, labels)
516
+ else:
517
+ print("[INFO] Skipping label annotation (both labels and confidence disabled)")
518
 
519
  print("[INFO] Annotation complete")
520
  return out
 
551
  """
552
  Accepts:
553
  - multipart/form-data with file field "file"
554
+ - or JSON {"image": "<data:url...>", "conf": 0.05, "show_labels": true, "show_confidence": true}
555
  Returns JSON:
556
  {"annotated": "<data:image/png;base64,...>", "confidences": [..], "count": N}
557
  """
 
602
  show_confidence = payload.get("show_confidence", True)
603
 
604
  print(f"[INFO] Image size: {img.size}, Confidence threshold: {conf_threshold}")
605
+ print(f"[INFO] Display options - Labels: {show_labels}, Confidence: {show_confidence}")
606
 
607
  # Optionally downscale large images to reduce memory usage
608
  MAX_SIZE = 1024
 
643
 
644
  # Annotate image using supervision library
645
  print("[INFO] Starting annotation...")
646
+ annotated_pil = annotate_segmentation(img, detections, show_labels, show_confidence)
647
 
648
  # Extract confidence scores
649
  confidences = [float(conf) for conf in detections.confidence]