Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,220 +1,277 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
import numpy as np
|
| 6 |
-
|
| 7 |
-
from ultralytics import FastSAM
|
| 8 |
-
import supervision as sv
|
| 9 |
import os
|
| 10 |
-
import
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# --- Constants and Model Initialization ---
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
CLIP_MODEL_NAME = "openai/clip-vit-base-patch32"
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
DEFAULT_RETINA_MASKS = False
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
"""Downloads a file from a URL with a progress bar."""
|
| 33 |
-
response = requests.get(url, stream=True)
|
| 34 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
for data in response.iter_content(block_size):
|
| 42 |
-
progress_bar.update(len(data))
|
| 43 |
-
file.write(data)
|
| 44 |
-
progress_bar.close()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
# Load FastSAM model with dynamic device handling
|
| 56 |
-
if not os.path.exists(FASTSAM_WEIGHTS_NAME):
|
| 57 |
-
print(f"Downloading FastSAM weights from {FASTSAM_WEIGHTS_URL}...")
|
| 58 |
-
try:
|
| 59 |
-
download_file(FASTSAM_WEIGHTS_URL, FASTSAM_WEIGHTS_NAME)
|
| 60 |
-
print("FastSAM weights downloaded successfully.")
|
| 61 |
except Exception as e:
|
| 62 |
-
print(f"Error
|
| 63 |
-
|
| 64 |
|
| 65 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 66 |
-
fast_sam = FastSAM(FASTSAM_WEIGHTS_NAME)
|
| 67 |
-
fast_sam.to(device)
|
| 68 |
-
print(f"FastSAM loaded on device: {device}")
|
| 69 |
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# ... (Your CLIP processing function remains the same) ...
|
| 74 |
-
if image is None:
|
| 75 |
-
return "Please upload an image first."
|
| 76 |
-
if not text_input:
|
| 77 |
-
return "Please enter some text to check in the image."
|
| 78 |
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
if
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# Create a list of candidate labels
|
| 85 |
-
candidate_labels = [text_input, f"not {text_input}"]
|
| 86 |
-
|
| 87 |
-
# Process image and text
|
| 88 |
-
inputs = processor(
|
| 89 |
-
images=image,
|
| 90 |
-
text=candidate_labels,
|
| 91 |
-
return_tensors="pt",
|
| 92 |
-
padding=True
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
# Get model predictions
|
| 96 |
-
outputs = model(**{k: v for k, v in inputs.items()})
|
| 97 |
-
logits_per_image = outputs.logits_per_image
|
| 98 |
-
probs = logits_per_image.softmax(dim=1)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
return f"Error processing image: {str(e)}"
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
image_np = np.array(image)
|
| 114 |
-
else:
|
| 115 |
-
image_np = image
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
if results is None or len(results) == 0 or results[0] is None:
|
| 122 |
-
return None, "FastSAM did not return valid results. Try adjusting parameters or using a different image."
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
return None, "Error: Out of memory. Try reducing the image size (imgsz) or disabling retina masks."
|
| 143 |
-
else:
|
| 144 |
-
return None, f"Runtime error during FastSAM processing: {str(re)}"
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
# --- Gradio Interface ---
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
""
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
-
output_text = gr.Textbox(label="Result")
|
| 170 |
-
classify_btn = gr.Button("Classify")
|
| 171 |
-
classify_btn.click(fn=process_image_clip, inputs=[image_input, text_input], outputs=output_text)
|
| 172 |
-
|
| 173 |
-
gr.Examples(
|
| 174 |
-
examples=[
|
| 175 |
-
["https://raw.githubusercontent.com/gradio-app/gradio/main/demo/kitchen/kitchen.png", "kitchen"],
|
| 176 |
-
["https://raw.githubusercontent.com/gradio-app/gradio/main/demo/calculator/calculator.jpg", "calculator"],
|
| 177 |
-
],
|
| 178 |
-
inputs=[image_input, text_input],
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
with gr.Tab("FastSAM Segmentation"):
|
| 182 |
-
with gr.Row():
|
| 183 |
-
image_input_sam = gr.Image(label="Input Image")
|
| 184 |
-
with gr.Column():
|
| 185 |
-
imgsz_slider = gr.Slider(minimum=320, maximum=1920, step=32, value=DEFAULT_IMGSZ, label="Image Size (imgsz)")
|
| 186 |
-
conf_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=DEFAULT_CONFIDENCE, label="Confidence Threshold")
|
| 187 |
-
iou_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=DEFAULT_IOU, label="IoU Threshold")
|
| 188 |
-
retina_checkbox = gr.Checkbox(label="Retina Masks", value=DEFAULT_RETINA_MASKS)
|
| 189 |
-
|
| 190 |
-
with gr.Row():
|
| 191 |
-
image_output = gr.Image(label="Segmentation Result")
|
| 192 |
-
error_output = gr.Textbox(label="Error Message", type="text") # Added for displaying errors
|
| 193 |
-
|
| 194 |
-
segment_btn = gr.Button("Segment")
|
| 195 |
-
segment_btn.click(
|
| 196 |
-
fn=process_image_fastsam,
|
| 197 |
-
inputs=[image_input_sam, imgsz_slider, conf_slider, iou_slider, retina_checkbox],
|
| 198 |
-
outputs=[image_output, error_output] # Output to both image and error textboxes
|
| 199 |
-
)
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoProcessor, AutoModel
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import numpy as np
|
| 6 |
+
import random
|
|
|
|
|
|
|
| 7 |
import os
|
| 8 |
+
import wget # To download weights
|
| 9 |
+
|
| 10 |
+
# --- Configuration & Model Loading ---
|
| 11 |
+
|
| 12 |
+
# Device Selection
|
| 13 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
print(f"Using device: {DEVICE}")
|
| 15 |
+
|
| 16 |
+
# --- CLIP Setup ---
|
| 17 |
+
CLIP_MODEL_ID = "openai/clip-vit-base-patch32"
|
| 18 |
+
clip_processor = None
|
| 19 |
+
clip_model = None
|
| 20 |
+
|
| 21 |
+
def load_clip_model():
|
| 22 |
+
global clip_processor, clip_model
|
| 23 |
+
if clip_processor is None:
|
| 24 |
+
print(f"Loading CLIP processor: {CLIP_MODEL_ID}...")
|
| 25 |
+
clip_processor = AutoProcessor.from_pretrained(CLIP_MODEL_ID)
|
| 26 |
+
print("CLIP processor loaded.")
|
| 27 |
+
if clip_model is None:
|
| 28 |
+
print(f"Loading CLIP model: {CLIP_MODEL_ID}...")
|
| 29 |
+
clip_model = AutoModel.from_pretrained(CLIP_MODEL_ID).to(DEVICE)
|
| 30 |
+
print(f"CLIP model loaded to {DEVICE}.")
|
| 31 |
+
|
| 32 |
+
# --- FastSAM Setup ---
|
| 33 |
+
# Use a smaller model suitable for Spaces CPU/basic GPU if needed
|
| 34 |
+
FASTSAM_CHECKPOINT = "FastSAM-s.pt"
|
| 35 |
+
FASTSAM_CHECKPOINT_URL = f"https://huggingface.co/spaces/An-619/FastSAM/resolve/main/{FASTSAM_CHECKPOINT}" # Example URL, find official if possible
|
| 36 |
+
|
| 37 |
+
fastsam_model = None
|
| 38 |
+
|
| 39 |
+
def download_fastsam_weights():
|
| 40 |
+
if not os.path.exists(FASTSAM_CHECKPOINT):
|
| 41 |
+
print(f"Downloading FastSAM weights: {FASTSAM_CHECKPOINT}...")
|
| 42 |
+
try:
|
| 43 |
+
wget.download(FASTSAM_CHECKPOINT_URL, FASTSAM_CHECKPOINT)
|
| 44 |
+
print("FastSAM weights downloaded.")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error downloading FastSAM weights: {e}")
|
| 47 |
+
print("Please ensure the URL is correct and reachable, or manually place the weights file.")
|
| 48 |
+
return False
|
| 49 |
+
return os.path.exists(FASTSAM_CHECKPOINT)
|
| 50 |
+
|
| 51 |
+
def load_fastsam_model():
|
| 52 |
+
global fastsam_model
|
| 53 |
+
if fastsam_model is None:
|
| 54 |
+
if download_fastsam_weights():
|
| 55 |
+
try:
|
| 56 |
+
from fastsam import FastSAM, FastSAMPrompt # Import here after potential download
|
| 57 |
+
print(f"Loading FastSAM model: {FASTSAM_CHECKPOINT}...")
|
| 58 |
+
fastsam_model = FastSAM(FASTSAM_CHECKPOINT)
|
| 59 |
+
print(f"FastSAM model loaded.") # Device handled internally by FastSAM based on its setup/torch device
|
| 60 |
+
except ImportError:
|
| 61 |
+
print("Error: 'fastsam' library not found. Please install it (pip install fastsam).")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Error loading FastSAM model: {e}")
|
| 64 |
+
else:
|
| 65 |
+
print("FastSAM weights not found. Cannot load model.")
|
| 66 |
|
|
|
|
| 67 |
|
| 68 |
+
# --- Processing Functions ---
|
|
|
|
| 69 |
|
| 70 |
+
# CLIP Zero-Shot Classification Function
|
| 71 |
+
def run_clip_zero_shot(image: Image.Image, text_labels: str):
|
| 72 |
+
if clip_model is None or clip_processor is None:
|
| 73 |
+
load_clip_model() # Attempt to load if not already loaded
|
| 74 |
+
if clip_model is None:
|
| 75 |
+
return "Error: CLIP Model not loaded. Check logs.", None
|
| 76 |
|
| 77 |
+
if not text_labels:
|
| 78 |
+
return "Please provide comma-separated text labels.", None
|
| 79 |
+
if image is None:
|
| 80 |
+
return "Please upload an image.", None
|
|
|
|
| 81 |
|
| 82 |
+
labels = [label.strip() for label in text_labels.split(',')]
|
| 83 |
+
if not labels:
|
| 84 |
+
return "No valid labels provided.", None
|
| 85 |
|
| 86 |
+
print(f"Running CLIP zero-shot classification with labels: {labels}")
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
try:
|
| 89 |
+
# Ensure image is RGB
|
| 90 |
+
if image.mode != "RGB":
|
| 91 |
+
image = image.convert("RGB")
|
| 92 |
|
| 93 |
+
inputs = clip_processor(text=labels, images=image, return_tensors="pt", padding=True).to(DEVICE)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
with torch.no_grad():
|
| 96 |
+
outputs = clip_model(**inputs)
|
| 97 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
| 98 |
+
probs = logits_per_image.softmax(dim=1) # convert to probabilities
|
| 99 |
|
| 100 |
+
print("CLIP processing complete.")
|
| 101 |
|
| 102 |
+
# Format output for Gradio Label
|
| 103 |
+
confidences = {labels[i]: float(probs[0, i].item()) for i in range(len(labels))}
|
| 104 |
+
return confidences, image # Return original image for display alongside results
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
+
print(f"Error during CLIP processing: {e}")
|
| 108 |
+
return f"An error occurred: {e}", None
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# FastSAM Segmentation Function
|
| 112 |
+
def run_fastsam_segmentation(image_pil: Image.Image, conf_threshold: float = 0.4, iou_threshold: float = 0.9):
|
| 113 |
+
if fastsam_model is None:
|
| 114 |
+
load_fastsam_model() # Attempt to load if not already loaded
|
| 115 |
+
if fastsam_model is None:
|
| 116 |
+
return "Error: FastSAM Model not loaded. Check logs.", None
|
| 117 |
+
if image_pil is None:
|
| 118 |
+
return "Please upload an image.", None
|
| 119 |
|
| 120 |
+
print("Running FastSAM segmentation...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
try:
|
| 123 |
+
# Ensure image is RGB
|
| 124 |
+
if image_pil.mode != "RGB":
|
| 125 |
+
image_pil = image_pil.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# FastSAM expects a BGR numpy array or path usually. Let's try with RGB numpy.
|
| 128 |
+
# If it fails, uncomment the BGR conversion line.
|
| 129 |
+
image_np_rgb = np.array(image_pil)
|
| 130 |
+
# image_np_bgr = image_np_rgb[:, :, ::-1] # Convert RGB to BGR if needed
|
|
|
|
| 131 |
|
| 132 |
+
# Run FastSAM inference
|
| 133 |
+
# Adjust imgsz, conf, iou as needed. Higher imgsz = more detail, slower.
|
| 134 |
+
everything_results = fastsam_model(
|
| 135 |
+
image_np_rgb, # Use image_np_bgr if conversion needed
|
| 136 |
+
device=DEVICE,
|
| 137 |
+
retina_masks=True,
|
| 138 |
+
imgsz=640, # Smaller size for faster inference on limited hardware
|
| 139 |
+
conf=conf_threshold,
|
| 140 |
+
iou=iou_threshold,
|
| 141 |
+
)
|
| 142 |
|
| 143 |
+
# Process results using FastSAMPrompt
|
| 144 |
+
from fastsam import FastSAMPrompt # Make sure it's imported
|
| 145 |
+
prompt_process = FastSAMPrompt(image_np_rgb, everything_results, device=DEVICE)
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
# Get all annotations (masks)
|
| 148 |
+
ann = prompt_process.everything_prompt()
|
| 149 |
|
| 150 |
+
print(f"FastSAM found {len(ann[0]['masks']) if ann and ann[0] else 0} masks.")
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# --- Plotting Masks on Image (Manual) ---
|
| 153 |
+
output_image = image_pil.copy()
|
| 154 |
+
if ann and ann[0] is not None and 'masks' in ann[0] and len(ann[0]['masks']) > 0:
|
| 155 |
+
masks = ann[0]['masks'].cpu().numpy() # shape (N, H, W)
|
| 156 |
+
|
| 157 |
+
# Create overlay image
|
| 158 |
+
overlay = Image.new('RGBA', output_image.size, (0, 0, 0, 0))
|
| 159 |
+
draw = ImageDraw.Draw(overlay)
|
| 160 |
|
| 161 |
+
for i in range(masks.shape[0]):
|
| 162 |
+
mask = masks[i] # shape (H, W), boolean
|
| 163 |
+
|
| 164 |
+
# Generate random color with some transparency
|
| 165 |
+
color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255), 128) # RGBA with alpha
|
| 166 |
|
| 167 |
+
# Create a single-channel image from the boolean mask
|
| 168 |
+
mask_image = Image.fromarray((mask * 255).astype(np.uint8), mode='L')
|
| 169 |
+
|
| 170 |
+
# Apply color to the mask area on the overlay
|
| 171 |
+
draw.bitmap((0,0), mask_image, fill=color)
|
| 172 |
|
| 173 |
+
# Composite the overlay onto the original image
|
| 174 |
+
output_image = Image.alpha_composite(output_image.convert('RGBA'), overlay).convert('RGB')
|
| 175 |
|
| 176 |
+
print("FastSAM processing and plotting complete.")
|
| 177 |
+
return output_image, image_pil # Return segmented and original images
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
except Exception as e:
|
| 180 |
+
print(f"Error during FastSAM processing: {e}")
|
| 181 |
+
import traceback
|
| 182 |
+
traceback.print_exc() # Print detailed traceback
|
| 183 |
+
return f"An error occurred: {e}", None
|
| 184 |
+
|
| 185 |
|
| 186 |
# --- Gradio Interface ---
|
| 187 |
|
| 188 |
+
# Pre-load models on startup (optional but good for performance)
|
| 189 |
+
print("Attempting to preload models...")
|
| 190 |
+
load_clip_model()
|
| 191 |
+
load_fastsam_model()
|
| 192 |
+
print("Preloading finished (or attempted).")
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 196 |
+
gr.Markdown("# CLIP & FastSAM Demo")
|
| 197 |
+
gr.Markdown("Explore Zero-Shot Classification with CLIP and 'Segment Anything' with FastSAM.")
|
| 198 |
+
|
| 199 |
+
with gr.Tabs():
|
| 200 |
+
# --- CLIP Tab ---
|
| 201 |
+
with gr.TabItem("CLIP Zero-Shot Classification"):
|
| 202 |
+
gr.Markdown("Upload an image and provide comma-separated candidate labels (e.g., 'cat, dog, car'). CLIP will predict the probability of the image matching each label.")
|
| 203 |
+
with gr.Row():
|
| 204 |
+
with gr.Column(scale=1):
|
| 205 |
+
clip_input_image = gr.Image(type="pil", label="Input Image")
|
| 206 |
+
clip_text_labels = gr.Textbox(label="Comma-Separated Labels", placeholder="e.g., astronaut, mountain, dog playing fetch")
|
| 207 |
+
clip_button = gr.Button("Run CLIP Classification", variant="primary")
|
| 208 |
+
with gr.Column(scale=1):
|
| 209 |
+
clip_output_label = gr.Label(label="Classification Probabilities")
|
| 210 |
+
clip_output_image_display = gr.Image(type="pil", label="Input Image Preview") # Show input for context
|
| 211 |
+
|
| 212 |
+
clip_button.click(
|
| 213 |
+
run_clip_zero_shot,
|
| 214 |
+
inputs=[clip_input_image, clip_text_labels],
|
| 215 |
+
outputs=[clip_output_label, clip_output_image_display]
|
| 216 |
+
)
|
| 217 |
+
gr.Examples(
|
| 218 |
+
examples=[
|
| 219 |
+
["examples/astronaut.jpg", "astronaut, moon, rover, mountain"],
|
| 220 |
+
["examples/dog_bike.jpg", "dog, bicycle, person, park, grass"],
|
| 221 |
+
],
|
| 222 |
+
inputs=[clip_input_image, clip_text_labels],
|
| 223 |
+
outputs=[clip_output_label, clip_output_image_display],
|
| 224 |
+
fn=run_clip_zero_shot,
|
| 225 |
+
cache_examples=False, # Re-run for live demo
|
| 226 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
# --- FastSAM Tab ---
|
| 229 |
+
with gr.TabItem("FastSAM Segmentation"):
|
| 230 |
+
gr.Markdown("Upload an image. FastSAM will attempt to segment all objects/regions in the image.")
|
| 231 |
+
with gr.Row():
|
| 232 |
+
with gr.Column(scale=1):
|
| 233 |
+
fastsam_input_image = gr.Image(type="pil", label="Input Image")
|
| 234 |
+
with gr.Row():
|
| 235 |
+
fastsam_conf = gr.Slider(minimum=0.1, maximum=1.0, value=0.4, step=0.05, label="Confidence Threshold")
|
| 236 |
+
fastsam_iou = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="IoU Threshold")
|
| 237 |
+
fastsam_button = gr.Button("Run FastSAM Segmentation", variant="primary")
|
| 238 |
+
with gr.Column(scale=1):
|
| 239 |
+
fastsam_output_image = gr.Image(type="pil", label="Segmented Image")
|
| 240 |
+
# fastsam_input_display = gr.Image(type="pil", label="Original Image") # Optional: show original side-by-side
|
| 241 |
+
|
| 242 |
+
fastsam_button.click(
|
| 243 |
+
run_fastsam_segmentation,
|
| 244 |
+
inputs=[fastsam_input_image, fastsam_conf, fastsam_iou],
|
| 245 |
+
outputs=[fastsam_output_image] # Removed the second output for simplicity, adjust if needed
|
| 246 |
+
)
|
| 247 |
+
gr.Examples(
|
| 248 |
+
examples=[
|
| 249 |
+
["examples/dogs.jpg", 0.4, 0.9],
|
| 250 |
+
["examples/fruits.jpg", 0.5, 0.8],
|
| 251 |
+
],
|
| 252 |
+
inputs=[fastsam_input_image, fastsam_conf, fastsam_iou],
|
| 253 |
+
outputs=[fastsam_output_image],
|
| 254 |
+
fn=run_fastsam_segmentation,
|
| 255 |
+
cache_examples=False, # Re-run for live demo
|
| 256 |
+
)
|
| 257 |
|
| 258 |
+
# Add example images (optional, but helpful)
|
| 259 |
+
# Create an 'examples' folder and add some jpg images like 'astronaut.jpg', 'dog_bike.jpg', 'dogs.jpg', 'fruits.jpg'
|
| 260 |
+
if not os.path.exists("examples"):
|
| 261 |
+
os.makedirs("examples")
|
| 262 |
+
print("Created 'examples' directory. Please add some images (e.g., astronaut.jpg, dog_bike.jpg) for the examples to work.")
|
| 263 |
+
# You might need to download some sample images here too if running on a fresh env
|
| 264 |
+
try:
|
| 265 |
+
print("Downloading example images...")
|
| 266 |
+
wget.download("https://huggingface.co/spaces/gradio/image-segmentation/resolve/main/images/lion.jpg", "examples/lion.jpg")
|
| 267 |
+
wget.download("https://raw.githubusercontent.com/openai/CLIP/main/CLIP.png", "examples/clip_logo.png")
|
| 268 |
+
wget.download("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-logo.png", "examples/gradio_logo.png")
|
| 269 |
+
# Manually add the examples used above if these don't match
|
| 270 |
+
print("Example images downloaded (or attempted). Please verify.")
|
| 271 |
+
except Exception as e:
|
| 272 |
+
print(f"Could not download example images: {e}")
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# Launch the Gradio app
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
demo.launch(debug=True) # Set debug=False for deployment
|