Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from turtle import title
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import torch
|
| 7 |
+
from torch import nn
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
from matplotlib import pyplot as plt
|
| 11 |
+
from segmentation_mask_overlay import overlay_masks
|
| 12 |
+
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation,AutoProcessor,AutoConfig
|
| 13 |
+
|
| 14 |
+
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 15 |
+
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 16 |
+
classes = list()
|
| 17 |
+
|
| 18 |
+
def create_rgb_mask(mask):
|
| 19 |
+
color = tuple(np.random.choice(range(0,256), size=3))
|
| 20 |
+
gray_3_channel = cv2.merge((mask, mask, mask))
|
| 21 |
+
gray_3_channel[mask==255] = color
|
| 22 |
+
return gray_3_channel.astype(np.uint8)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def detect_using_clip(image,prompts=[],threshould=0.4):
|
| 26 |
+
predicted_masks = list()
|
| 27 |
+
inputs = processor(
|
| 28 |
+
text=prompts,
|
| 29 |
+
images=[image] * len(prompts),
|
| 30 |
+
padding="max_length",
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
)
|
| 33 |
+
with torch.no_grad(): # Use 'torch.no_grad()' to disable gradient computation
|
| 34 |
+
outputs = model(**inputs)
|
| 35 |
+
#preds = outputs.logits.unsqueeze(1)
|
| 36 |
+
preds = nn.functional.interpolate(
|
| 37 |
+
outputs.logits.unsqueeze(1),
|
| 38 |
+
size=(image.shape[0], image.shape[1]),
|
| 39 |
+
mode="bilinear"
|
| 40 |
+
)
|
| 41 |
+
threshold = 0.1
|
| 42 |
+
|
| 43 |
+
flat_preds = torch.sigmoid(preds.squeeze()).reshape((preds.shape[0], -1))
|
| 44 |
+
|
| 45 |
+
# Initialize a dummy "unlabeled" mask with the threshold
|
| 46 |
+
flat_preds_with_treshold = torch.full((preds.shape[0] + 1, flat_preds.shape[-1]), threshold)
|
| 47 |
+
flat_preds_with_treshold[1:preds.shape[0]+1,:] = flat_preds
|
| 48 |
+
|
| 49 |
+
# Get the top mask index for each pixel
|
| 50 |
+
inds = torch.topk(flat_preds_with_treshold, 1, dim=0).indices.reshape((preds.shape[-2], preds.shape[-1]))
|
| 51 |
+
predicted_masks = []
|
| 52 |
+
|
| 53 |
+
for i in range(1, len(prompts)+1):
|
| 54 |
+
mask = np.where(inds==i,255,0)
|
| 55 |
+
predicted_masks.append(mask)
|
| 56 |
+
|
| 57 |
+
return predicted_masks
|
| 58 |
+
|
| 59 |
+
def visualize_images(image,predicted_images,brightness=15,contrast=1.8):
|
| 60 |
+
alpha = 0.7
|
| 61 |
+
image_resize = cv2.resize(image,(352,352))
|
| 62 |
+
resize_image_copy = image_resize.copy()
|
| 63 |
+
|
| 64 |
+
# for mask_image in predicted_images:
|
| 65 |
+
# resize_image_copy = cv2.addWeighted(resize_image_copy,alpha,mask_image,1-alpha,10)
|
| 66 |
+
|
| 67 |
+
return cv2.convertScaleAbs(resize_image_copy, alpha=contrast, beta=brightness)
|
| 68 |
+
|
| 69 |
+
def shot(alpha,beta,image,labels_text):
|
| 70 |
+
print(labels_text)
|
| 71 |
+
|
| 72 |
+
if "," in labels_text:
|
| 73 |
+
prompts = labels_text.split(',')
|
| 74 |
+
else:
|
| 75 |
+
prompts = [labels_text]
|
| 76 |
+
print(prompts)
|
| 77 |
+
|
| 78 |
+
prompts = list(map(lambda x: x.strip(),prompts))
|
| 79 |
+
|
| 80 |
+
mask_labels = [f"{prompt}_{i}" for i,prompt in enumerate(prompts)]
|
| 81 |
+
cmap = plt.cm.tab20(np.arange(len(mask_labels)))[..., :-1]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
predicted_masks = detect_using_clip(image,prompts=prompts)
|
| 85 |
+
bool_masks = [predicted_mask.astype('bool') for predicted_mask in predicted_masks]
|
| 86 |
+
category_image = overlay_masks(image,np.stack(bool_masks,-1),labels=mask_labels,colors=cmap,alpha=alpha,beta=beta)
|
| 87 |
+
|
| 88 |
+
return category_image
|
| 89 |
+
|
| 90 |
+
iface = gr.Interface(fn=shot,
|
| 91 |
+
inputs = [
|
| 92 |
+
gr.Slider(0.1, 1, value=0.3, step=0.1 , label="alpha", info="Choose between 0.1 to 1"),
|
| 93 |
+
gr.Slider(0.1, 1, value=0.7, step=0.1, label="beta", info="Choose between 0.1 to 1"),
|
| 94 |
+
"image",
|
| 95 |
+
"text"
|
| 96 |
+
],
|
| 97 |
+
outputs = "image",
|
| 98 |
+
description ="Add an Image and labels to be detected separated by commas(atleast 2)",
|
| 99 |
+
title = "Zero-shot Image Segmentation with Prompt",
|
| 100 |
+
examples=[
|
| 101 |
+
[0.4,0.7,"images/room.jpg","chair, plant , flower pot , white cabinet , paintings , decorative plates , books"],
|
| 102 |
+
[0.4,0.7,"images/seats.jpg","door,table,chairs"],
|
| 103 |
+
[0.3,0.8,"images/vegetables.jpg","carrot,white radish,brinjal,basket,potato"],
|
| 104 |
+
[0.4,0.7,"images/dashcam.jpeg","car,sky,road,grassland,trees"]
|
| 105 |
+
],
|
| 106 |
+
# allow_flagging=False,
|
| 107 |
+
# analytics_enabled=False,
|
| 108 |
+
)
|
| 109 |
+
iface.launch()
|