File size: 2,940 Bytes
b477a9c
c745af5
 
 
 
18ab632
cf3afd7
d552cb0
cf3afd7
375323c
a0d7137
c745af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf3afd7
 
b29d8af
 
c745af5
 
375323c
 
c745af5
1fd9f37
 
375323c
 
 
cf3afd7
c745af5
 
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
import gradio as gr
import cv2
import torch
import numpy as np
from torchvision import transforms

title = "REMOVEDOR DE FUNDO DE IMAGEM"
description = "Remova automaticamente o fundo das suas imagens com essa poderosa ferramenta de edição. Apenas envie suas imagens que deseja remover o fundo, então nossa IA irá removê-las"
article = "<p style='text-align: center'><a href='https://editoraitacaiunas.com.br/'>Editora Itacaiúnas</a> | <a href='https://editoraitacaiunas.com.br/itacaiunas-ia/'>Itacaiúnas IA</a> | <a href='https://github.com/eugenesiow/practical-ml'>Github Repo</a></p>"


def make_transparent_foreground(pic, mask):
    # split the image into channels
    b, g, r = cv2.split(np.array(pic).astype('uint8'))
    # add an alpha channel with and fill all with transparent pixels (max 255)
    a = np.ones(mask.shape, dtype='uint8') * 255
    # merge the alpha channel back
    alpha_im = cv2.merge([b, g, r, a], 4)
    # create a transparent background
    bg = np.zeros(alpha_im.shape)
    # setup the new mask
    new_mask = np.stack([mask, mask, mask, mask], axis=2)
    # copy only the foreground color pixels from the original image where mask is set
    foreground = np.where(new_mask, alpha_im, bg).astype(np.uint8)

    return foreground


def remove_background(input_image):
    preprocess = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])

    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(0)  # create a mini-batch as expected by the model

    # move the input and model to GPU for speed if available
    if torch.cuda.is_available():
        input_batch = input_batch.to('cuda')
        model.to('cuda')

    with torch.no_grad():
        output = model(input_batch)['out'][0]
    output_predictions = output.argmax(0)

    # create a binary (black and white) mask of the profile foreground
    mask = output_predictions.byte().cpu().numpy()
    background = np.zeros(mask.shape)
    bin_mask = np.where(mask, 255, background).astype(np.uint8)

    foreground = make_transparent_foreground(input_image, bin_mask)

    return foreground, bin_mask


def inference(img):
    foreground, _ = remove_background(img)
    return foreground


torch.hub.download_url_to_file('https://itacaiunas.com.br/api/exemplo1.jpg',
                               'exemplo1.jpg')
torch.hub.download_url_to_file('https://itacaiunas.com.br/api/exemplo2.jpg',
                               'exemplo2.jpg')
model = torch.hub.load('pytorch/vision:v0.6.0', 'deeplabv3_resnet101', pretrained=True)
model.eval()

gr.Interface(
    inference,
    gr.inputs.Image(type="pil", label="Entrada"),
    gr.outputs.Image(type="pil", label="Saída"),
    title=title,
    description=description,
    article=article,
    examples=[['exemplo1.jpg'], ['exemplo2.jpg']],
    enable_queue=True
).launch(debug=False)