Spaces:
Runtime error
Runtime error
File size: 8,553 Bytes
e00f1e9 e59b5af e00f1e9 176edaf e59b5af |
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
import gradio as gr
import os
home = os.getcwd()
home
!git clone https://github.com/IDEA-Research/GroundingDINO
%cd /{home}/GroundingDINO
!pip install -q -e .
text_prompt = 'basket'
image_path = '/kaggle/input/avataar/wall hanging.jpg'
output_image_path = '/kaggle/working'
'''Importing Libraries'''
import os
import groundingdino.datasets.transforms as T
import numpy as np
import torch
from groundingdino.models import build_model
from groundingdino.util import box_ops
from groundingdino.util.inference import predict
from groundingdino.util.slconfig import SLConfig
from groundingdino.util.utils import clean_state_dict
from huggingface_hub import hf_hub_download
from segment_anything import sam_model_registry
from segment_anything import SamPredictor
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from torchvision.utils import draw_bounding_boxes
from torchvision.utils import draw_segmentation_masks
def load_model_hf(repo_id, filename, ckpt_config_filename, device='cpu'):
'''
Loads model from hugging face, we use it to get grounding dino model checkpoints
'''
cache_config_file = hf_hub_download(repo_id=repo_id, filename=ckpt_config_filename)
args = SLConfig.fromfile(cache_config_file)
model = build_model(args)
args.device = device
cache_file = hf_hub_download(repo_id=repo_id, filename=filename)
checkpoint = torch.load(cache_file, map_location='cpu')
log = model.load_state_dict(clean_state_dict(checkpoint['model']), strict=False)
model.eval()
return model
def transform_image(image) -> torch.Tensor:
transform = T.Compose([
# T.RandomResize([800], max_size=1333),
T.ToTensor(),
# T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
image_transformed, _ = transform(image, None)
return image_transformed
class CFG:
'''
Defines variables used in our code
'''
sam_type = "vit_h"
SAM_MODELS = {
"vit_h": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth",
"vit_l": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth",
"vit_b": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
}
device = 'cuda'
ckpt_repo_id = "ShilongLiu/GroundingDINO"
ckpt_filename = "groundingdino_swinb_cogcoor.pth"
ckpt_config_filename = "GroundingDINO_SwinB.cfg.py"
# image_path = os.path.join(os.getcwd(), 'fruits.jpg')
# image_path = '/kaggle/input/avataar/wall hanging.jpg'
# text_prompt = 'chair'
'''Build models'''
def build_sam():
checkpoint_url = CFG.SAM_MODELS[CFG.sam_type]
sam = sam_model_registry[CFG.sam_type]()
state_dict = torch.hub.load_state_dict_from_url(checkpoint_url)
sam.load_state_dict(state_dict, strict=True)
sam.to(device = CFG.device)
sam = SamPredictor(sam)
return sam
def build_groundingdino():
ckpt_repo_id = CFG.ckpt_repo_id
ckpt_filename = CFG.ckpt_filename
ckpt_config_filename = CFG.ckpt_config_filename
groundingdino = load_model_hf(ckpt_repo_id, ckpt_filename, ckpt_config_filename)
return groundingdino
model_sam = build_sam()
model_groundingdino = build_groundingdino()
'''Predictions'''
def predict_dino(image_pil, text_prompt, box_threshold, text_threshold):
image_trans = transform_image(image_pil)
boxes, logits, phrases = predict(model = model_groundingdino,
image = image_trans,
caption = text_prompt,
box_threshold = box_threshold,
text_threshold = text_threshold,
device = CFG.device)
W, H = image_pil.size
boxes = box_ops.box_cxcywh_to_xyxy(boxes) * torch.Tensor([W, H, W, H]) # center cood to corner cood
return boxes, logits, phrases
def predict_sam(image_pil, boxes):
image_array = np.asarray(image_pil)
model_sam.set_image(image_array)
transformed_boxes = model_sam.transform.apply_boxes_torch(boxes, image_array.shape[:2])
masks, _, _ = model_sam.predict_torch(
point_coords=None,
point_labels=None,
boxes=transformed_boxes.to(model_sam.device),
multimask_output=False,
)
return masks.cpu()
def mask_predict(image_pil, text_prompt, box_threshold=0.3, text_threshold=0.25):
boxes, logits, phrases = predict_dino(image_pil, text_prompt, box_threshold, text_threshold)
masks = torch.tensor([])
if len(boxes) > 0:
masks = predict_sam(image_pil, boxes)
masks = masks.squeeze(1)
return masks, boxes, phrases, logits
'''Utils'''
def load_image(image_path):
return Image.open(image_path).convert("RGB")
def draw_image(image_pil, masks, boxes, alpha=0.4):
image = np.asarray(image_pil)
image = torch.from_numpy(image).permute(2, 0, 1)
if len(masks) > 0:
image = draw_segmentation_masks(image, masks=masks, colors=['red'] * len(masks), alpha=alpha)
return image.numpy().transpose(1, 2, 0)
image_pil = load_image(image_path)
masks, boxes, phrases, logits = mask_predict(image_pil, text_prompt=text_prompt, box_threshold=0.23, text_threshold=0.25)
output = draw_image(image_pil, masks, boxes, alpha=0.4)
# torch.save(masks, 'masks.pt')
'''Visualise segmented results'''
def visualize_results(img1, img2, task):
fig, axes = plt.subplots(1, 2, figsize=(20, 10))
axes[0].imshow(img1)
axes[0].set_title('Original Image')
axes[1].imshow(img2)
axes[1].set_title(f'{text_prompt} : {task}')
for ax in axes:
ax.axis('off')
visualize_results(image_pil, output, 'segmented')
x_units = 200
y_units = -100
# import torch
# import numpy as np
# masks = torch.load('/kaggle/input/chair-mask/masks.pt')
# print(masks.shape)
# masks
def main_fun():
'''Get masked object and background as two separate images'''
mask = np.expand_dims(masks[0], axis=-1)
masked_object = image_pil * mask
background = image_pil * ~mask
'''Shifts image by x_units and y_units'''
M = np.float32([[1, 0, x_units], [0, 1, y_units]])
shifted_image = cv2.warpAffine(masked_object, M, (masked_object.shape[1] , masked_object.shape[0] ), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
masked_shifted_image = np.where(shifted_image[:, :, 0] != 0, True, False)
'''Load stable diffuser model at checkpoint finetuned for inpainting task'''
from diffusers import StableDiffusionInpaintPipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained(
# "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
"stabilityai/stable-diffusion-2-inpainting",torch_dtype=torch.float16)
pipe.to(CFG.device)
# With Dilation
from scipy.ndimage import binary_dilation
structuring_element = np.ones((15, 15, 1), dtype=bool)
extrapolated_mask = binary_dilation(mask, structure=structuring_element)
mask_as_uint8 = extrapolated_mask.astype(np.uint8) * 255
pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L').resize((1024, 1024))
# pil_mask
# # Without Dilation
# pil_background = Image.fromarray(background)
# mask_as_uint8 = mask.astype(np.uint8) * 255
# pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L')
# # pil_mask
'''Do inpainting on masked locations of original image'''
prompt = 'a photo of background'
inpainted_image = pipe(prompt=prompt, image=image_pil, mask_image=pil_mask).images[0]
# inpainted_image
'''Get composite of shifted object and background inpainted imaage'''
pil_shifted_image = Image.fromarray(shifted_image).resize(inpainted_image.size)
np_shifted_image = np.array(pil_shifted_image)
masked_shifted_image = np.where(np_shifted_image[:, :, 0] != 0, True, False)
masked_shifted_image = np.expand_dims(masked_shifted_image, axis=-1)
inpainted_shifted = np.array(inpainted_image) * ~masked_shifted_image
shifted_image = cv2.resize(shifted_image, inpainted_image.size)
output = inpainted_shifted + shifted_image
output = Image.fromarray(output)
visualize_results(image_pil, output, 'shifted')
inputs_image = [
gr.components.Image(type="filepath", label="Input Image"),
]
outputs_image = [
gr.components.Image(type="numpy", label="Output Image"),
]
interface_image = gr.Interface(
fn=main_fun,
inputs=inputs_image,
outputs=outputs_image,
title="Pothole detector",
# examples=path,
cache_examples=False,
) |