Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -208,109 +208,4 @@ with gr.Blocks(
|
|
| 208 |
)
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True)
|
| 212 |
-
```
|
| 213 |
-
|
| 214 |
-
=== utils.py ===
|
| 215 |
-
```python
|
| 216 |
-
import torch
|
| 217 |
-
import numpy as np
|
| 218 |
-
from PIL import Image
|
| 219 |
-
import matplotlib
|
| 220 |
-
import requests
|
| 221 |
-
from io import BytesIO
|
| 222 |
-
|
| 223 |
-
def load_image_from_url(url: str) -> Image.Image:
|
| 224 |
-
"""
|
| 225 |
-
Load an image from a URL.
|
| 226 |
-
|
| 227 |
-
Args:
|
| 228 |
-
url: Image URL
|
| 229 |
-
|
| 230 |
-
Returns:
|
| 231 |
-
PIL Image object
|
| 232 |
-
"""
|
| 233 |
-
try:
|
| 234 |
-
response = requests.get(url, timeout=10)
|
| 235 |
-
response.raise_for_status()
|
| 236 |
-
image = Image.open(BytesIO(response.content))
|
| 237 |
-
return image.convert("RGB")
|
| 238 |
-
except Exception as e:
|
| 239 |
-
raise ValueError(f"Could not load image from URL: {str(e)}")
|
| 240 |
-
|
| 241 |
-
def validate_image(image: Image.Image) -> bool:
|
| 242 |
-
"""
|
| 243 |
-
Validate if the image is suitable for processing.
|
| 244 |
-
|
| 245 |
-
Args:
|
| 246 |
-
image: PIL Image object
|
| 247 |
-
|
| 248 |
-
Returns:
|
| 249 |
-
True if valid, False otherwise
|
| 250 |
-
"""
|
| 251 |
-
if image is None:
|
| 252 |
-
return False
|
| 253 |
-
|
| 254 |
-
if image.size[0] <= 0 or image.size[1] <= 0:
|
| 255 |
-
return False
|
| 256 |
-
|
| 257 |
-
return True
|
| 258 |
-
|
| 259 |
-
def resize_for_processing(image: Image.Image, max_size: int = 1024) -> Image.Image:
|
| 260 |
-
"""
|
| 261 |
-
Resize image for processing while maintaining aspect ratio.
|
| 262 |
-
|
| 263 |
-
Args:
|
| 264 |
-
image: Input PIL Image
|
| 265 |
-
max_size: Maximum size for the longer dimension
|
| 266 |
-
|
| 267 |
-
Returns:
|
| 268 |
-
Resized PIL Image
|
| 269 |
-
"""
|
| 270 |
-
width, height = image.size
|
| 271 |
-
if max(width, height) <= max_size:
|
| 272 |
-
return image
|
| 273 |
-
|
| 274 |
-
if width > height:
|
| 275 |
-
new_width = max_size
|
| 276 |
-
new_height = int(height * max_size / width)
|
| 277 |
-
else:
|
| 278 |
-
new_height = max_size
|
| 279 |
-
new_width = int(width * max_size / height)
|
| 280 |
-
|
| 281 |
-
return image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 282 |
-
|
| 283 |
-
def overlay_masks_advanced(image: Image.Image, masks: torch.Tensor, alpha: float = 0.5) -> Image.Image:
|
| 284 |
-
"""
|
| 285 |
-
Advanced overlay function with customizable alpha.
|
| 286 |
-
|
| 287 |
-
Args:
|
| 288 |
-
image: Input PIL Image
|
| 289 |
-
masks: Segmentation masks tensor
|
| 290 |
-
alpha: Overlay transparency (0-1)
|
| 291 |
-
|
| 292 |
-
Returns:
|
| 293 |
-
Overlaid PIL Image
|
| 294 |
-
"""
|
| 295 |
-
image = image.convert("RGBA")
|
| 296 |
-
masks = 255 * masks.cpu().numpy().astype(np.uint8)
|
| 297 |
-
|
| 298 |
-
n_masks = masks.shape[0]
|
| 299 |
-
if n_masks == 0:
|
| 300 |
-
return image.convert("RGB")
|
| 301 |
-
|
| 302 |
-
# Use a good colormap
|
| 303 |
-
cmap = matplotlib.colormaps.get_cmap("tab10").resampled(n_masks)
|
| 304 |
-
colors = [
|
| 305 |
-
tuple(int(c * 255) for c in cmap(i)[:3])
|
| 306 |
-
for i in range(n_masks)
|
| 307 |
-
]
|
| 308 |
-
|
| 309 |
-
for mask, color in zip(masks, colors):
|
| 310 |
-
mask_img = Image.fromarray(mask)
|
| 311 |
-
overlay = Image.new("RGBA", image.size, color + (0,))
|
| 312 |
-
alpha_map = mask_img.point(lambda v: int(v * alpha * 255))
|
| 313 |
-
overlay.putalpha(alpha_map)
|
| 314 |
-
image = Image.alpha_composite(image, overlay)
|
| 315 |
-
|
| 316 |
-
return image
|
|
|
|
| 208 |
)
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|