File size: 2,210 Bytes
5a3dfd3
e6995ca
3144567
5a3dfd3
 
 
 
e2aae4e
5a3dfd3
b483613
e6995ca
b483613
5a3dfd3
1943daa
d37873a
 
 
0c217d4
 
 
7422b24
5a3dfd3
be469ef
03898c7
 
 
 
 
 
 
 
 
0c217d4
 
e6995ca
0c217d4
9ba6616
1d401cf
03898c7
a01ad06
8ce9b88
a01ad06
5a3dfd3
0900976
 
5a3dfd3
74dcc00
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
import os
os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")

import cv2
import paddlehub as hub
import gradio as gr
import torch
from PIL import Image, ImageOps
import numpy as np
os.mkdir("data")
os.rename("best.ckpt", "models/best.ckpt")
os.mkdir("dataout")
model = hub.Module(name='U2Net')
def infer(img,option):
  print(type(img))
  print(type(img["image"]))
  print(type(img["mask"]))
  img = Image.fromarray(img["image"])
  mask = Image.fromarray(img["mask"])
  img = ImageOps.contain(img, (700,700))
  width, height = img.size
  img.save("./data/data.png")
  if option == "automatic (U2net)":
      result = model.Segmentation(
          images=[cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)],
          paths=None,
          batch_size=1,
          input_size=320,
          output_dir='output',
          visualization=True)
      im = Image.fromarray(result[0]['mask'])
  else:
      mask = mask.resize((width,height))
  mask.save("./data/data_mask.png")
  os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
  return "./dataout/data_mask.png",mask
  
inputs = [gr.Image(source="upload",tool="sketch", label="Input",type="numpy"),gr.inputs.Radio(choices=["automatic (U2net)","manual"], type="value", default="manual", label="Masking option")]
outputs = [gr.outputs.Image(type="file",label="output"),gr.outputs.Image(type="pil",label="Mask")]
title = "LaMa Image Inpainting"
description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
examples = [
  ['person512.png',"automatic (U2net)"],
  ['person512.png',"manual"]
]
gr.Interface(infer, inputs, outputs, title=title, description=description, article=article, examples=examples,cache_examples=False).launch()