Spaces:
Running
on
Zero
Running
on
Zero
Restore preprocessors.py to the original file
Browse files
hy3dshape/hy3dshape/preprocessors.py
CHANGED
@@ -35,43 +35,20 @@ class ImageProcessorV2:
|
|
35 |
@staticmethod
|
36 |
def recenter(image, border_ratio: float = 0.2):
|
37 |
""" recenter an image to leave some empty space at the image border.
|
38 |
-
|
39 |
Args:
|
40 |
image (ndarray): input image, float/uint8 [H, W, 3/4]
|
41 |
mask (ndarray): alpha mask, bool [H, W]
|
42 |
border_ratio (float, optional): border ratio, image will be resized to (1 - border_ratio). Defaults to 0.2.
|
43 |
-
|
44 |
Returns:
|
45 |
ndarray: output image, float/uint8 [H, W, 3/4]
|
46 |
"""
|
47 |
-
|
48 |
-
|
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 |
-
|
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 |
|
@@ -109,62 +86,17 @@ class ImageProcessorV2:
|
|
109 |
return result, mask
|
110 |
|
111 |
def load_image(self, image, border_ratio=0.15, to_tensor=True):
|
112 |
-
# Handle different input types
|
113 |
if isinstance(image, str):
|
114 |
-
# Load from file path
|
115 |
image = cv2.imread(image, cv2.IMREAD_UNCHANGED)
|
116 |
-
if image is None:
|
117 |
-
raise ValueError(f"Could not load image from path: {image}")
|
118 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
119 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
120 |
elif isinstance(image, Image.Image):
|
121 |
-
# Convert PIL Image to numpy array
|
122 |
image = image.convert("RGBA")
|
123 |
image = np.asarray(image)
|
124 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
125 |
-
|
126 |
-
|
127 |
-
|
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:
|
137 |
-
if hasattr(image, 'convert'):
|
138 |
-
# Assume it's a PIL-like image
|
139 |
-
image = image.convert("RGBA")
|
140 |
-
image = np.asarray(image)
|
141 |
-
else:
|
142 |
-
# Try direct conversion to numpy array
|
143 |
-
image = np.asarray(image)
|
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}")
|
154 |
-
|
155 |
-
# Validate that we have valid arrays before resizing
|
156 |
-
if not isinstance(image, np.ndarray) or image.size == 0:
|
157 |
-
raise ValueError("Image processing failed - invalid image array")
|
158 |
-
if not isinstance(mask, np.ndarray) or mask.size == 0:
|
159 |
-
raise ValueError("Image processing failed - invalid mask array")
|
160 |
-
|
161 |
-
# Resize with error handling
|
162 |
-
try:
|
163 |
-
image = cv2.resize(image, (self.size, self.size), interpolation=cv2.INTER_CUBIC)
|
164 |
-
mask = cv2.resize(mask, (self.size, self.size), interpolation=cv2.INTER_NEAREST)
|
165 |
-
except cv2.error as e:
|
166 |
-
raise ValueError(f"OpenCV resize failed: {e}. Image shape: {image.shape if hasattr(image, 'shape') else 'unknown'}, Mask shape: {mask.shape if hasattr(mask, 'shape') else 'unknown'}")
|
167 |
-
|
168 |
mask = mask[..., np.newaxis]
|
169 |
|
170 |
if to_tensor:
|
@@ -230,4 +162,4 @@ IMAGE_PROCESSORS = {
|
|
230 |
'mv_v2': MVImageProcessorV2,
|
231 |
}
|
232 |
|
233 |
-
DEFAULT_IMAGEPROCESSOR = 'v2'
|
|
|
35 |
@staticmethod
|
36 |
def recenter(image, border_ratio: float = 0.2):
|
37 |
""" recenter an image to leave some empty space at the image border.
|
|
|
38 |
Args:
|
39 |
image (ndarray): input image, float/uint8 [H, W, 3/4]
|
40 |
mask (ndarray): alpha mask, bool [H, W]
|
41 |
border_ratio (float, optional): border ratio, image will be resized to (1 - border_ratio). Defaults to 0.2.
|
|
|
42 |
Returns:
|
43 |
ndarray: output image, float/uint8 [H, W, 3/4]
|
44 |
"""
|
45 |
+
|
46 |
+
if image.shape[-1] == 4:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
mask = image[..., 3]
|
48 |
+
else:
|
|
|
49 |
mask = np.ones_like(image[..., 0:1]) * 255
|
50 |
image = np.concatenate([image, mask], axis=-1)
|
51 |
mask = mask[..., 0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
H, W, C = image.shape
|
54 |
|
|
|
86 |
return result, mask
|
87 |
|
88 |
def load_image(self, image, border_ratio=0.15, to_tensor=True):
|
|
|
89 |
if isinstance(image, str):
|
|
|
90 |
image = cv2.imread(image, cv2.IMREAD_UNCHANGED)
|
|
|
|
|
91 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
92 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
93 |
elif isinstance(image, Image.Image):
|
|
|
94 |
image = image.convert("RGBA")
|
95 |
image = np.asarray(image)
|
96 |
image, mask = self.recenter(image, border_ratio=border_ratio)
|
97 |
+
|
98 |
+
image = cv2.resize(image, (self.size, self.size), interpolation=cv2.INTER_CUBIC)
|
99 |
+
mask = cv2.resize(mask, (self.size, self.size), interpolation=cv2.INTER_NEAREST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
mask = mask[..., np.newaxis]
|
101 |
|
102 |
if to_tensor:
|
|
|
162 |
'mv_v2': MVImageProcessorV2,
|
163 |
}
|
164 |
|
165 |
+
DEFAULT_IMAGEPROCESSOR = 'v2'
|