Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,197 +1,248 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
import cv2
|
|
|
|
|
|
|
| 6 |
from transformers import SamModel, SamProcessor
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
print(f"Using device: {device}")
|
| 11 |
|
| 12 |
# Load SAM model and processor
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
def
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
image: Input image as numpy array
|
| 23 |
-
target_ratio: Desired width/height ratio
|
| 24 |
-
Returns:
|
| 25 |
-
Cropped image
|
| 26 |
"""
|
| 27 |
-
# Convert
|
| 28 |
-
if
|
| 29 |
-
image =
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Generate masks
|
| 56 |
-
with torch.no_grad():
|
| 57 |
-
outputs = sam_model(**inputs)
|
| 58 |
-
|
| 59 |
-
# Get masks from model output
|
| 60 |
-
masks = outputs.pred_masks.squeeze().cpu().numpy()
|
| 61 |
-
|
| 62 |
-
# Combine all masks to get regions of interest
|
| 63 |
-
combined_mask = np.any(masks, axis=0)
|
| 64 |
-
|
| 65 |
-
# Find the bounding box of important content
|
| 66 |
-
y_indices, x_indices = np.where(combined_mask)
|
| 67 |
-
if len(x_indices) == 0 or len(y_indices) == 0:
|
| 68 |
-
# If no important content detected, use center crop
|
| 69 |
-
return center_crop(image, target_ratio)
|
| 70 |
-
|
| 71 |
-
x_min, x_max = x_indices.min(), x_indices.max()
|
| 72 |
-
y_min, y_max = y_indices.min(), y_indices.max()
|
| 73 |
-
|
| 74 |
-
# Calculate new dimensions to match target ratio while including important content
|
| 75 |
-
content_width = x_max - x_min
|
| 76 |
-
content_height = y_max - y_min
|
| 77 |
-
|
| 78 |
-
if target_ratio > current_ratio:
|
| 79 |
-
# Need wider image
|
| 80 |
-
new_width = min(width, int(height * target_ratio))
|
| 81 |
-
new_height = height
|
| 82 |
else:
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Adjust if crop goes out of bounds
|
| 97 |
-
if x1 == 0:
|
| 98 |
-
x2 = new_width
|
| 99 |
-
if x2 == width:
|
| 100 |
-
x1 = width - new_width
|
| 101 |
-
if y1 == 0:
|
| 102 |
-
y2 = new_height
|
| 103 |
-
if y2 == height:
|
| 104 |
-
y1 = height - new_height
|
| 105 |
-
|
| 106 |
-
return image[y1:y2, x1:x2]
|
| 107 |
|
| 108 |
-
def
|
| 109 |
-
"""
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
else:
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
"
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
def process_image(input_image,
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if isinstance(input_image, str):
|
| 139 |
-
input_image = np.array(Image.open(input_image))
|
| 140 |
|
| 141 |
-
# Get
|
| 142 |
-
|
| 143 |
|
| 144 |
-
#
|
| 145 |
-
|
| 146 |
|
| 147 |
-
return
|
| 148 |
|
| 149 |
-
|
| 150 |
-
with gr.Blocks(title="Smart Image Cropper") as
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
["example_image.jpg", "Portrait (4:5)"]
|
| 190 |
-
],
|
| 191 |
-
inputs=[input_image, ratio_choice]
|
| 192 |
-
)
|
| 193 |
-
)
|
| 194 |
|
| 195 |
-
#
|
| 196 |
if __name__ == "__main__":
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
| 5 |
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
from transformers import SamModel, SamProcessor
|
| 9 |
+
import warnings
|
| 10 |
+
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
+
# Check if CUDA is available
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
print(f"Using device: {device}")
|
| 15 |
|
| 16 |
# Load SAM model and processor
|
| 17 |
+
model_id = "facebook/sam-vit-base"
|
| 18 |
+
processor = SamProcessor.from_pretrained(model_id)
|
| 19 |
+
model = SamModel.from_pretrained(model_id).to(device)
|
| 20 |
|
| 21 |
+
def get_sam_mask(image, points=None):
|
| 22 |
"""
|
| 23 |
+
Generate mask from SAM model based on the entire image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
+
# Convert to RGB if needed
|
| 26 |
+
if image.mode != "RGB":
|
| 27 |
+
image = image.convert("RGB")
|
| 28 |
+
|
| 29 |
+
# Process image with SAM
|
| 30 |
+
if points is None:
|
| 31 |
+
# Generate automatic masks for the whole image
|
| 32 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model(**inputs)
|
| 35 |
+
|
| 36 |
+
# Get the best mask (highest IoU)
|
| 37 |
+
masks = processor.image_processor.post_process_masks(
|
| 38 |
+
outputs.pred_masks.cpu(),
|
| 39 |
+
inputs["original_sizes"].cpu(),
|
| 40 |
+
inputs["reshaped_input_sizes"].cpu()
|
| 41 |
+
)[0][0]
|
| 42 |
+
|
| 43 |
+
# Convert to binary mask and return the largest mask
|
| 44 |
+
masks = masks.numpy()
|
| 45 |
+
if masks.shape[0] > 0:
|
| 46 |
+
# Calculate area of each mask and get the largest one
|
| 47 |
+
areas = [np.sum(mask) for mask in masks]
|
| 48 |
+
largest_mask_idx = np.argmax(areas)
|
| 49 |
+
return masks[largest_mask_idx].astype(np.uint8) * 255
|
| 50 |
+
else:
|
| 51 |
+
# If no masks found, return full image mask
|
| 52 |
+
return np.ones((image.height, image.width), dtype=np.uint8) * 255
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
+
# Use the provided points to generate a mask
|
| 55 |
+
inputs = processor(images=image, input_points=[points], return_tensors="pt").to(device)
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
outputs = model(**inputs)
|
| 58 |
+
|
| 59 |
+
# Get the mask
|
| 60 |
+
masks = processor.image_processor.post_process_masks(
|
| 61 |
+
outputs.pred_masks.cpu(),
|
| 62 |
+
inputs["original_sizes"].cpu(),
|
| 63 |
+
inputs["reshaped_input_sizes"].cpu()
|
| 64 |
+
)[0][0]
|
| 65 |
+
|
| 66 |
+
return masks[0].numpy().astype(np.uint8) * 255
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
def find_optimal_crop(image, mask, target_aspect_ratio):
|
| 69 |
+
"""
|
| 70 |
+
Find the optimal crop that preserves important content based on the mask
|
| 71 |
+
"""
|
| 72 |
+
# Convert PIL image to numpy array
|
| 73 |
+
image_np = np.array(image)
|
| 74 |
+
h, w = mask.shape
|
| 75 |
+
|
| 76 |
+
# Calculate target dimensions
|
| 77 |
+
if target_aspect_ratio > w / h:
|
| 78 |
+
# Target is wider than original
|
| 79 |
+
target_h = int(w / target_aspect_ratio)
|
| 80 |
+
target_w = w
|
| 81 |
else:
|
| 82 |
+
# Target is taller than original
|
| 83 |
+
target_h = h
|
| 84 |
+
target_w = int(h * target_aspect_ratio)
|
| 85 |
+
|
| 86 |
+
# Find the best crop position
|
| 87 |
+
best_score = -1
|
| 88 |
+
best_box = (0, 0, target_w, target_h)
|
| 89 |
+
|
| 90 |
+
# Step size for searching (can be adjusted for performance)
|
| 91 |
+
step = max(1, min(h, w) // 50)
|
| 92 |
+
|
| 93 |
+
for y in range(0, h - target_h + 1, step):
|
| 94 |
+
for x in range(0, w - target_w + 1, step):
|
| 95 |
+
# Calculate how much of the mask is preserved in this crop
|
| 96 |
+
crop_mask = mask[y:y+target_h, x:x+target_w]
|
| 97 |
+
score = np.sum(crop_mask) / 255.0
|
| 98 |
+
|
| 99 |
+
if score > best_score:
|
| 100 |
+
best_score = score
|
| 101 |
+
best_box = (x, y, x + target_w, y + target_h)
|
| 102 |
+
|
| 103 |
+
return best_box
|
| 104 |
+
|
| 105 |
+
def smart_crop(input_image, target_aspect_ratio, point_x=None, point_y=None):
|
| 106 |
+
"""
|
| 107 |
+
Main function to perform smart cropping
|
| 108 |
+
"""
|
| 109 |
+
if input_image is None:
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
+
# Open image and convert to RGB
|
| 113 |
+
pil_image = Image.fromarray(input_image) if isinstance(input_image, np.ndarray) else input_image
|
| 114 |
+
if pil_image.mode != "RGB":
|
| 115 |
+
pil_image = pil_image.convert("RGB")
|
| 116 |
+
|
| 117 |
+
# Generate mask using SAM
|
| 118 |
+
points = None
|
| 119 |
+
if point_x is not None and point_y is not None and point_x > 0 and point_y > 0:
|
| 120 |
+
points = [[point_x, point_y]]
|
| 121 |
+
|
| 122 |
+
mask = get_sam_mask(pil_image, points)
|
| 123 |
|
| 124 |
+
# Calculate the best crop
|
| 125 |
+
crop_box = find_optimal_crop(pil_image, mask, target_aspect_ratio)
|
| 126 |
|
| 127 |
+
# Crop the image
|
| 128 |
+
cropped_img = pil_image.crop(crop_box)
|
| 129 |
+
|
| 130 |
+
# Visualize the process
|
| 131 |
+
fig, ax = plt.subplots(1, 3, figsize=(15, 5))
|
| 132 |
+
ax[0].imshow(pil_image)
|
| 133 |
+
ax[0].set_title("Original Image")
|
| 134 |
+
ax[0].axis("off")
|
| 135 |
+
|
| 136 |
+
ax[1].imshow(mask, cmap='gray')
|
| 137 |
+
ax[1].set_title("SAM Segmentation Mask")
|
| 138 |
+
ax[1].axis("off")
|
| 139 |
+
|
| 140 |
+
ax[2].imshow(cropped_img)
|
| 141 |
+
ax[2].set_title(f"Smart Cropped ({target_aspect_ratio:.2f})")
|
| 142 |
+
ax[2].axis("off")
|
| 143 |
+
|
| 144 |
+
plt.tight_layout()
|
| 145 |
+
|
| 146 |
+
# Create a temporary file for visualization
|
| 147 |
+
vis_path = "visualization.png"
|
| 148 |
+
plt.savefig(vis_path)
|
| 149 |
+
plt.close()
|
| 150 |
+
|
| 151 |
+
return cropped_img, vis_path
|
| 152 |
|
| 153 |
+
def aspect_ratio_options(choice):
|
| 154 |
+
"""Map aspect ratio choices to actual values"""
|
| 155 |
+
options = {
|
| 156 |
+
"16:9 (Landscape)": 16/9,
|
| 157 |
+
"9:16 (Portrait)": 9/16,
|
| 158 |
+
"4:3 (Standard)": 4/3,
|
| 159 |
+
"3:4 (Portrait)": 3/4,
|
| 160 |
+
"1:1 (Square)": 1/1,
|
| 161 |
+
"21:9 (Ultrawide)": 21/9,
|
| 162 |
+
"2:3 (Portrait)": 2/3,
|
| 163 |
+
"3:2 (Landscape)": 3/2,
|
| 164 |
+
}
|
| 165 |
+
return options.get(choice, 16/9)
|
| 166 |
|
| 167 |
+
def process_image(input_image, aspect_ratio_choice, point_x=None, point_y=None):
|
| 168 |
+
if input_image is None:
|
| 169 |
+
return None, None
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# Get the actual aspect ratio value
|
| 172 |
+
target_aspect_ratio = aspect_ratio_options(aspect_ratio_choice)
|
| 173 |
|
| 174 |
+
# Process the image
|
| 175 |
+
result_img, vis_path = smart_crop(input_image, target_aspect_ratio, point_x, point_y)
|
| 176 |
|
| 177 |
+
return result_img, vis_path
|
| 178 |
|
| 179 |
+
def create_app():
|
| 180 |
+
with gr.Blocks(title="Smart Image Cropper using SAM") as app:
|
| 181 |
+
gr.Markdown("# Smart Image Cropper using Segment Anything Model (SAM)")
|
| 182 |
+
gr.Markdown("""
|
| 183 |
+
Upload an image and choose your target aspect ratio. The app will use the Segment Anything Model (SAM)
|
| 184 |
+
to identify important content and crop intelligently to preserve it.
|
| 185 |
+
|
| 186 |
+
Optionally, you can click on the uploaded image to specify a point of interest.
|
| 187 |
+
""")
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
with gr.Column(scale=1):
|
| 191 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
| 192 |
+
aspect_ratio = gr.Dropdown(
|
| 193 |
+
choices=[
|
| 194 |
+
"16:9 (Landscape)",
|
| 195 |
+
"9:16 (Portrait)",
|
| 196 |
+
"4:3 (Standard)",
|
| 197 |
+
"3:4 (Portrait)",
|
| 198 |
+
"1:1 (Square)",
|
| 199 |
+
"21:9 (Ultrawide)",
|
| 200 |
+
"2:3 (Portrait)",
|
| 201 |
+
"3:2 (Landscape)"
|
| 202 |
+
],
|
| 203 |
+
value="16:9 (Landscape)",
|
| 204 |
+
label="Target Aspect Ratio"
|
| 205 |
+
)
|
| 206 |
+
point_coords = gr.State(value=[None, None])
|
| 207 |
+
|
| 208 |
+
def update_coords(img, evt: gr.SelectData):
|
| 209 |
+
return [evt.index[0], evt.index[1]]
|
| 210 |
+
|
| 211 |
+
input_image.select(update_coords, inputs=[input_image], outputs=[point_coords])
|
| 212 |
+
|
| 213 |
+
process_btn = gr.Button("Process Image")
|
| 214 |
|
| 215 |
+
with gr.Column(scale=2):
|
| 216 |
+
output_image = gr.Image(type="pil", label="Cropped Result")
|
| 217 |
+
visualization = gr.Image(type="filepath", label="Process Visualization")
|
| 218 |
+
|
| 219 |
+
process_btn.click(
|
| 220 |
+
fn=lambda img, ratio, coords: process_image(img, ratio, coords[0], coords[1]),
|
| 221 |
+
inputs=[input_image, aspect_ratio, point_coords],
|
| 222 |
+
outputs=[output_image, visualization]
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
gr.Markdown("""
|
| 226 |
+
## How It Works
|
| 227 |
|
| 228 |
+
1. The Segment Anything Model (SAM) analyzes your image to identify the important content
|
| 229 |
+
2. The app finds the optimal crop window that maximizes the preservation of that content
|
| 230 |
+
3. The image is cropped to your desired aspect ratio while keeping the important parts
|
| 231 |
+
|
| 232 |
+
## Tips
|
| 233 |
+
|
| 234 |
+
- For better results with specific subjects, click on the important object in the image
|
| 235 |
+
- Try different aspect ratios to see how the model adapts the cropping
|
| 236 |
+
""")
|
| 237 |
+
|
| 238 |
+
return app
|
| 239 |
+
|
| 240 |
+
# Create and launch the app
|
| 241 |
+
demo = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
# For local testing
|
| 244 |
if __name__ == "__main__":
|
| 245 |
+
demo.launch()
|
| 246 |
+
else:
|
| 247 |
+
# For Hugging Face Spaces
|
| 248 |
+
demo.launch()
|