Spaces:
Running
on
Zero
Running
on
Zero
Added validation and handling for image shapes and types
Browse files
hy3dshape/hy3dshape/preprocessors.py
CHANGED
@@ -44,13 +44,34 @@ class ImageProcessorV2:
|
|
44 |
Returns:
|
45 |
ndarray: output image, float/uint8 [H, W, 3/4]
|
46 |
"""
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
mask = image[..., 3]
|
50 |
-
|
|
|
51 |
mask = np.ones_like(image[..., 0:1]) * 255
|
52 |
image = np.concatenate([image, mask], axis=-1)
|
53 |
mask = mask[..., 0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
H, W, C = image.shape
|
56 |
|
@@ -105,7 +126,11 @@ class ImageProcessorV2:
|
|
105 |
# Handle numpy array input
|
106 |
if image.size == 0:
|
107 |
raise ValueError("Input image array is empty")
|
108 |
-
|
|
|
|
|
|
|
|
|
109 |
else:
|
110 |
# Handle any other type by trying to convert to numpy array
|
111 |
try:
|
@@ -119,6 +144,10 @@ class ImageProcessorV2:
|
|
119 |
|
120 |
if image.size == 0:
|
121 |
raise ValueError("Converted image array is empty")
|
|
|
|
|
|
|
|
|
122 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
123 |
except Exception as e:
|
124 |
raise ValueError(f"Could not process image input of type {type(image)}: {e}")
|
|
|
44 |
Returns:
|
45 |
ndarray: output image, float/uint8 [H, W, 3/4]
|
46 |
"""
|
47 |
+
|
48 |
+
# Validate image shape before accessing dimensions
|
49 |
+
if not isinstance(image, np.ndarray):
|
50 |
+
raise ValueError(f"Expected numpy array for image, got {type(image)}")
|
51 |
+
|
52 |
+
# Handle different image shapes
|
53 |
+
if len(image.shape) == 2:
|
54 |
+
# Convert grayscale to RGB
|
55 |
+
image = np.stack([image, image, image], axis=-1)
|
56 |
+
mask = np.ones_like(image[..., 0:1]) * 255
|
57 |
+
image = np.concatenate([image, mask], axis=-1)
|
58 |
+
mask = mask[..., 0]
|
59 |
+
elif len(image.shape) >= 3 and image.shape[-1] == 4:
|
60 |
+
# RGBA image
|
61 |
mask = image[..., 3]
|
62 |
+
elif len(image.shape) >= 3 and image.shape[-1] == 3:
|
63 |
+
# RGB image
|
64 |
mask = np.ones_like(image[..., 0:1]) * 255
|
65 |
image = np.concatenate([image, mask], axis=-1)
|
66 |
mask = mask[..., 0]
|
67 |
+
elif len(image.shape) >= 3 and image.shape[-1] == 1:
|
68 |
+
# Single channel image
|
69 |
+
mask = np.ones_like(image) * 255
|
70 |
+
image = np.concatenate([image, image, image, mask], axis=-1)
|
71 |
+
mask = mask[..., 0]
|
72 |
+
else:
|
73 |
+
# Handle unexpected shape
|
74 |
+
raise ValueError(f"Unexpected image shape: {image.shape}. Expected 2D array or array with 1, 3, or 4 channels.")
|
75 |
|
76 |
H, W, C = image.shape
|
77 |
|
|
|
126 |
# Handle numpy array input
|
127 |
if image.size == 0:
|
128 |
raise ValueError("Input image array is empty")
|
129 |
+
|
130 |
+
try:
|
131 |
+
image, mask = self.recenter(image, border_ratio=border_ratio)
|
132 |
+
except Exception as e:
|
133 |
+
raise ValueError(f"Failed to process numpy array with shape {image.shape}: {str(e)}")
|
134 |
else:
|
135 |
# Handle any other type by trying to convert to numpy array
|
136 |
try:
|
|
|
144 |
|
145 |
if image.size == 0:
|
146 |
raise ValueError("Converted image array is empty")
|
147 |
+
|
148 |
+
# Print debug info before recenter
|
149 |
+
print(f"Debug: image shape before recenter: {image.shape}, type: {type(image)}")
|
150 |
+
|
151 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
152 |
except Exception as e:
|
153 |
raise ValueError(f"Could not process image input of type {type(image)}: {e}")
|