Spaces:
Build error
Build error
Miroslav Purkrabek commited on
Commit ·
b76961c
1
Parent(s): 928cdff
show only masks > 1%
Browse files- app.py +0 -16
- demo/demo_utils.py +17 -17
- mmpose/datasets/transforms/common_transforms.py +0 -2
app.py
CHANGED
|
@@ -3,15 +3,11 @@ import spaces
|
|
| 3 |
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
-
# import os
|
| 7 |
-
# os.system("pip install .")
|
| 8 |
-
|
| 9 |
import numpy as np
|
| 10 |
import yaml
|
| 11 |
from demo.demo_utils import DotDict, concat_instances, filter_instances, pose_nms, visualize_demo
|
| 12 |
from demo.mm_utils import run_MMDetector, run_MMPose
|
| 13 |
from mmdet.apis import init_detector
|
| 14 |
-
from mmengine.logging import print_log
|
| 15 |
from demo.sam2_utils import prepare_model as prepare_sam2_model
|
| 16 |
from demo.sam2_utils import process_image_with_SAM
|
| 17 |
|
|
@@ -96,7 +92,6 @@ def process_image_with_BMP(
|
|
| 96 |
rtmdet_result = None
|
| 97 |
all_detections = None
|
| 98 |
for iteration in range(bmp_config.num_bmp_iters):
|
| 99 |
-
print_log("BMP Iteration {}/{} started".format(iteration + 1, bmp_config.num_bmp_iters), logger="current")
|
| 100 |
|
| 101 |
# Step 1: Detection
|
| 102 |
det_instances = run_MMDetector(
|
|
@@ -106,9 +101,7 @@ def process_image_with_BMP(
|
|
| 106 |
bbox_thr=DEFAULT_BBOX_THR,
|
| 107 |
nms_thr=DEFAULT_NMS_THR,
|
| 108 |
)
|
| 109 |
-
print_log("Detected {} instances".format(len(det_instances.bboxes)), logger="current")
|
| 110 |
if len(det_instances.bboxes) == 0:
|
| 111 |
-
print_log("No detections found, skipping.", logger="current")
|
| 112 |
continue
|
| 113 |
|
| 114 |
# Step 2: Pose estimation
|
|
@@ -149,7 +142,6 @@ def process_image_with_BMP(
|
|
| 149 |
keep_new_indices = [i - num_old_detections for i in keep_indices if i >= num_old_detections]
|
| 150 |
keep_old_indices = [i for i in keep_indices if i < num_old_detections]
|
| 151 |
if len(keep_new_indices) == 0:
|
| 152 |
-
print_log("No new instances passed pose NMS, skipping SAM refinement.", logger="current")
|
| 153 |
continue
|
| 154 |
# filter new detections and compute scores
|
| 155 |
new_dets = filter_instances(pose_instances, keep_new_indices)
|
|
@@ -157,12 +149,6 @@ def process_image_with_BMP(
|
|
| 157 |
old_dets = None
|
| 158 |
if len(keep_old_indices) > 0:
|
| 159 |
old_dets = filter_instances(all_detections, keep_old_indices)
|
| 160 |
-
print_log(
|
| 161 |
-
"Pose NMS reduced instances to {:d} ({:d}+{:d}) instances".format(
|
| 162 |
-
len(new_dets.bboxes) + num_old_detections, num_old_detections, len(new_dets.bboxes)
|
| 163 |
-
),
|
| 164 |
-
logger="current",
|
| 165 |
-
)
|
| 166 |
|
| 167 |
new_detections = process_image_with_SAM(
|
| 168 |
DotDict(bmp_config.sam2.prompting),
|
|
@@ -186,8 +172,6 @@ def process_image_with_BMP(
|
|
| 186 |
|
| 187 |
if iteration == 0:
|
| 188 |
rtmdet_result = rtmdet_r
|
| 189 |
-
print_log("Iteration {} completed".format(iteration + 1), logger="current")
|
| 190 |
-
|
| 191 |
|
| 192 |
_, _, bmp_result = visualize_demo(
|
| 193 |
img.copy(),
|
|
|
|
| 3 |
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import yaml
|
| 8 |
from demo.demo_utils import DotDict, concat_instances, filter_instances, pose_nms, visualize_demo
|
| 9 |
from demo.mm_utils import run_MMDetector, run_MMPose
|
| 10 |
from mmdet.apis import init_detector
|
|
|
|
| 11 |
from demo.sam2_utils import prepare_model as prepare_sam2_model
|
| 12 |
from demo.sam2_utils import process_image_with_SAM
|
| 13 |
|
|
|
|
| 92 |
rtmdet_result = None
|
| 93 |
all_detections = None
|
| 94 |
for iteration in range(bmp_config.num_bmp_iters):
|
|
|
|
| 95 |
|
| 96 |
# Step 1: Detection
|
| 97 |
det_instances = run_MMDetector(
|
|
|
|
| 101 |
bbox_thr=DEFAULT_BBOX_THR,
|
| 102 |
nms_thr=DEFAULT_NMS_THR,
|
| 103 |
)
|
|
|
|
| 104 |
if len(det_instances.bboxes) == 0:
|
|
|
|
| 105 |
continue
|
| 106 |
|
| 107 |
# Step 2: Pose estimation
|
|
|
|
| 142 |
keep_new_indices = [i - num_old_detections for i in keep_indices if i >= num_old_detections]
|
| 143 |
keep_old_indices = [i for i in keep_indices if i < num_old_detections]
|
| 144 |
if len(keep_new_indices) == 0:
|
|
|
|
| 145 |
continue
|
| 146 |
# filter new detections and compute scores
|
| 147 |
new_dets = filter_instances(pose_instances, keep_new_indices)
|
|
|
|
| 149 |
old_dets = None
|
| 150 |
if len(keep_old_indices) > 0:
|
| 151 |
old_dets = filter_instances(all_detections, keep_old_indices)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
new_detections = process_image_with_SAM(
|
| 154 |
DotDict(bmp_config.sam2.prompting),
|
|
|
|
| 172 |
|
| 173 |
if iteration == 0:
|
| 174 |
rtmdet_result = rtmdet_r
|
|
|
|
|
|
|
| 175 |
|
| 176 |
_, _, bmp_result = visualize_demo(
|
| 177 |
img.copy(),
|
demo/demo_utils.py
CHANGED
|
@@ -145,23 +145,23 @@ def _visualize_predictions(
|
|
| 145 |
"""
|
| 146 |
vis_types = vis_type.split("+")
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
|
| 166 |
if mask_is_binary:
|
| 167 |
poly_masks: List[Optional[List[np.ndarray]]] = []
|
|
|
|
| 145 |
"""
|
| 146 |
vis_types = vis_type.split("+")
|
| 147 |
|
| 148 |
+
# Filter-out small detections to make the visualization more clear
|
| 149 |
+
new_bboxes = []
|
| 150 |
+
new_scores = []
|
| 151 |
+
new_masks = []
|
| 152 |
+
new_poses = []
|
| 153 |
+
size_thr = img.shape[0] * img.shape[1] * 0.01
|
| 154 |
+
for bbox, score, mask, pose in zip(bboxes, scores, masks, poses):
|
| 155 |
+
area = mask.sum() # Assume binary mask. OK for demo purposes
|
| 156 |
+
if area > size_thr:
|
| 157 |
+
new_bboxes.append(bbox)
|
| 158 |
+
new_scores.append(score)
|
| 159 |
+
new_masks.append(mask)
|
| 160 |
+
new_poses.append(pose)
|
| 161 |
+
bboxes = np.array(new_bboxes)
|
| 162 |
+
scores = np.array(new_scores)
|
| 163 |
+
masks = new_masks
|
| 164 |
+
poses = new_poses
|
| 165 |
|
| 166 |
if mask_is_binary:
|
| 167 |
poly_masks: List[Optional[List[np.ndarray]]] = []
|
mmpose/datasets/transforms/common_transforms.py
CHANGED
|
@@ -291,10 +291,8 @@ class MaskBackground(BaseTransform):
|
|
| 291 |
|
| 292 |
if mask is None and not self.continue_on_failure:
|
| 293 |
raise ValueError('No mask found in the results and self.continue_on_failure is set to False.')
|
| 294 |
-
print("\n\nI'm inside the MaskBackground transform")
|
| 295 |
|
| 296 |
if mask is not None and self._do_masking():
|
| 297 |
-
print("MaskBackground transform will take place now\n\n")
|
| 298 |
# Convert mask from polygons to binary mask
|
| 299 |
try:
|
| 300 |
mask_rle = Mask.frPyObjects(mask, results['img_shape'][0], results['img_shape'][1])
|
|
|
|
| 291 |
|
| 292 |
if mask is None and not self.continue_on_failure:
|
| 293 |
raise ValueError('No mask found in the results and self.continue_on_failure is set to False.')
|
|
|
|
| 294 |
|
| 295 |
if mask is not None and self._do_masking():
|
|
|
|
| 296 |
# Convert mask from polygons to binary mask
|
| 297 |
try:
|
| 298 |
mask_rle = Mask.frPyObjects(mask, results['img_shape'][0], results['img_shape'][1])
|