File size: 2,154 Bytes
398654a
 
 
 
 
 
 
 
 
 
 
 
4a57abf
 
398654a
01740ec
 
 
398654a
4a57abf
398654a
dfb75bd
01740ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfb75bd
 
398654a
4a57abf
9f2bb03
4a57abf
 
 
 
 
 
 
 
 
 
dfb75bd
01740ec
398654a
01740ec
398654a
 
 
 
 
 
 
 
 
86527b0
398654a
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os

import gradio as gr

import sys
sys.path.insert(0, 'U-2-Net')

from skimage import io, transform

import numpy as np
from PIL import Image

from utils.face_seg import FaceSeg
import cv2

import requests
import base64
from io import BytesIO

segment = FaceSeg()


def profuai(im_path, out_path):
    r = requests.post(
        'http://nebula.cs.ualberta.ca/predict',
        files={
            'file': open(im_path, 'rb'),
        },
        headers={'Host': 'nebula.cs.ualberta.ca', 'Origin': 'http://nebula.cs.ualberta.ca','Referer':'http://nebula.cs.ualberta.ca/',
                 'X-Requested-With':'XMLHttpRequest',
                 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0'}

    )
    #print(r)
    if (r.status_code == 200):
        data = r.text
        #data:image/png;base64,
        a = data[len("data:image/png;base64,"):]
        missing_padding = 4 - len(a) % 4
        if missing_padding:
            a += '=' * missing_padding

        img = Image.open(BytesIO(base64.urlsafe_b64decode(a)))
        # print(a)
        img.save(out_path, quality=80)
    else:
        raise Exception('error 1001')


def process(im):
    image = cv2.imread(im.name)
    matte = segment.get_mask(image)

    if len(image.shape) == 2:
        image = image[:, :, None]
    if image.shape[2] == 1:
        image = np.repeat(image, 3, axis=2)
    elif image.shape[2] == 4:
        image = image[:, :, 0:3]
    matte = np.repeat(np.asarray(matte)[:, :, None], 3, axis=2) / 255
    foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
    cv2.imwrite(im.name, foreground)

    profuai(im.name, im.name)

    return Image.open(im.name)
        
title = "U-2-Net"
description = "Gradio demo for U-2-Net, https://github.com/xuebinqin/U-2-Net"
article = ""

gr.Interface(
    process, 
    [gr.inputs.Image(type="file", label="Input")
], 
    [gr.outputs.Image(type="pil", label="Output")],
    title=title,
    description=description,
    article=article,
    examples=[],
    allow_flagging=False,
    allow_screenshot=False
    ).launch(enable_queue=True,cache_examples=True)