PSW99 commited on
Commit
244c5dd
·
1 Parent(s): 7ddee02
Files changed (8) hide show
  1. app.py +104 -0
  2. labels.txt +18 -0
  3. person-1.jpg +0 -0
  4. person-2.jpg +0 -0
  5. person-3.jpg +0 -0
  6. person-4.jpg +0 -0
  7. person-5.jpg +0 -0
  8. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
+ "mattmdjaga/segformer_b2_clothes"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "mattmdjaga/segformer_b2_clothes"
15
+ )
16
+
17
+
18
+ def ade_palette():
19
+ """ADE20K palette that maps each class to RGB values."""
20
+ return [
21
+ [204, 87, 92],
22
+ [112, 185, 212],
23
+ [45, 189, 106],
24
+ [234, 123, 67],
25
+ [78, 56, 123],
26
+ [210, 32, 89],
27
+ [90, 180, 56],
28
+ ]
29
+
30
+
31
+ labels_list = []
32
+
33
+ with open(r'labels.txt', 'r') as fp:
34
+ for line in fp:
35
+ labels_list.append(line[:-1])
36
+
37
+ colormap = np.asarray(ade_palette())
38
+
39
+
40
+ def label_to_color_image(label):
41
+ if label.ndim != 2:
42
+ raise ValueError("Expect 2-D input label")
43
+
44
+ if np.max(label) >= len(colormap):
45
+ raise ValueError("label value too large.")
46
+ return colormap[label]
47
+
48
+
49
+ def draw_plot(pred_img, seg):
50
+ fig = plt.figure(figsize=(20, 15))
51
+
52
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
53
+
54
+ plt.subplot(grid_spec[0])
55
+ plt.imshow(pred_img)
56
+ plt.axis('off')
57
+ LABEL_NAMES = np.asarray(labels_list)
58
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
59
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
60
+
61
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
62
+ ax = plt.subplot(grid_spec[1])
63
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
64
+ ax.yaxis.tick_right()
65
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
66
+ plt.xticks([], [])
67
+ ax.tick_params(width=0.0, labelsize=25)
68
+ return fig
69
+
70
+
71
+ def sepia(input_img):
72
+ input_img = Image.fromarray(input_img)
73
+
74
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
75
+ outputs = model(**inputs)
76
+ logits = outputs.logits
77
+
78
+ logits = tf.transpose(logits, [0, 2, 3, 1])
79
+ logits = tf.image.resize(
80
+ logits, input_img.size[::-1]
81
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
82
+ seg = tf.math.argmax(logits, axis=-1)[0]
83
+
84
+ color_seg = np.zeros(
85
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
86
+ ) # height, width, 3
87
+ for label, color in enumerate(colormap):
88
+ color_seg[seg.numpy() == label, :] = color
89
+
90
+ # Show image + mask
91
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
92
+ pred_img = pred_img.astype(np.uint8)
93
+
94
+ fig = draw_plot(pred_img, seg)
95
+ return fig
96
+
97
+
98
+ demo = gr.Interface(fn=sepia,
99
+ inputs=gr.Image(shape=(400, 600)),
100
+ outputs=['plot'],
101
+ examples=["person-1", "person-2", "person-3", "person-4", "person-5"],
102
+ allow_flagging='never')
103
+
104
+ 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