Nechba commited on
Commit
65cb410
1 Parent(s): 0b291c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -61
app.py CHANGED
@@ -1,61 +1,61 @@
1
- import gradio as gr
2
- import albumentations as albu
3
- from pylab import imshow
4
- import numpy as np
5
- import cv2
6
- import torch
7
- import albumentations as albu
8
- from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
9
- from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
10
- from collections import namedtuple
11
- from tempfile import NamedTemporaryFile
12
- import os
13
- from people_segmentation.pre_trained_models import create_model
14
- model = create_model("Unet_2020-07-20")
15
- model.eval()
16
- # Define model
17
- import matplotlib.pyplot as plt
18
- from pylab import imshow
19
-
20
-
21
- def segment_people(image):
22
- transform = albu.Compose([albu.Normalize(p=1)], p=1)
23
- padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
24
- x = transform(image=padded_image)["image"]
25
- x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
26
- with torch.no_grad():
27
- prediction = model(x)[0][0]
28
-
29
- mask = (prediction > 0).cpu().numpy().astype(np.uint8)
30
- mask = unpad(mask, pads)
31
- dst = cv2.addWeighted(image, 1, (cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) * (0, 255, 0)).astype(np.uint8), 0.5, 0)
32
-
33
- return dst
34
-
35
-
36
- # Create Gradio app
37
- def gradio_segmentation(image_path):
38
-
39
- image = load_rgb(image_path)
40
- mask = segment_people(image)
41
- return mask
42
-
43
- examples = [
44
- [ "76.jpg"],
45
- [ "69.jpg"],
46
- [ "80.jpg"]
47
- ]
48
-
49
- description = """
50
- # People Segmentation
51
- This application segments people from the input image. Upload an image to see the segmented output.
52
- """
53
-
54
- gr.Interface(
55
- fn=gradio_segmentation,
56
- inputs=gr.Image(type="filepath"),
57
- outputs=gr.Image(type="numpy"),
58
- examples=examples,
59
- title="People Segmentation",
60
- description=description,
61
- ).launch()
 
1
+ import gradio as gr
2
+ import albumentations as albu
3
+ from pylab import imshow
4
+ import numpy as np
5
+ import cv2
6
+ import torch
7
+ import albumentations as albu
8
+ from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
9
+ from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
10
+ from collections import namedtuple
11
+ from tempfile import NamedTemporaryFile
12
+ import os
13
+ from people_segmentation.pre_trained_models import create_model
14
+ model = create_model("Unet_2020-07-20")
15
+ model.eval()
16
+ # Define model
17
+ import matplotlib.pyplot as plt
18
+ from pylab import imshow
19
+
20
+
21
+ def segment_people(image):
22
+ transform = albu.Compose([albu.Normalize(p=1)], p=1)
23
+ padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
24
+ x = transform(image=padded_image)["image"]
25
+ x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
26
+ with torch.no_grad():
27
+ prediction = model(x)[0][0]
28
+
29
+ mask = (prediction > 0).cpu().numpy().astype(np.uint8)
30
+ mask = unpad(mask, pads)
31
+ dst = cv2.addWeighted(image, 1, (cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) * (0, 255, 0)).astype(np.uint8), 0.5, 0)
32
+
33
+ return dst
34
+
35
+
36
+ # Create Gradio app
37
+ def gradio_segmentation(image_path):
38
+
39
+ image = load_rgb(image_path)
40
+ mask = segment_people(image)
41
+ return mask
42
+
43
+ examples = [
44
+ [ "73.jpg"],
45
+ [ "69.jpg"],
46
+ [ "80.jpg"]
47
+ ]
48
+
49
+ description = """
50
+ # People Segmentation
51
+ This application segments people from the input image. Upload an image to see the segmented output.
52
+ """
53
+
54
+ gr.Interface(
55
+ fn=gradio_segmentation,
56
+ inputs=gr.Image(type="filepath"),
57
+ outputs=gr.Image(type="numpy"),
58
+ examples=examples,
59
+ title="People Segmentation",
60
+ description=description,
61
+ ).launch()