Spaces:
Sleeping
Sleeping
edwin200311
commited on
Commit
•
c0c857f
1
Parent(s):
a3301e2
thks
Browse files- app.py +118 -0
- labels.txt +18 -0
- person-1.jpg +0 -0
- person-2.jpg +0 -0
- person-3.jpg +0 -0
- person-4.jpg +0 -0
- person-5.jpg +0 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from matplotlib import gridspec
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import tensorflow as tf
|
8 |
+
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
13 |
+
"mattmdjaga/segformer_b2_clothes"
|
14 |
+
)
|
15 |
+
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
16 |
+
"mattmdjaga/segformer_b2_clothes"
|
17 |
+
)
|
18 |
+
|
19 |
+
|
20 |
+
def ade_palette():
|
21 |
+
"""ADE20K palette that maps each class to RGB values."""
|
22 |
+
return [
|
23 |
+
[255, 255, 255],
|
24 |
+
[255, 0, 0],
|
25 |
+
[82, 75, 69],
|
26 |
+
[17, 0, 0],
|
27 |
+
[88, 76, 86],
|
28 |
+
[30, 30, 32],
|
29 |
+
[26, 32, 44],
|
30 |
+
[251, 225, 210],
|
31 |
+
[32, 26, 74],
|
32 |
+
[43, 48, 42],
|
33 |
+
[65, 60, 66],
|
34 |
+
[222, 176, 153],
|
35 |
+
[192, 125, 106],
|
36 |
+
[158, 106, 95],
|
37 |
+
[147, 140, 121],
|
38 |
+
[204, 194, 182],
|
39 |
+
[163, 173, 182],
|
40 |
+
[67, 57, 91],
|
41 |
+
|
42 |
+
]
|
43 |
+
|
44 |
+
|
45 |
+
labels_list = []
|
46 |
+
|
47 |
+
with open(r'labels.txt', 'r') as fp:
|
48 |
+
for line in fp:
|
49 |
+
labels_list.append(line[:-1])
|
50 |
+
|
51 |
+
colormap = np.asarray(ade_palette())
|
52 |
+
|
53 |
+
|
54 |
+
def label_to_color_image(label):
|
55 |
+
if label.ndim != 2:
|
56 |
+
raise ValueError("Expect 2-D input label")
|
57 |
+
|
58 |
+
if np.max(label) >= len(colormap):
|
59 |
+
raise ValueError("label value too large.")
|
60 |
+
return colormap[label]
|
61 |
+
|
62 |
+
|
63 |
+
def draw_plot(pred_img, seg):
|
64 |
+
fig = plt.figure(figsize=(20, 15))
|
65 |
+
|
66 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
67 |
+
|
68 |
+
plt.subplot(grid_spec[0])
|
69 |
+
plt.imshow(pred_img)
|
70 |
+
plt.axis('off')
|
71 |
+
LABEL_NAMES = np.asarray(labels_list)
|
72 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
73 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
74 |
+
|
75 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
76 |
+
ax = plt.subplot(grid_spec[1])
|
77 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
78 |
+
ax.yaxis.tick_right()
|
79 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
80 |
+
plt.xticks([], [])
|
81 |
+
ax.tick_params(width=0.0, labelsize=25)
|
82 |
+
return fig
|
83 |
+
|
84 |
+
|
85 |
+
def sepia(input_img):
|
86 |
+
input_img = Image.fromarray(input_img)
|
87 |
+
|
88 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
89 |
+
outputs = model(**inputs)
|
90 |
+
logits = outputs.logits
|
91 |
+
|
92 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
93 |
+
logits = tf.image.resize(
|
94 |
+
logits, input_img.size[::-1]
|
95 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
96 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
97 |
+
|
98 |
+
color_seg = np.zeros(
|
99 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
100 |
+
) # height, width, 3
|
101 |
+
for label, color in enumerate(colormap):
|
102 |
+
color_seg[seg.numpy() == label, :] = color
|
103 |
+
|
104 |
+
# Show image + mask
|
105 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
106 |
+
pred_img = pred_img.astype(np.uint8)
|
107 |
+
|
108 |
+
fig = draw_plot(pred_img, seg)
|
109 |
+
return fig
|
110 |
+
|
111 |
+
|
112 |
+
demo = gr.Interface(fn=sepia,
|
113 |
+
inputs=gr.Image(shape=(400, 600)),
|
114 |
+
outputs=['plot'],
|
115 |
+
examples=["person-1.jpg,person-2.jpg,person-3.jpg,person-4.jpg,person-5.jpg"],
|
116 |
+
allow_flagging='never')
|
117 |
+
|
118 |
+
demo.launch()
|
labels.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Background
|
2 |
+
Hat
|
3 |
+
Hair
|
4 |
+
Sunglasses
|
5 |
+
Upper-clothes
|
6 |
+
Skirt
|
7 |
+
Pants
|
8 |
+
Dress
|
9 |
+
Belt
|
10 |
+
Left-shoe
|
11 |
+
Right-shoe
|
12 |
+
Face
|
13 |
+
Left-leg
|
14 |
+
Right-leg
|
15 |
+
Left-arm
|
16 |
+
Right-arm
|
17 |
+
Bag
|
18 |
+
Scarf
|
person-1.jpg
ADDED
![]() |
person-2.jpg
ADDED
![]() |
person-3.jpg
ADDED
![]() |
person-4.jpg
ADDED
![]() |
person-5.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
tensorflow
|
4 |
+
numpy
|
5 |
+
Image
|
6 |
+
matplotlib
|