File size: 2,358 Bytes
032ff24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
import gradio as gr
from PIL import Image
import matplotlib.pyplot as plt
import torch
import cv2


import os
os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")
import paddlehub as hub
import gradio as gr
import torch
from PIL import Image, ImageOps
import numpy as np
import imageio
os.mkdir("data")
os.rename("best.ckpt", "models/best.ckpt")
os.mkdir("dataout")

# Load CLIPSeg model
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
clipseg_model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")

# Load LAMA model
lama_model = hub.Module(name='U2Net')

def process_image(image, prompt):
    # Generate mask with CLIPSeg
    inputs = processor(text=prompt, images=image, padding="max_length", return_tensors="pt")
    with torch.no_grad():
        outputs = clipseg_model(**inputs)
        preds = outputs.logits
    plt.imsave("mask.png", torch.sigmoid(preds))
    mask_image = Image.open("mask.png").convert("RGB")

    # Convert image to BGR format
    image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    # Convert mask to grayscale format
    mask_image = cv2.cvtColor(np.array(mask_image), cv2.COLOR_RGB2GRAY)

    # Perform inpainting with LAMA
    input_dict = {"image": image, "mask": mask_image}
    inpainted_image = lama_model.inference(data=input_dict)["data"][0]
    
    inpainted_image = cv2.cvtColor(inpainted_image, cv2.COLOR_BGR2RGB)
    inpainted_image = Image.fromarray(inpainted_image)

    return mask_image, inpainted_image

interface = gr.Interface(fn=process_image, 
                     inputs=[gr.Image(type="pil"), gr.Textbox(label="Please describe what you want to identify")],
                     outputs=[gr.Image(type="pil"), gr.Image(type="pil")],
                     title="Interactive demo: zero-shot image segmentation with CLIPSeg and inpainting with LAMA",
                     description="Demo for using CLIPSeg and LAMA to perform zero- and one-shot image segmentation and inpainting. 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.",
                     examples=[["example_image.png", "wood"]])

interface.launch(debug=True)