Spaces:
Sleeping
Sleeping
import os, sys | |
import random | |
import warnings | |
os.system("python -m pip install -e segment_anything") | |
os.system("python -m pip install -e GroundingDINO") | |
os.system("pip install --upgrade diffusers[torch]") | |
os.system("pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel") | |
os.system("wget http://cleancoastindia.com//SsagarSvc/Uploads/sample1.jpg") | |
os.system("wget http://cleancoastindia.com/SsagarSvc/Uploads/sample2.jpg") | |
os.system("wget http://cleancoastindia.com/SsagarSvc/Uploads/sample3.jpg") | |
os.system("wget http://cleancoastindia.com/SsagarSvc/Uploads/sample4.jpg") | |
os.system("wget http://cleancoastindia.com/SsagarSvc/Uploads/sample5.jpg") | |
os.system("wget http://cleancoastindia.com/SsagarSvc/Uploads/sample6.jpg") | |
os.system("wget http://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swint_ogc.pth") | |
os.system("wget http://huggingface.co/spaces/mrtlive/segment-anything-model/resolve/main/sam_vit_h_4b8939.pth") | |
sys.path.append(os.path.join(os.getcwd(), "GroundingDINO")) | |
sys.path.append(os.path.join(os.getcwd(), "segment_anything")) | |
sys.path.append(os.path.join(os.getcwd(), "Roboto-Regular")) | |
warnings.filterwarnings("ignore") | |
import gradio as gr | |
import argparse | |
import numpy as np | |
import torch | |
import torchvision | |
from PIL import Image, ImageDraw, ImageFont | |
# Grounding DINO | |
import GroundingDINO.groundingdino.datasets.transforms as T | |
from GroundingDINO.groundingdino.models import build_model | |
from GroundingDINO.groundingdino.util.slconfig import SLConfig | |
from GroundingDINO.groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap | |
# segment anything | |
from segment_anything import build_sam, SamPredictor | |
import numpy as np | |
# diffusers | |
import torch | |
from diffusers import StableDiffusionInpaintPipeline | |
# BLIP | |
from transformers import BlipProcessor, BlipForConditionalGeneration | |
def generate_caption(processor, blip_model, raw_image): | |
# unconditional image captioning | |
inputs = processor(raw_image, return_tensors="pt").to( | |
"cuda", torch.float16) | |
out = blip_model.generate(**inputs) | |
caption = processor.decode(out[0], skip_special_tokens=True) | |
return caption | |
def transform_image(image_pil): | |
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, _ = transform(image_pil, None) # 3, h, w | |
return image | |
def load_model(model_config_path, model_checkpoint_path, device): | |
args = SLConfig.fromfile(model_config_path) | |
args.device = device | |
model = build_model(args) | |
checkpoint = torch.load(model_checkpoint_path, map_location="cpu") | |
load_res = model.load_state_dict( | |
clean_state_dict(checkpoint["model"]), strict=False) | |
print(load_res) | |
_ = model.eval() | |
return model | |
def get_grounding_output(model, image, caption, box_threshold, text_threshold, with_logits=True): | |
# caption="all plastic.all metal.all paper.all glass.all cardboard.all wood.all rubber" | |
# caption="plastic.metal.paper.glass.cardboard.wood.rubber" | |
print(caption) | |
caption = caption.lower() | |
caption = caption.strip() | |
if not caption.endswith("."): | |
caption = caption + "." | |
with torch.no_grad(): | |
outputs = model(image[None], captions=[caption]) | |
logits = outputs["pred_logits"].cpu().sigmoid()[0] # (nq, 256) | |
boxes = outputs["pred_boxes"].cpu()[0] # (nq, 4) | |
logits.shape[0] | |
# filter output | |
logits_filt = logits.clone() | |
boxes_filt = boxes.clone() | |
filt_mask = logits_filt.max(dim=1)[0] > box_threshold | |
logits_filt = logits_filt[filt_mask] # num_filt, 256 | |
boxes_filt = boxes_filt[filt_mask] # num_filt, 4 | |
logits_filt.shape[0] | |
# get phrase | |
tokenlizer = model.tokenizer | |
tokenized = tokenlizer(caption) | |
# build pred | |
pred_phrases = [] | |
scores = [] | |
for logit, box in zip(logits_filt, boxes_filt): | |
pred_phrase = get_phrases_from_posmap( | |
logit > text_threshold, tokenized, tokenlizer) | |
if with_logits: | |
pred_phrases.append( | |
pred_phrase + f"({str(logit.max().item())[:4]})") | |
else: | |
pred_phrases.append(pred_phrase) | |
scores.append(logit.max().item()) | |
return boxes_filt, torch.Tensor(scores), pred_phrases | |
def draw_mask(mask, draw, random_color=False): | |
if random_color: | |
color = (random.randint(0, 255), random.randint( | |
0, 255), random.randint(0, 255), 153) | |
else: | |
color = (30, 144, 255, 153) | |
nonzero_coords = np.transpose(np.nonzero(mask)) | |
for coord in nonzero_coords: | |
draw.point(coord[::-1], fill=color) | |
def draw_box(box, draw, label): | |
# random color | |
color = tuple(np.random.randint(0, 255, size=3).tolist()) | |
draw.rectangle(((box[0], box[1]), (box[2], box[3])), | |
outline=color, width=2) | |
if label: | |
# font = ImageFont.load_default() | |
font = ImageFont.truetype("Roboto-Regular.ttf", 20) | |
if hasattr(font, "getbbox"): | |
bbox = draw.textbbox((box[0], box[1]), str(label), font) | |
else: | |
w, h = draw.textsize(str(label), font) | |
bbox = (box[0], box[1], w + box[0], box[1] + h) | |
draw.rectangle(bbox, fill=color) | |
# draw.text((box[0], box[1]), str(label), fill="white") | |
draw.text((box[0], box[1]), label) | |
config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py' | |
ckpt_repo_id = "ShilongLiu/GroundingDINO" | |
ckpt_filenmae = "groundingdino_swint_ogc.pth" | |
sam_checkpoint = 'sam_vit_h_4b8939.pth' | |
output_dir = "outputs" | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
blip_processor = None | |
blip_model = None | |
groundingdino_model = None | |
sam_predictor = None | |
inpaint_pipeline = None | |
def run_grounded_sam(input_image, text_prompt, box_threshold, text_threshold, iou_threshold): | |
global blip_processor, blip_model, groundingdino_model, sam_predictor, inpaint_pipeline | |
task_type = "seg" | |
box_threshold = 0.3 | |
text_threshold = 0.3 | |
iou_threshold = 0.75 | |
# make dir | |
os.makedirs(output_dir, exist_ok=True) | |
# load image | |
image_pil = input_image.convert("RGB") | |
transformed_image = transform_image(image_pil) | |
if groundingdino_model is None: | |
groundingdino_model = load_model( | |
config_file, ckpt_filenmae, device=device) | |
# run grounding dino model | |
boxes_filt, scores, pred_phrases = get_grounding_output( | |
groundingdino_model, transformed_image, text_prompt, box_threshold, text_threshold | |
) | |
size = image_pil.size | |
# process boxes | |
H, W = size[1], size[0] | |
for i in range(boxes_filt.size(0)): | |
boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H]) | |
boxes_filt[i][:2] -= boxes_filt[i][2:] / 2 | |
boxes_filt[i][2:] += boxes_filt[i][:2] | |
boxes_filt = boxes_filt.cpu() | |
# nms | |
print(f"Before NMS: {boxes_filt.shape[0]} boxes") | |
nms_idx = torchvision.ops.nms( | |
boxes_filt, scores, iou_threshold).numpy().tolist() | |
boxes_filt = boxes_filt[nms_idx] | |
pred_phrases = [pred_phrases[idx] for idx in nms_idx] | |
print(f"After NMS: {boxes_filt.shape[0]} boxes") | |
if task_type == 'seg' and boxes_filt.shape[0] > 0: | |
if sam_predictor is None: | |
# initialize SAM | |
assert sam_checkpoint, 'sam_checkpoint is not found!' | |
sam = build_sam(checkpoint=sam_checkpoint) | |
sam.to(device=device) | |
sam_predictor = SamPredictor(sam) | |
image = np.array(image_pil) | |
sam_predictor.set_image(image) | |
transformed_boxes = sam_predictor.transform.apply_boxes_torch( | |
boxes_filt, image.shape[:2]).to(device) | |
masks, _, _ = sam_predictor.predict_torch( | |
point_coords=None, | |
point_labels=None, | |
boxes=transformed_boxes, | |
multimask_output=False, | |
) | |
# masks: [1, 1, 512, 512] | |
if task_type == 'seg' and len(masks) > 0: | |
mask_image = Image.new('RGBA', size, color=(0, 0, 0, 0)) | |
mask_draw = ImageDraw.Draw(mask_image) | |
for mask in masks: | |
draw_mask(mask[0].cpu().numpy(), mask_draw, random_color=True) | |
image_draw = ImageDraw.Draw(image_pil) | |
label_string = "" | |
label_stringarr = [] | |
for box, label in zip(boxes_filt, pred_phrases): | |
# draw_box(box, image_draw, label) | |
index = label.find("(") | |
if index != -1: | |
new_label = label[:index].strip() | |
label_stringarr.append(new_label) | |
draw_box(box, image_draw, new_label) | |
else: | |
new_label = label | |
label_stringarr.append(new_label) | |
draw_box(box, image_draw, new_label) | |
# print(label_stringarr) | |
label_counts = [] | |
counts = {} | |
for label in label_stringarr: | |
if label in counts: | |
counts[label] += 1 | |
else: | |
counts[label] = 1 | |
for label, count in counts.items(): | |
label_counts.append(f"{label}: {count}") | |
label_counts_string = "\n".join(label_counts) | |
print(label_counts_string) | |
image_pil = image_pil.convert('RGBA') | |
image_pil.alpha_composite(mask_image) | |
return image_pil, label_counts_string | |
# return [image_pil, mask_image] | |
else: | |
raise gr.InvalidOutputException("Model output is empty") | |
print("number of masks".format(len(masks))) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser("Marine Litter", add_help=True) | |
parser.add_argument("--debug", action="store_true", | |
help="using debug mode") | |
parser.add_argument("--share", action="store_true", help="share the app") | |
parser.add_argument('--no-gradio-queue', action="store_true", | |
help='path to the SAM checkpoint') | |
args = parser.parse_args() | |
print(args) | |
block = gr.Blocks() | |
if not args.no_gradio_queue: | |
block = block.queue() | |
with block: | |
with gr.Row(): | |
with gr.Column(): | |
gr.Markdown("# Litter Segmentation and Detection") | |
input_image = gr.Image(type="pil", label="Upload Image", interactive=True) | |
# , value="demo1.jpg" | |
# task_type = gr.Dropdown( | |
text_prompt = gr.Dropdown(["plastic.metal.paper.glass.rubber.wood", | |
"bottle.cap.cup.rope.tire.cigarette.bag.slippers.can.card board box.paper.wrapper.food.coconut.dustbin.cover"], | |
value="plastic.metal.paper.glass.rubber.wood", label="select or enter prompt separate with dot . ",allow_custom_value=True) | |
# text_prompt = gr.Textbox(label="Text Prompt", | |
# value="plastic.metal.paper.glass.cardboard.wood.rubber") | |
run_button = gr.Button("Run") | |
gr.Markdown("# Image Examples") | |
#gr.Examples(["sample1.jpg", "sample2.jpg","sample3.jpg","sample4.jpg","sample5.jpg","sample6.jpg"], input_image,cache_examples=False) | |
gr.Examples(examples=["sample1.jpg", "sample2.jpg", "sample3.jpg", "sample4.jpg", "sample5.jpg", "sample6.jpg"],inputs=[input_image],cache_examples=False) | |
# with gr.Accordion("Advanced options", open=False): | |
with gr.Column(): | |
gr.Markdown("# Model Output") | |
gallery = gr.Image("Segmented Output") | |
gr.Markdown("# No of classes and count") | |
lbl = gr.Label("Classes Detected") | |
# gallery = gr.Gallery( | |
# label="Generated images", show_label=True, elem_id="gallery" | |
# ).style(preview=True, grid=2, object_fit="scale-down") | |
run_button.click(fn=run_grounded_sam, inputs=[input_image, text_prompt], | |
outputs=[gallery, lbl]) | |
block.launch(debug=args.debug, share=args.share, show_error=True) | |
#block.launch(debug=True, show_error=True) | |