Update app.py
Browse files
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 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 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
|