Update app.py
Browse files
app.py
CHANGED
@@ -3,28 +3,16 @@ import gradio as gr
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import numpy as np
|
6 |
-
import threading
|
7 |
|
|
|
8 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
9 |
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
10 |
|
11 |
-
# Function to process image and generate mask
|
12 |
# Function to process image and generate mask
|
13 |
def process_image(image, prompt):
|
14 |
inputs = processor(
|
15 |
text=prompt, images=image, padding="max_length", return_tensors="pt"
|
16 |
)
|
17 |
-
|
18 |
-
# Extract image tensor and normalize it
|
19 |
-
image_tensor = inputs["pixel_values"].squeeze().permute(1, 2, 0).cpu().numpy()
|
20 |
-
image_tensor = (image_tensor * 255).astype(np.uint8)
|
21 |
-
image_tensor = Image.fromarray(image_tensor)
|
22 |
-
image_tensor = image_tensor.convert("RGB")
|
23 |
-
|
24 |
-
# Perform CLIPSeg processing
|
25 |
-
inputs = processor(
|
26 |
-
text=prompt, images=image_tensor, padding="max_length", return_tensors="pt"
|
27 |
-
)
|
28 |
with torch.no_grad():
|
29 |
outputs = model(**inputs)
|
30 |
preds = outputs.logits
|
@@ -42,6 +30,16 @@ def process_image(image, prompt):
|
|
42 |
|
43 |
return mask
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Function to extract image using positive and negative prompts
|
47 |
def extract_image(pos_prompts, neg_prompts, img, threshold):
|
@@ -58,6 +56,7 @@ def extract_image(pos_prompts, neg_prompts, img, threshold):
|
|
58 |
|
59 |
return output_image, final_mask
|
60 |
|
|
|
61 |
# Define Gradio interface
|
62 |
iface = gr.Interface(
|
63 |
fn=extract_image,
|
@@ -70,14 +69,18 @@ iface = gr.Interface(
|
|
70 |
label="Please describe what you want to ignore (comma separated)",
|
71 |
key="neg_prompts",
|
72 |
),
|
73 |
-
gr.Image(type="pil", label="Input Image"
|
74 |
-
gr.Slider(minimum=0, maximum=1, default=0.4, label="Threshold"
|
75 |
],
|
76 |
outputs=[
|
77 |
-
gr.Image(label="Result"
|
78 |
-
gr.Image(label="Mask"
|
79 |
],
|
80 |
)
|
81 |
|
82 |
# Launch Gradio API
|
83 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import numpy as np
|
|
|
6 |
|
7 |
+
# Load CLIPSeg processor and model
|
8 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
9 |
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
10 |
|
|
|
11 |
# Function to process image and generate mask
|
12 |
def process_image(image, prompt):
|
13 |
inputs = processor(
|
14 |
text=prompt, images=image, padding="max_length", return_tensors="pt"
|
15 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
with torch.no_grad():
|
17 |
outputs = model(**inputs)
|
18 |
preds = outputs.logits
|
|
|
30 |
|
31 |
return mask
|
32 |
|
33 |
+
# Function to get masks from positive or negative prompts
|
34 |
+
def get_masks(prompts, img, threshold):
|
35 |
+
prompts = prompts.split(",")
|
36 |
+
masks = []
|
37 |
+
for prompt in prompts:
|
38 |
+
mask = process_image(img, prompt)
|
39 |
+
mask = mask > threshold
|
40 |
+
masks.append(mask)
|
41 |
+
|
42 |
+
return masks
|
43 |
|
44 |
# Function to extract image using positive and negative prompts
|
45 |
def extract_image(pos_prompts, neg_prompts, img, threshold):
|
|
|
56 |
|
57 |
return output_image, final_mask
|
58 |
|
59 |
+
# Define Gradio interface
|
60 |
# Define Gradio interface
|
61 |
iface = gr.Interface(
|
62 |
fn=extract_image,
|
|
|
69 |
label="Please describe what you want to ignore (comma separated)",
|
70 |
key="neg_prompts",
|
71 |
),
|
72 |
+
gr.Image(type="pil", label="Input Image"),
|
73 |
+
gr.Slider(minimum=0, maximum=1, default=0.4, label="Threshold"),
|
74 |
],
|
75 |
outputs=[
|
76 |
+
gr.Image(label="Result"),
|
77 |
+
gr.Image(label="Mask"),
|
78 |
],
|
79 |
)
|
80 |
|
81 |
# Launch Gradio API
|
82 |
iface.launch()
|
83 |
+
|
84 |
+
|
85 |
+
# Launch Gradio API
|
86 |
+
iface.launch()
|