File size: 1,704 Bytes
65cb410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import albumentations as albu
from pylab import imshow
import numpy as np
import cv2
import torch
import albumentations as albu
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
from collections import namedtuple
from tempfile import NamedTemporaryFile
import os
from people_segmentation.pre_trained_models import create_model
model = create_model("Unet_2020-07-20")
model.eval()
# Define model
import matplotlib.pyplot as plt
from pylab import imshow


def segment_people(image):
    transform = albu.Compose([albu.Normalize(p=1)], p=1)
    padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
    x = transform(image=padded_image)["image"]
    x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
    with torch.no_grad():
        prediction = model(x)[0][0]

    mask = (prediction > 0).cpu().numpy().astype(np.uint8)
    mask = unpad(mask, pads)
    dst = cv2.addWeighted(image, 1, (cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) * (0, 255, 0)).astype(np.uint8), 0.5, 0)
    
    return dst


# Create Gradio app
def gradio_segmentation(image_path):
   
    image = load_rgb(image_path) 
    mask = segment_people(image)
    return mask

examples = [
    [ "73.jpg"],
     [ "69.jpg"],
      [ "80.jpg"]
]

description = """
# People Segmentation
This application segments people from the input image. Upload an image to see the segmented output.
"""

gr.Interface(
    fn=gradio_segmentation,
    inputs=gr.Image(type="filepath"),
    outputs=gr.Image(type="numpy"),
    examples=examples,
    title="People Segmentation",
    description=description,
    ).launch()