Sam10155 commited on
Commit
67efabd
·
1 Parent(s): 4cfc0f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -3
app.py CHANGED
@@ -9,6 +9,7 @@ import supervision as sv
9
  from segment_anything import sam_model_registry, SamPredictor
10
  from werkzeug.utils import secure_filename
11
 
 
12
  import argparse
13
  from io import BytesIO
14
  from PIL import Image
@@ -89,9 +90,10 @@ def run_grounding(input_image, grounding_caption, box_threshold, text_threshold)
89
  image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
90
 
91
  # run sam
92
- boxes_np = boxes.cpu().numpy()
93
- boxes.mask = segment(sam_predictor, image_tensor, boxes_np)
94
- sam_segment = mask_annotator.annotate(scene=np.asarray(image_pil),detections=boxes)
 
95
  sam_output = Image.fromarray(cv2.cvtColor(sam_segment, cv2.COLOR_BGR2RGB))
96
 
97
  return image_with_box, sam_output
 
9
  from segment_anything import sam_model_registry, SamPredictor
10
  from werkzeug.utils import secure_filename
11
 
12
+ import torchvision.ops
13
  import argparse
14
  from io import BytesIO
15
  from PIL import Image
 
90
  image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
91
 
92
  # run sam
93
+ areas = torchvision.ops.boxes.box_area(boxes)
94
+ sorted_indices = torch.flip(torch.argsort(areas), [0])
95
+ sorted_boxes = boxes[sorted_indices]
96
+ sam_segment = mask_annotator.annotate(scene=np.asarray(image_pil), detections=sorted_boxes)
97
  sam_output = Image.fromarray(cv2.cvtColor(sam_segment, cv2.COLOR_BGR2RGB))
98
 
99
  return image_with_box, sam_output