Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,17 @@ def get_sam_masks(image):
|
|
| 24 |
else:
|
| 25 |
image_np = image
|
| 26 |
image_pil = Image.fromarray(image_np)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
h, w = image_np.shape[:2]
|
| 29 |
|
| 30 |
# Create a grid of points to sample the image
|
|
@@ -52,20 +62,21 @@ def get_sam_masks(image):
|
|
| 52 |
inputs["reshaped_input_sizes"].cpu()
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Combine all masks to create importance map
|
| 56 |
importance_map = np.zeros((h, w), dtype=np.float32)
|
| 57 |
-
individual_masks = []
|
| 58 |
|
| 59 |
for i in range(len(masks[0])):
|
| 60 |
mask = masks[0][i].numpy().astype(np.float32)
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
importance_map += mask
|
| 63 |
|
| 64 |
# Normalize to 0-1
|
| 65 |
if importance_map.max() > 0:
|
| 66 |
importance_map = importance_map / importance_map.max()
|
| 67 |
|
| 68 |
-
return importance_map
|
| 69 |
|
| 70 |
def find_optimal_crop(image, target_ratio, importance_map):
|
| 71 |
"""Find the optimal crop area that preserves important content while matching target ratio"""
|
|
@@ -132,24 +143,23 @@ def apply_crop(image, crop_box):
|
|
| 132 |
else:
|
| 133 |
return image.crop((left, top, right, bottom))
|
| 134 |
|
| 135 |
-
def visualize_importance_map(
|
| 136 |
"""Create a visualization of the importance map overlaid on the image"""
|
| 137 |
-
#
|
| 138 |
if len(importance_map.shape) > 2:
|
| 139 |
-
importance_map = importance_map[:, :, 0]
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
if
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# Convert to colormap
|
| 146 |
heatmap = cv2.applyColorMap((importance_map * 255).astype(np.uint8), cv2.COLORMAP_JET)
|
| 147 |
|
| 148 |
-
# Convert image to BGR
|
| 149 |
-
|
| 150 |
-
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 151 |
-
else:
|
| 152 |
-
image_bgr = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
|
| 153 |
|
| 154 |
# Overlay heatmap on original image
|
| 155 |
overlay = cv2.addWeighted(image_bgr, 0.7, heatmap, 0.3, 0)
|
|
@@ -161,21 +171,22 @@ def visualize_importance_map(image, importance_map):
|
|
| 161 |
|
| 162 |
def adjust_aspect_ratio(image, target_ratio):
|
| 163 |
"""Main function to adjust aspect ratio through intelligent cropping"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# Get segmentation masks and importance map
|
| 165 |
-
importance_map
|
| 166 |
|
| 167 |
# Find optimal crop box
|
| 168 |
-
crop_box = find_optimal_crop(
|
| 169 |
|
| 170 |
# Apply the crop
|
| 171 |
-
result = apply_crop(
|
| 172 |
|
| 173 |
# Create visualization of importance map
|
| 174 |
-
if isinstance(image, np.ndarray):
|
| 175 |
-
image_np = image
|
| 176 |
-
else:
|
| 177 |
-
image_np = np.array(image)
|
| 178 |
-
|
| 179 |
importance_vis = visualize_importance_map(image_np, importance_map)
|
| 180 |
|
| 181 |
return result, importance_vis
|
|
@@ -251,4 +262,4 @@ with gr.Blocks(title="SAM-Based Smart Crop Aspect Ratio Adjuster") as demo:
|
|
| 251 |
|
| 252 |
# Launch the app
|
| 253 |
if __name__ == "__main__":
|
| 254 |
-
demo.launch()
|
|
|
|
| 24 |
else:
|
| 25 |
image_np = image
|
| 26 |
image_pil = Image.fromarray(image_np)
|
| 27 |
+
|
| 28 |
+
# Handle grayscale images
|
| 29 |
+
if len(image_np.shape) == 2:
|
| 30 |
+
# Convert grayscale to RGB
|
| 31 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 32 |
+
image_pil = Image.fromarray(image_np)
|
| 33 |
+
elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
|
| 34 |
+
# Convert RGBA to RGB
|
| 35 |
+
image_np = image_np[:, :, :3]
|
| 36 |
+
image_pil = Image.fromarray(image_np)
|
| 37 |
+
|
| 38 |
h, w = image_np.shape[:2]
|
| 39 |
|
| 40 |
# Create a grid of points to sample the image
|
|
|
|
| 62 |
inputs["reshaped_input_sizes"].cpu()
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Combine all masks to create importance map (ensure 2D)
|
| 66 |
importance_map = np.zeros((h, w), dtype=np.float32)
|
|
|
|
| 67 |
|
| 68 |
for i in range(len(masks[0])):
|
| 69 |
mask = masks[0][i].numpy().astype(np.float32)
|
| 70 |
+
# Ensure mask is 2D
|
| 71 |
+
if len(mask.shape) > 2:
|
| 72 |
+
mask = mask[:, :, 0]
|
| 73 |
importance_map += mask
|
| 74 |
|
| 75 |
# Normalize to 0-1
|
| 76 |
if importance_map.max() > 0:
|
| 77 |
importance_map = importance_map / importance_map.max()
|
| 78 |
|
| 79 |
+
return importance_map
|
| 80 |
|
| 81 |
def find_optimal_crop(image, target_ratio, importance_map):
|
| 82 |
"""Find the optimal crop area that preserves important content while matching target ratio"""
|
|
|
|
| 143 |
else:
|
| 144 |
return image.crop((left, top, right, bottom))
|
| 145 |
|
| 146 |
+
def visualize_importance_map(image_np, importance_map):
|
| 147 |
"""Create a visualization of the importance map overlaid on the image"""
|
| 148 |
+
# Ensure importance_map is 2D
|
| 149 |
if len(importance_map.shape) > 2:
|
| 150 |
+
importance_map = importance_map[:, :, 0]
|
| 151 |
|
| 152 |
+
# Ensure image is RGB
|
| 153 |
+
if len(image_np.shape) == 2:
|
| 154 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 155 |
+
elif image_np.shape[2] == 4:
|
| 156 |
+
image_np = image_np[:, :, :3]
|
| 157 |
|
| 158 |
# Convert to colormap
|
| 159 |
heatmap = cv2.applyColorMap((importance_map * 255).astype(np.uint8), cv2.COLORMAP_JET)
|
| 160 |
|
| 161 |
+
# Convert image to BGR for OpenCV
|
| 162 |
+
image_bgr = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Overlay heatmap on original image
|
| 165 |
overlay = cv2.addWeighted(image_bgr, 0.7, heatmap, 0.3, 0)
|
|
|
|
| 171 |
|
| 172 |
def adjust_aspect_ratio(image, target_ratio):
|
| 173 |
"""Main function to adjust aspect ratio through intelligent cropping"""
|
| 174 |
+
# Convert image to numpy if needed
|
| 175 |
+
if isinstance(image, Image.Image):
|
| 176 |
+
image_np = np.array(image)
|
| 177 |
+
else:
|
| 178 |
+
image_np = image
|
| 179 |
+
|
| 180 |
# Get segmentation masks and importance map
|
| 181 |
+
importance_map = get_sam_masks(image_np)
|
| 182 |
|
| 183 |
# Find optimal crop box
|
| 184 |
+
crop_box = find_optimal_crop(image_np, target_ratio, importance_map)
|
| 185 |
|
| 186 |
# Apply the crop
|
| 187 |
+
result = apply_crop(image_np, crop_box)
|
| 188 |
|
| 189 |
# Create visualization of importance map
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
importance_vis = visualize_importance_map(image_np, importance_map)
|
| 191 |
|
| 192 |
return result, importance_vis
|
|
|
|
| 262 |
|
| 263 |
# Launch the app
|
| 264 |
if __name__ == "__main__":
|
| 265 |
+
demo.launch(share=True) # Set share=True to create a public link
|