File size: 2,663 Bytes
032ff24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08d114a
032ff24
 
 
 
 
 
 
8edd020
032ff24
 
 
 
 
 
 
 
 
 
 
 
c7a242e
032ff24
 
 
 
14dc1bc
 
08d114a
8edd020
73fe085
032ff24
08d114a
14dc1bc
 
032ff24
 
 
 
 
af80700
032ff24
7faaab8
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
60
61
62
63
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
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)
    imageio.imwrite("./data/data.png", image)
    # 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}
    # imageio.imwrite("./data/data_mask.png", input_dict["mask"])
    imageio.imwrite("./data/data_mask.png", mask_image)
    os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
    inpainted_image = "./dataout/data_mask.png"
    
    # inpainted_image = Image.new('RGB', (image.shape[1], image.shape[0]), (0, 0, 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="filepath")],
                     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.")

interface.launch(debug=True)