# -*- coding: utf-8 -*-
import sys
import io
import requests
import json
import base64
from PIL import Image
import numpy as np
import gradio as gr
def inference_mask1(prompt,
img,
img_):
files = {
"pimage" : resizeImg(prompt["image"]),
"pmask" : resizeImg(prompt["mask"]),
"img" : resizeImg(img),
"img_" : resizeImg(img_)
}
#r = requests.post("https://flagstudio.baai.ac.cn/painter/run", json = files)
r = requests.post("http://120.92.79.209/painter/run", json = files)
a = json.loads(r.text)
res = []
for i in range(len(a)):
#out = Image.open(io.BytesIO(base64.b64decode(a[i])))
#out = out.resize((224, 224))
#res.append(np.uint8(np.array(out)))
res.append(np.uint8(np.array(Image.open(io.BytesIO(base64.b64decode(a[i]))))))
return res
def resizeImg(img):
res, hres = 448, 448
img = Image.fromarray(img).convert("RGB")
img = img.resize((res, hres))
temp = io.BytesIO()
img.save(temp, format="WEBP")
return base64.b64encode(temp.getvalue()).decode('ascii')
def inference_mask_cat(
prompt,
img,
img_,
):
output_list = [img, img_]
return output_list
# define app features and run
examples = [
['./images/hmbb_1.jpg', './images/hmbb_2.jpg', './images/hmbb_3.jpg'],
['./images/rainbow_1.jpg', './images/rainbow_2.jpg', './images/rainbow_3.jpg'],
['./images/earth_1.jpg', './images/earth_2.jpg', './images/earth_3.jpg'],
['./images/obj_1.jpg', './images/obj_2.jpg', './images/obj_3.jpg'],
['./images/ydt_2.jpg', './images/ydt_1.jpg', './images/ydt_3.jpg'],
]
demo_mask = gr.Interface(fn=inference_mask1,
inputs=[gr.ImageMask(brush_radius=8, label="prompt (提示图)"), gr.Image(label="img1 (测试图1)"), gr.Image(label="img2 (测试图2)")],
#outputs=[gr.Image(shape=(448, 448), label="output1 (输出图1)"), gr.Image(shape=(448, 448), label="output2 (输出图2)")],
outputs=[gr.Image(label="output1 (输出图1)").style(height=256, width=256), gr.Image(label="output2 (输出图2)").style(height=256, width=256)],
#outputs=gr.Gallery(label="outputs (输出图)"),
examples=examples,
#title="SegGPT for Any Segmentation
(Painter Inside)",
description="
\
Choose an example below 🔥 🔥 🔥
\
Or, upload by yourself:
\
1. Upload images to be tested to 'img1' and/or 'img2'.
2. Upload a prompt image to 'prompt' and draw a mask.
\
\
💎 The more accurate you annotate, the more accurate the model predicts.
\
💎 Examples below were never trained and are randomly selected for testing in the wild.
\
💎 Current UI interface only unleashes a small part of the capabilities of SegGPT, i.e., 1-shot case. \