Update app.py
Browse files
app.py
CHANGED
|
@@ -8,78 +8,102 @@ import threading
|
|
| 8 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 9 |
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 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 |
iface = gr.Interface(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
gr.
|
| 55 |
-
gr.Textbox(label="
|
| 56 |
-
gr.
|
| 57 |
-
gr.Slider(minimum=0, maximum=1,
|
| 58 |
-
],
|
| 59 |
-
outputs=[
|
| 60 |
-
gr.Image(type="pil", label="Output Image"),
|
| 61 |
-
gr.Image(type="pil", label="Output Mask"),
|
| 62 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Launch
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Define API interface
|
| 69 |
-
api_interface = gr.Interface(
|
| 70 |
-
fn=extract_image,
|
| 71 |
-
inputs=[
|
| 72 |
-
gr.Image(type="pil", label="Input Image"),
|
| 73 |
-
gr.Textbox(label="Positive Prompts (comma separated)"),
|
| 74 |
-
gr.Textbox(label="Negative Prompts (comma separated)"),
|
| 75 |
-
gr.Slider(minimum=0, maximum=1, default=0.4, label="Threshold"),
|
| 76 |
-
],
|
| 77 |
-
outputs=[
|
| 78 |
-
gr.Image(type="pil", label="Output Image"),
|
| 79 |
-
gr.Image(type="pil", label="Output Mask"),
|
| 80 |
-
],
|
| 81 |
-
live=True # Setting live to True enables the API endpoint
|
| 82 |
-
)
|
| 83 |
|
| 84 |
-
# Launch API
|
| 85 |
-
api_interface.launch(share=True) # share=True allows external access to the API
|
|
|
|
| 8 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 9 |
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 10 |
|
| 11 |
+
# Gradio UI
|
| 12 |
+
with gr.Blocks() as demo:
|
| 13 |
+
gr.Markdown("# CLIPSeg: Image Segmentation Using Text and Image Prompts")
|
| 14 |
+
|
| 15 |
+
# Add your article and description here
|
| 16 |
+
gr.Markdown("Your article goes here")
|
| 17 |
+
gr.Markdown("Your description goes here")
|
| 18 |
+
|
| 19 |
+
with gr.Row():
|
| 20 |
+
with gr.Column():
|
| 21 |
+
input_image = gr.Image(type="pil")
|
| 22 |
+
positive_prompts = gr.Textbox(
|
| 23 |
+
label="Please describe what you want to identify (comma separated)"
|
| 24 |
+
)
|
| 25 |
+
negative_prompts = gr.Textbox(
|
| 26 |
+
label="Please describe what you want to ignore (comma separated)"
|
| 27 |
+
)
|
| 28 |
+
input_slider_T = gr.Slider(
|
| 29 |
+
minimum=0, maximum=1, value=0.4, label="Threshold"
|
| 30 |
+
)
|
| 31 |
+
btn_process = gr.Button(label="Process")
|
| 32 |
+
|
| 33 |
+
with gr.Column():
|
| 34 |
+
output_image = gr.Image(label="Result")
|
| 35 |
+
output_mask = gr.Image(label="Mask")
|
| 36 |
+
|
| 37 |
+
def process_image(image, prompt):
|
| 38 |
+
inputs = processor(
|
| 39 |
+
text=prompt, images=image, padding="max_length", return_tensors="pt"
|
| 40 |
+
)
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = model(**inputs)
|
| 43 |
+
preds = outputs.logits
|
| 44 |
+
|
| 45 |
+
pred = torch.sigmoid(preds)
|
| 46 |
+
mat = pred.cpu().numpy()
|
| 47 |
+
mask = Image.fromarray(np.uint8(mat * 255), "L")
|
| 48 |
+
mask = mask.convert("RGB")
|
| 49 |
+
mask = mask.resize(image.size)
|
| 50 |
+
mask = np.array(mask)[:, :, 0]
|
| 51 |
+
|
| 52 |
+
mask_min = mask.min()
|
| 53 |
+
mask_max = mask.max()
|
| 54 |
+
mask = (mask - mask_min) / (mask_max - mask_min)
|
| 55 |
+
|
| 56 |
+
return mask
|
| 57 |
+
|
| 58 |
+
def get_masks(prompts, img, threshold):
|
| 59 |
+
prompts = prompts.split(",")
|
| 60 |
+
masks = []
|
| 61 |
+
for prompt in prompts:
|
| 62 |
+
mask = process_image(img, prompt)
|
| 63 |
+
mask = mask > threshold
|
| 64 |
+
masks.append(mask)
|
| 65 |
+
|
| 66 |
+
return masks
|
| 67 |
|
| 68 |
+
def extract_image(pos_prompts, neg_prompts, img, threshold):
|
| 69 |
+
positive_masks = get_masks(pos_prompts, img, 0.5)
|
| 70 |
+
negative_masks = get_masks(neg_prompts, img, 0.5)
|
| 71 |
+
|
| 72 |
+
pos_mask = np.any(np.stack(positive_masks), axis=0)
|
| 73 |
+
neg_mask = np.any(np.stack(negative_masks), axis=0)
|
| 74 |
+
final_mask = pos_mask & ~neg_mask
|
| 75 |
+
|
| 76 |
+
final_mask = Image.fromarray(final_mask.astype(np.uint8) * 255, "L")
|
| 77 |
+
output_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
| 78 |
+
output_image.paste(img, mask=final_mask)
|
| 79 |
+
|
| 80 |
+
return output_image, final_mask
|
| 81 |
+
|
| 82 |
+
btn_process.click(
|
| 83 |
+
extract_image,
|
| 84 |
+
inputs=[
|
| 85 |
+
positive_prompts,
|
| 86 |
+
negative_prompts,
|
| 87 |
+
input_image,
|
| 88 |
+
input_slider_T,
|
| 89 |
+
],
|
| 90 |
+
outputs=[output_image, output_mask],
|
| 91 |
+
)
|
| 92 |
iface = gr.Interface(
|
| 93 |
+
extract_image,
|
| 94 |
+
[
|
| 95 |
+
gr.Textbox(label="Positive prompts"),
|
| 96 |
+
gr.Textbox(label="Negative prompts"),
|
| 97 |
+
gr.Image(type="pil"),
|
| 98 |
+
gr.Slider(minimum=0, maximum=1, value=0.4, label="Threshold"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
],
|
| 100 |
+
[gr.Image(label="Result")], # Only return the final image
|
| 101 |
+
"textbox,textbox,image,slider", # Match the directory name (without mask)
|
| 102 |
+
"image",
|
| 103 |
+
title="CLIPSeg API",
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# Launch both UI and API
|
| 107 |
+
demo.launch()
|
| 108 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
|
|
|
|
|