Spaces:
Sleeping
Sleeping
File size: 8,234 Bytes
3b41a3f 10f4748 8d95fbe 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 50b3319 10f4748 50b3319 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 3b41a3f 10f4748 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
import gradio as gr
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
from fastsam import FastSAM, FastSAMPrompt
project_path = "."
sam_model = FastSAM(f"{project_path}/FastSAM-x.pt")
DEVICE = "cpu"
sample_images = [f"{project_path}/sample_images/{i}.jpg" for i in range(5)]
prediction_image = None
sam_prediction_image = None
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
def upload_file(files):
file_paths = [file.name for file in files]
return file_paths
def read_image(path):
img = Image.open(path)
return img
def set_prediction_image(evt: gr.SelectData, gallery):
global prediction_image
if isinstance(gallery[evt.index], dict):
prediction_image = gallery[evt.index]["name"]
else:
prediction_image = gallery[evt.index][0]["name"]
def predict(text):
text_classes = text.split(",")
text_classes = [sentence.strip() for sentence in text_classes]
image = read_image(prediction_image)
inputs = clip_processor(
text=text_classes,
images=image,
return_tensors="pt",
padding=True,
)
outputs = clip_model(**inputs)
logits_per_image = (
outputs.logits_per_image
) # this is the image-text similarity score
probs = logits_per_image.softmax(dim=1)[0]
results = {text_class: prob.item() for text_class, prob in zip(text_classes, probs)}
return {output: gr.update(value=results)}
def show_hide_sam_text(status):
if status == "Text Based":
return {sam_input_text: gr.update(visible=True)}
return {sam_input_text: gr.update(visible=False)}
def set_prediction_image_sam(evt: gr.SelectData, gallery):
global sam_prediction_image
if isinstance(gallery[evt.index], dict):
sam_prediction_image = gallery[evt.index]["name"]
else:
sam_prediction_image = gallery[evt.index][0]["name"]
def sam_predict(radio, text):
output_path = f"{project_path}/output/sam_results.jpg"
everything_results = sam_model(
sam_prediction_image,
device=DEVICE,
retina_masks=True,
imgsz=1024,
conf=0.4,
iou=0.9,
)
prompt_process = FastSAMPrompt(
sam_prediction_image, everything_results, device=DEVICE
)
ann = prompt_process.everything_prompt()
if radio == "Text Based":
ann = prompt_process.text_prompt(text=text)
prompt_process.plot(
annotations=ann,
output_path=output_path,
)
return {sam_output: gr.update(value=output_path)}
with gr.Blocks() as app:
gr.Markdown("## FastSAM & CLIP Inference with Gradio")
with gr.Tab("FastSAM"):
gr.Markdown("### Image Segmentation with FastSAM")
gr.Markdown(
"""Please an image or select one of the sample images.
Select either segment everything or text based segmentation.
Enter the text if you opt for segment based on text and hit Submit.
"""
)
with gr.Row():
with gr.Column():
with gr.Box():
with gr.Group():
upload_gallery = gr.Gallery(
value=None,
label="Uploaded images",
show_label=False,
elem_id="gallery_upload",
columns=5,
rows=2,
height="auto",
object_fit="contain",
)
upload_button = gr.UploadButton(
"Click to Upload images",
file_types=["image"],
file_count="multiple",
)
upload_button.upload(upload_file, upload_button, upload_gallery)
with gr.Group():
sample_gallery = gr.Gallery(
value=sample_images,
label="Sample images",
show_label=False,
elem_id="gallery_sample",
columns=3,
rows=2,
height="auto",
object_fit="contain",
)
upload_gallery.select(
set_prediction_image_sam, inputs=[upload_gallery]
)
sample_gallery.select(
set_prediction_image_sam, inputs=[sample_gallery]
)
with gr.Box():
radio = gr.Radio(
choices=["Segment Everything", "Text Based"],
value="Segment Everything",
type="value",
label="Select a Segmentation approach",
interactive=True,
)
sam_input_text = gr.TextArea(
label="Segementation Input",
placeholder="Please enter some text",
interactive=True,
visible=False,
)
radio.change(
show_hide_sam_text, inputs=[radio], outputs=[sam_input_text]
)
sam_submit_btn = gr.Button(value="Submit")
with gr.Column():
with gr.Box():
sam_output = gr.Image(value=None, label="Segmentation Results")
sam_submit_btn.click(
sam_predict, inputs=[radio, sam_input_text], outputs=[sam_output]
)
with gr.Tab("CLIP"):
gr.Markdown("### ERA Session19 - Zero Shot Classification with CLIP")
gr.Markdown(
"Please an image or select one of the sample images. Type some classification labels separated by comma. For ex: dog, cat"
)
with gr.Row():
with gr.Column():
with gr.Box():
with gr.Group():
upload_gallery = gr.Gallery(
value=None,
label="Uploaded images",
show_label=False,
elem_id="gallery_upload",
columns=5,
rows=2,
height="auto",
object_fit="contain",
)
upload_button = gr.UploadButton(
"Click to Upload images",
file_types=["image"],
file_count="multiple",
)
upload_button.upload(upload_file, upload_button, upload_gallery)
with gr.Group():
sample_gallery = gr.Gallery(
value=sample_images,
label="Sample images",
show_label=False,
elem_id="gallery_sample",
columns=3,
rows=2,
height="auto",
object_fit="contain",
)
upload_gallery.select(set_prediction_image, inputs=[upload_gallery])
sample_gallery.select(set_prediction_image, inputs=[sample_gallery])
with gr.Box():
input_text = gr.TextArea(
label="Classification Text",
placeholder="Please enter comma separated text",
interactive=True,
)
submit_btn = gr.Button(value="Submit")
with gr.Column():
with gr.Box():
output = gr.Label(value=None, label="Classification Results")
submit_btn.click(predict, inputs=[input_text], outputs=[output])
app.launch(debug=True, show_error=True)
|