from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation import gradio as gr import torch import matplotlib.pyplot as plt import cv2 processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined") model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined") def process_image(image, prompts): inputs = processor(text=prompts, images=[image] * len(prompts), padding="max_length", return_tensors="pt") # predict with torch.no_grad(): outputs = model(**inputs) preds = outputs.logits.unsqueeze(1) filename = f"mask.png" plt.imsave(filename,torch.sigmoid(preds[1][0])) img2 = cv2.imread(filename) gray_image = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) (thresh, bw_image) = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY) # fix color format cv2.cvtColor(bw_image, cv2.COLOR_BGR2RGB) return Image.fromarray(bw_image) title = "Interactive demo: zero-shot image segmentation with CLIPSeg" description = "Demo for using CLIPSeg, a CLIP-based model for zero- and one-shot image segmentation. To use it, simply upload an image and add a text to mask (identify in the image), or use one of the examples below and click 'submit'. Results will show up in a few seconds." article = "
CLIPSeg: Image Segmentation Using Text and Image Prompts | HuggingFace docs
" examples = [["example_image.png", "a glass", "something to fill", "wood", "a jar"]] interface = gr.Interface(fn=process_image, inputs=[gr.Image(type="pil"), gr.Textbox(label="What do you want to identify (separated by comma)?")], outputs=gr.Image(type="pil"), title=title, description=description, article=article, examples=examples) interface.launch(debug=True)