CodeJackR
commited on
Commit
·
8b9c502
1
Parent(s):
5407eec
Manage image resizing
Browse files- handler.py +14 -7
handler.py
CHANGED
|
@@ -86,9 +86,20 @@ class EndpointHandler():
|
|
| 86 |
best_mask_idx = min(best_mask_idx.item(), masks.shape[0] - 1)
|
| 87 |
best_mask = masks[best_mask_idx] # Shape: (H, W)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
print("Final mask shape: {}".format(best_mask.shape))
|
| 94 |
else:
|
|
@@ -97,10 +108,6 @@ class EndpointHandler():
|
|
| 97 |
# Convert to numpy and create binary mask
|
| 98 |
mask_binary = (best_mask > 0.0).cpu().numpy().astype(np.uint8) * 255
|
| 99 |
|
| 100 |
-
# Ensure mask_binary is 2D
|
| 101 |
-
if mask_binary.ndim > 2:
|
| 102 |
-
mask_binary = mask_binary.squeeze()
|
| 103 |
-
|
| 104 |
print("Final mask_binary shape: {}".format(mask_binary.shape))
|
| 105 |
|
| 106 |
except Exception as e:
|
|
|
|
| 86 |
best_mask_idx = min(best_mask_idx.item(), masks.shape[0] - 1)
|
| 87 |
best_mask = masks[best_mask_idx] # Shape: (H, W)
|
| 88 |
|
| 89 |
+
# Safely convert to 2D by squeezing all singleton dimensions
|
| 90 |
+
best_mask = best_mask.squeeze()
|
| 91 |
+
|
| 92 |
+
# If still not 2D, take the last 2 dimensions
|
| 93 |
+
if best_mask.ndim > 2:
|
| 94 |
+
# Take the last 2 dimensions which should be height and width
|
| 95 |
+
best_mask = best_mask.view(-1, best_mask.shape[-1])
|
| 96 |
+
elif best_mask.ndim == 1:
|
| 97 |
+
# If somehow we got 1D, try to reshape to square
|
| 98 |
+
size = int(best_mask.shape[0] ** 0.5)
|
| 99 |
+
if size * size == best_mask.shape[0]:
|
| 100 |
+
best_mask = best_mask.view(size, size)
|
| 101 |
+
else:
|
| 102 |
+
raise ValueError("Cannot reshape 1D mask to 2D")
|
| 103 |
|
| 104 |
print("Final mask shape: {}".format(best_mask.shape))
|
| 105 |
else:
|
|
|
|
| 108 |
# Convert to numpy and create binary mask
|
| 109 |
mask_binary = (best_mask > 0.0).cpu().numpy().astype(np.uint8) * 255
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
print("Final mask_binary shape: {}".format(mask_binary.shape))
|
| 112 |
|
| 113 |
except Exception as e:
|