Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,6 +132,33 @@ def apply_crop(image, crop_box):
|
|
| 132 |
else:
|
| 133 |
return image.crop((left, top, right, bottom))
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
def adjust_aspect_ratio(image, target_ratio):
|
| 136 |
"""Main function to adjust aspect ratio through intelligent cropping"""
|
| 137 |
# Get segmentation masks and importance map
|
|
@@ -143,7 +170,15 @@ def adjust_aspect_ratio(image, target_ratio):
|
|
| 143 |
# Apply the crop
|
| 144 |
result = apply_crop(image, crop_box)
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
def process_image(input_image, target_ratio="16:9"):
|
| 149 |
"""Process function for Gradio interface"""
|
|
@@ -155,7 +190,7 @@ def process_image(input_image, target_ratio="16:9"):
|
|
| 155 |
image = input_image
|
| 156 |
|
| 157 |
# Adjust aspect ratio
|
| 158 |
-
result,
|
| 159 |
|
| 160 |
# Convert result to appropriate format
|
| 161 |
if isinstance(result, np.ndarray):
|
|
@@ -163,21 +198,15 @@ def process_image(input_image, target_ratio="16:9"):
|
|
| 163 |
else:
|
| 164 |
result_pil = result
|
| 165 |
|
| 166 |
-
#
|
| 167 |
-
|
| 168 |
-
# Convert to heatmap
|
| 169 |
-
heatmap = (importance_map * 255).astype(np.uint8)
|
| 170 |
-
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 171 |
-
|
| 172 |
-
# Convert to PIL
|
| 173 |
-
heatmap_pil = Image.fromarray(heatmap)
|
| 174 |
-
|
| 175 |
-
return [result_pil, heatmap_pil]
|
| 176 |
|
| 177 |
-
return [result_pil,
|
| 178 |
|
| 179 |
except Exception as e:
|
| 180 |
-
print(f"Error processing image: {e}")
|
|
|
|
|
|
|
| 181 |
return [None, None]
|
| 182 |
|
| 183 |
# Create the Gradio interface
|
|
@@ -200,7 +229,7 @@ with gr.Blocks(title="SAM-Based Smart Crop Aspect Ratio Adjuster") as demo:
|
|
| 200 |
|
| 201 |
with gr.Column():
|
| 202 |
output_image = gr.Image(label="Processed Image")
|
| 203 |
-
importance_map_vis = gr.Image(label="Importance Map (
|
| 204 |
|
| 205 |
submit_btn.click(
|
| 206 |
process_image,
|
|
@@ -211,7 +240,7 @@ with gr.Blocks(title="SAM-Based Smart Crop Aspect Ratio Adjuster") as demo:
|
|
| 211 |
gr.Markdown("""
|
| 212 |
## How it works
|
| 213 |
1. **Segmentation**: Uses Meta's Segment Anything Model (SAM) to identify important regions in your image
|
| 214 |
-
2. **Importance Mapping**: Creates a heatmap of important areas based on segmentation masks
|
| 215 |
3. **Smart Cropping**: Finds the optimal crop window that preserves the most important content
|
| 216 |
|
| 217 |
## Tips
|
|
|
|
| 132 |
else:
|
| 133 |
return image.crop((left, top, right, bottom))
|
| 134 |
|
| 135 |
+
def visualize_importance_map(image, importance_map):
|
| 136 |
+
"""Create a visualization of the importance map overlaid on the image"""
|
| 137 |
+
# Make sure importance_map is 2D
|
| 138 |
+
if len(importance_map.shape) > 2:
|
| 139 |
+
importance_map = importance_map[:, :, 0] # Take first channel if multichannel
|
| 140 |
+
|
| 141 |
+
# Resize importance map to match image dimensions if needed
|
| 142 |
+
if importance_map.shape[:2] != image.shape[:2]:
|
| 143 |
+
importance_map = cv2.resize(importance_map, (image.shape[1], image.shape[0]))
|
| 144 |
+
|
| 145 |
+
# Convert to colormap
|
| 146 |
+
heatmap = cv2.applyColorMap((importance_map * 255).astype(np.uint8), cv2.COLORMAP_JET)
|
| 147 |
+
|
| 148 |
+
# Convert image to BGR if it's RGB
|
| 149 |
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
| 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)
|
| 156 |
+
|
| 157 |
+
# Convert back to RGB for display
|
| 158 |
+
overlay_rgb = cv2.cvtColor(overlay, cv2.COLOR_BGR2RGB)
|
| 159 |
+
|
| 160 |
+
return overlay_rgb
|
| 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
|
|
|
|
| 170 |
# Apply the crop
|
| 171 |
result = apply_crop(image, crop_box)
|
| 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
|
| 182 |
|
| 183 |
def process_image(input_image, target_ratio="16:9"):
|
| 184 |
"""Process function for Gradio interface"""
|
|
|
|
| 190 |
image = input_image
|
| 191 |
|
| 192 |
# Adjust aspect ratio
|
| 193 |
+
result, importance_vis = adjust_aspect_ratio(image, target_ratio)
|
| 194 |
|
| 195 |
# Convert result to appropriate format
|
| 196 |
if isinstance(result, np.ndarray):
|
|
|
|
| 198 |
else:
|
| 199 |
result_pil = result
|
| 200 |
|
| 201 |
+
# Convert importance visualization to PIL
|
| 202 |
+
importance_vis_pil = Image.fromarray(importance_vis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
return [result_pil, importance_vis_pil]
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
+
print(f"Error processing image: {str(e)}")
|
| 208 |
+
import traceback
|
| 209 |
+
traceback.print_exc()
|
| 210 |
return [None, None]
|
| 211 |
|
| 212 |
# Create the Gradio interface
|
|
|
|
| 229 |
|
| 230 |
with gr.Column():
|
| 231 |
output_image = gr.Image(label="Processed Image")
|
| 232 |
+
importance_map_vis = gr.Image(label="Importance Map (Overlay View)")
|
| 233 |
|
| 234 |
submit_btn.click(
|
| 235 |
process_image,
|
|
|
|
| 240 |
gr.Markdown("""
|
| 241 |
## How it works
|
| 242 |
1. **Segmentation**: Uses Meta's Segment Anything Model (SAM) to identify important regions in your image
|
| 243 |
+
2. **Importance Mapping**: Creates a heatmap of important areas based on segmentation masks (shown in the overlay)
|
| 244 |
3. **Smart Cropping**: Finds the optimal crop window that preserves the most important content
|
| 245 |
|
| 246 |
## Tips
|