forked from nhatminh-it/Single-Human-Clothes-Parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
213 lines (176 loc) · 7.87 KB
/
server.py
File metadata and controls
213 lines (176 loc) · 7.87 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from flask import Flask,render_template
from flask_cors import CORS, cross_origin
from flask import request
from PIL import Image
from model import *
UPLOAD_FOLDER = 'INPUT_PATH'
RESULT_FOLDER = os.path.join('static', 'result')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULT_FOLDER'] = RESULT_FOLDER
# Apply Flask CORS
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# parsing_result_path = os.path.join('static/result', img_name[:-4] + '.png')
# #parsing_result_path = os.path.join(args.output_dir, 'result' + '.png')
# output_img = Image.fromarray(np.asarray(parsing_result, dtype=np.uint8))
# output_img.putpalette(palette)
# output_img.save(parsing_result_path)
# if args.logits:
# logits_result_path = os.path.join(args.output_dir, img_name[:-4] + '.npy')
# np.save(logits_result_path, logits_result)
def color_image(img, pred, file):
fig, axes = plt.subplots(1, 2)
ax0, ax1 = axes
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])
ax1.get_xaxis().set_ticks([])
ax1.get_yaxis().set_ticks([])
classes = np.array(('Background', # always index 0
'Hat', 'Hair', 'Glove', 'Sunglasses',
'UpperClothes', 'Dress', 'Coat', 'Socks',
'Pants', 'Jumpsuits', 'Scarf', 'Skirt',
'Face', 'Left-arm', 'Right-arm', 'Left-leg',
'Right-leg', 'Left-shoe', 'Right-shoe',))
colormap = [(0, 0, 0),
(1, 0.25, 0), (0, 0.25, 0), (0.5, 0, 0.25), (1, 1, 1),
(1, 0.75, 0), (0, 0, 0.5), (0.5, 0.25, 0), (0.75, 0, 0.25),
(1, 0, 0.25), (0, 0.5, 0), (0.5, 0.5, 0), (0.25, 0, 0.5),
(1, 0, 0.75), (0, 0.5, 0.5), (0, 0.5, 0.5), (1, 0, 0),
(1, 0, 0), (0, 0.75, 0), (0, 0.75, 0), ]
cmap = matplotlib.colors.ListedColormap(colormap)
bounds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
h, w, _ = pred.shape
def denormalize(img, mean, std):
c, _, _ = img.shape
for idx in range(c):
img[idx, :, :] = img[idx, :, :] * std[idx] + mean[idx]
return img
img = denormalize(img.cpu().numpy(), [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
img = img.transpose(1, 2, 0).reshape((h, w, 3))
pred = pred.reshape((h, w))
# show image
ax0.set_title('img')
ax0.imshow(img)
ax1.set_title('pred')
mappable = ax1.imshow(pred, cmap=cmap, norm=norm)
# colorbar legend
cbar = plt.colorbar(mappable, ax=axes, shrink=1, )
cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(classes):
cbar.ax.text(30, (20*j + 10) / 20.0, lab, ha='left', va='center', )
plt.savefig(fname=os.path.join(app.config['RESULT_FOLDER'], file.filename))
#plt.savefig(fname=os.path.join(app.config['RESULT_FOLDER'], 'result.jpg'))
#print('result saved to ./result.jpg')
#plt.show()
def color_image_new(img, pred, file, bounds):
fig, axes = plt.subplots(1, 2)
ax0, ax1 = axes
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])
ax1.get_xaxis().set_ticks([])
ax1.get_yaxis().set_ticks([])
classes = ['Background', # always index 0
'Hat', 'Hair', 'Glove', 'Sunglasses',
'UpperClothes', 'Dress', 'Coat', 'Socks',
'Pants', 'Jumpsuits', 'Scarf', 'Skirt',
'Face', 'Left-arm', 'Right-arm', 'Left-leg',
'Right-leg', 'Left-shoe', 'Right-shoe']
pred_cls = np.array(([classes[i] for i in np.unique(pred)]))
colormap = [(0, 0, 0),
(1, 0.25, 0), (0, 0.25, 0), (0.5, 0, 0.25), (1, 1, 1),
(1, 0.75, 0), (0, 0, 0.5), (0.5, 0.25, 0), (0.75, 0, 0.25),
(1, 0, 0.25), (0, 0.5, 0), (0.5, 0.5, 0), (0.25, 0, 0.5),
(1, 0, 0.75), (0, 0.5, 0.5), (0, 0.5, 0.5), (1, 0, 0),
(1, 0, 0), (0, 0.75, 0), (0, 0.75, 0), ]
#cmap = matplotlib.colors.ListedColormap(colormap)
pred_colormap = [colormap[i] for i in np.unique(pred)]
cmap = matplotlib.colors.ListedColormap(pred_colormap)
#bounds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
h, w, _ = pred.shape
def denormalize(img, mean, std):
c, _, _ = img.shape
for idx in range(c):
img[idx, :, :] = img[idx, :, :] * std[idx] + mean[idx]
return img
img = denormalize(img.cpu().numpy(), [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
img = img.transpose(1, 2, 0).reshape((h, w, 3))
pred = pred.reshape((h, w))
# show image
ax0.set_title('img')
ax0.imshow(img)
ax1.set_title('pred')
mappable = ax1.imshow(pred, cmap=cmap, norm=norm)
# colorbar legend
cbar = plt.colorbar(mappable, ax=axes, shrink=1, )
cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(pred_cls):
cbar.ax.text(30, (len(pred_cls)*j*1.8 +15 ) / len(pred_cls), lab, ha='left', va='center', )
plt.savefig(fname=os.path.join(app.config['RESULT_FOLDER'], file.filename))
def get_transform():
transform_image_list = [
transforms.Resize((256, 256), 3),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
return transforms.Compose(transform_image_list)
def get_gt_transform():
transform_gt_list = [
transforms.Resize((256, 256), 0),
transforms.Lambda(lambda img: np.asarray(img, dtype=np.uint8)),
]
return transforms.Compose(transform_gt_list)
def build_network(snapshot, models):
epoch = 0
net = models
net = nn.DataParallel(net)
if snapshot is not None:
_, epoch = os.path.basename(snapshot).split('_')
if not epoch == 'last':
epoch = int(epoch)
net.load_state_dict(torch.load(snapshot,map_location={'cuda:0': 'cpu'}))
#logging.info("Snapshot for epoch {} loaded from {}".format(epoch, snapshot))
net = net.cuda()
return net, epoch
model = PSPNet(sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024)
snapshot = 'model/PSPNet_last'
net, starting_epoch = build_network(snapshot, model)
net.eval()
@app.route('/')
@cross_origin(origin='*')
def index():
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'fileUpload' not in request.files:
return render_template('404.html')
file = request.files['fileUpload']
path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
if path == 'INPUT_PATH\\':
return render_template('404.html')
file.save(path) #save image to path
# ------------ load image ------------ #
data_transform = get_transform()
img = Image.open(path)
img = data_transform(img)
img = img.cuda()
# --------------- inference --------------- #
with torch.no_grad():
pred, _ = net(img.unsqueeze(dim=0))
pred = pred.squeeze(dim=0)
pred = pred.cpu().numpy().transpose(1, 2, 0)
pred = np.asarray(np.argmax(pred, axis=2), dtype=np.uint8).reshape((256, 256, 1))
bounds = np.unique(pred)
new_bounds=np.append(bounds,bounds[-1])
color_image(img, pred, file)
#cv2.imwrite(os.path.join(app.config['RESULT_FOLDER'], file.filename), pred)
#show_image(img, pred)
#return render_template('result.html', imagepathresult = os.path.join(app.config['RESULT_FOLDER'], file.filename))
return render_template('result.html', imageresult = file.filename)
return render_template('index.html')
# Start Backend
if __name__ == '__main__':
app.run()