Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -87,6 +87,13 @@ def get_boxes(detection_results: List[DetectionResult]) -> List[List[List[float]
|
|
87 |
boxes.append(xyxy)
|
88 |
return [boxes]
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def refine_masks(masks: torch.BoolTensor, polygon_refinement: bool = False) -> List[np.ndarray]:
|
91 |
masks = masks.cpu().float().permute(0, 2, 3, 1).mean(axis=-1).numpy().astype(np.uint8)
|
92 |
masks = (masks > 0).astype(np.uint8)
|
@@ -94,7 +101,7 @@ def refine_masks(masks: torch.BoolTensor, polygon_refinement: bool = False) -> L
|
|
94 |
for idx, mask in enumerate(masks):
|
95 |
shape = mask.shape
|
96 |
polygon = mask_to_polygon(mask)
|
97 |
-
masks[idx] =
|
98 |
return list(masks)
|
99 |
|
100 |
@spaces.GPU
|
|
|
87 |
boxes.append(xyxy)
|
88 |
return [boxes]
|
89 |
|
90 |
+
def mask_to_polygon(mask: np.ndarray) -> np.ndarray:
|
91 |
+
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
92 |
+
if len(contours) == 0:
|
93 |
+
return np.array([])
|
94 |
+
largest_contour = max(contours, key=cv2.contourArea)
|
95 |
+
return largest_contour
|
96 |
+
|
97 |
def refine_masks(masks: torch.BoolTensor, polygon_refinement: bool = False) -> List[np.ndarray]:
|
98 |
masks = masks.cpu().float().permute(0, 2, 3, 1).mean(axis=-1).numpy().astype(np.uint8)
|
99 |
masks = (masks > 0).astype(np.uint8)
|
|
|
101 |
for idx, mask in enumerate(masks):
|
102 |
shape = mask.shape
|
103 |
polygon = mask_to_polygon(mask)
|
104 |
+
masks[idx] = cv2.fillPoly(np.zeros(shape, dtype=np.uint8), [polygon], 1)
|
105 |
return list(masks)
|
106 |
|
107 |
@spaces.GPU
|