-
Notifications
You must be signed in to change notification settings - Fork 89
/
run_app.py
executable file
·404 lines (295 loc) · 16.2 KB
/
run_app.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
import torch
import flask
import imageio
torch.manual_seed(0)
import json
import pickle
from flask import Blueprint, render_template
import os
import cv2
device_ids = [0]
from PIL import Image
import timeit
from utils.poisson_image_editing import poisson_edit
from utils.data_utils import *
from utils.model_utils import *
import numpy as np
import argparse
import copy
from io import BytesIO
from models.EditGAN.EditGAN_tool import Tool
np.random.seed(6)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
app = Flask(__name__)
CORS(app, support_credentials=True)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=8888)
args = parser.parse_args()
return args
@app.route('/')
def index():
global tool
tool = Tool()
return render_template('index.html')
@app.route('/api/edit_from_mask', methods=['POST'])
@cross_origin(supports_credentials=True)
def edit_from_mask():
data = request.get_json(force=True)
# load mask
base64im = data['imageBase64']
# extension = base64im.split('/')[1].split(';')[0]
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
im = Image.open(BytesIO(base64.b64decode(base64im.encode())))
seg = np.asarray(im)[:, :, :-1]
seg_mask = np.zeros((seg.shape[0], seg.shape[1]))
for i in range(int(len(car_32_palette) / 3)):
curr_palette = [car_32_palette[3 * i], car_32_palette[3 * i + 1], car_32_palette[3 * i + 2]]
id = np.all(seg == np.array(curr_palette), 2)
seg_mask[id == 1] = i
if seg_mask.shape[0] != seg_mask.shape[1]:
canvas = np.zeros([seg_mask.shape[1], seg_mask.shape[1]], dtype=np.uint8)
canvas[(seg_mask.shape[1] - seg_mask.shape[0]) // 2: (seg_mask.shape[1] + seg_mask.shape[0]) // 2, :] = seg_mask
seg_mask = canvas
# load roi
base64im = data['roi']
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
im = Image.open(BytesIO(base64.b64decode(base64im.encode())))
roi = (np.asarray(im)[:, :, 0]) == 0
if roi.shape[0] != roi.shape[1]:
canvas = np.zeros([roi.shape[1], roi.shape[1]], dtype=np.uint8)
canvas[(roi.shape[1] - roi.shape[0]) // 2: (roi.shape[1] + roi.shape[0]) // 2, :] = roi
roi = canvas
if data['image_id'][:8] == "results_":
curr_latent = np.load(os.path.join(tool.result_path, data['image_id'][8:] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "sample_":
curr_latent = np.load(os.path.join(tool.sampling_path, data['image_id'] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "upload_":
curr_latent = torch.from_numpy(
np.load(os.path.join(tool.upload_latent_path, data['image_id'][7:] + '_latent.npy'))).cuda().unsqueeze(0)
else:
curr_image_id = int(data['image_id'])
print("Current image id: ", curr_image_id)
curr_latent = torch.from_numpy(tool.testing_latent_list[curr_image_id]).cuda().unsqueeze(0)
org_latent = copy.deepcopy(curr_latent)
img_out, img_seg_final, optimized_latent = tool.run_optimization_editGAN(seg_mask, curr_latent, roi)
np.save(os.path.join(tool.result_path, data['image_id'] + '_latent.npy'), optimized_latent)
roi_colors = data['roi_id']
roi_color_color_ids = []
for color in roi_colors:
roi_color_color_ids.append(tool.car_platte.index(color))
dump_dict = {"edit_vector": optimized_latent - org_latent.detach().squeeze(0).cpu().numpy(),
"roi_ids": roi_color_color_ids}
with open(os.path.join(tool.result_path, 'current_editing_latent_cache.pickle'), 'wb') as handle:
pickle.dump(dump_dict, handle)
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.result_path, data['image_id'] + '_mask_org.npy'), img_seg_final.astype(np.uint8))
sv_name = os.path.join(tool.result_path, data['image_id'] + ".jpg")
imageio.imsave(sv_name, img_out[0, 64:448].astype(np.uint8))
return flask.make_response(json.dumps({"sv_name": sv_name.split("/")[-1].split(".")[0]}), 200)
@app.route('/api/apply_current_editing_vector', methods=['POST'])
@cross_origin(supports_credentials=True)
def apply_current_editing_vector():
data = request.get_json(force=True)
# load roi
base64im = data['roi']
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
im = Image.open(BytesIO(base64.b64decode(base64im.encode())))
roi = (np.asarray(im)[:, :, 0]) == 0
if roi.shape[0] != roi.shape[1]:
canvas = np.zeros([roi.shape[1], roi.shape[1]], dtype=np.uint8)
canvas[(roi.shape[1] - roi.shape[0]) // 2: (roi.shape[1] + roi.shape[0]) // 2, :] = roi
roi = canvas
with open(os.path.join(tool.result_path, 'current_editing_latent_cache.pickle'), 'rb') as handle:
dump_dict = pickle.load(handle)
if data['image_id'][:8] == "results_":
curr_latent = np.load(os.path.join(tool.result_path, data['image_id'][8:] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "sample_":
curr_latent = np.load(os.path.join(tool.sampling_path, data['image_id'] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "upload_":
curr_latent = torch.from_numpy(
np.load(os.path.join(tool.upload_latent_path, data['image_id'][7:] + '_latent.npy'))).cuda().unsqueeze(0)
else:
curr_image_id = int(data['image_id'].split('_')[-1])
print("Current image id: ", curr_image_id)
curr_latent = torch.from_numpy(tool.testing_latent_list[curr_image_id]).cuda().unsqueeze(0)
editing_vector = dump_dict['edit_vector']
editing_vector = torch.from_numpy(editing_vector).cuda()
scale = float(data['scale']) / 2.
finetune_steps = int(data['steps'])
img_out, img_seg_final, optimized_latent = tool.run_optimization_post_process(finetune_steps, curr_latent,
editing_vector, scale, "",
class_ids=dump_dict['roi_ids'])
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '.jpg'),
img_out[0, 64:448].astype(np.uint8))
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.result_path, data['image_id'] + '_mask_org.npy'), img_seg_final.astype(np.uint8))
np.save(os.path.join(tool.result_path, data['image_id'] + '_latent.npy'), optimized_latent)
return flask.make_response(json.dumps({"sv_name": str(data['image_id'])}), 200)
@app.route('/api/random_roll', methods=['POST'])
@cross_origin(supports_credentials=True)
def random_roll():
start_time = timeit.default_timer()
img_out, img_seg_final, latent = tool.run_sampling()
print("run_sampling time,", timeit.default_timer() - start_time)
random_im_id = np.random.randint(10000, size=1)[0]
imageio.imsave(os.path.join(tool.sampling_path, "sample_" + str(random_im_id) + '.jpg'), img_out)
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.sampling_path, "sample_" + str(random_im_id) + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.sampling_path, "sample_" + str(random_im_id) + '_latent.npy'), latent)
return flask.make_response(json.dumps({"sv_name": str(random_im_id)}), 200)
@app.route('/api/apply_editing_vector', methods=['POST'])
@cross_origin(supports_credentials=True)
def apply_editing_vector():
data = request.get_json(force=True)
editing_vector_name = data['editing_vector_id']
editing_vector = np.load(os.path.join(tool.editing_vector_path, editing_vector_name + '.npy'))
editing_vector = torch.from_numpy(editing_vector).cuda()
scale = float(data['scale']) / 2.
finetune_steps = int(data['steps'])
if data['image_id'][:8] == "results_":
curr_latent = np.load(os.path.join(tool.result_path, data['image_id'][8:] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "sample_":
curr_latent = np.load(os.path.join(tool.sampling_path, data['image_id'] + '_latent.npy'))
curr_latent = torch.from_numpy(curr_latent).cuda().unsqueeze(0)
elif data['image_id'][:7] == "upload_":
curr_latent = torch.from_numpy(
np.load(os.path.join(tool.upload_latent_path, data['image_id'][7:] + '_latent.npy'))).cuda().unsqueeze(0)
else:
curr_image_id = int(data['image_id'])
print("Current image id: ", curr_image_id)
curr_latent = torch.from_numpy(tool.testing_latent_list[curr_image_id]).cuda().unsqueeze(0)
img_out, img_seg_final, optimized_latent = tool.run_optimization_post_process(finetune_steps, curr_latent,
editing_vector, scale,
editing_vector_name)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '.jpg'),
img_out[0, 64:448].astype(np.uint8))
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.result_path, data['image_id'] + '_mask_org.npy'), img_seg_final.astype(np.uint8))
np.save(os.path.join(tool.result_path, data['image_id'] + '_latent.npy'), optimized_latent)
return flask.make_response(json.dumps({"sv_name": str(data['image_id'])}), 200)
@app.route('/upload_crop_image', methods=['POST'])
@cross_origin(supports_credentials=True)
def upload_crop():
data = request.get_json(force=True)
# load mask
base64im = data['imageBase64']
# extension = base64im.split('/')[1].split(';')[0]
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
img = Image.open(BytesIO(base64.b64decode(base64im.encode()))).convert('RGB')
img = img.resize((512, 384))
img = np.asarray(img)
canvas = np.zeros([512, 512, 3], dtype=np.uint8)
canvas[(512 - 384) // 2: (512 + 384) // 2, :, :] = img
canvas = Image.fromarray(canvas, 'RGB')
img_out, img_seg_final, optimized_latent, optimized_noise = tool.run_embedding(canvas)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '.jpg'),
img_out[0, 64:448].astype(np.uint8))
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.result_path, data['image_id'] + '_latent.npy'), optimized_latent)
np.save(os.path.join(tool.upload_latent_path, data['image_id'] + '_latent.npy'), optimized_latent)
return flask.make_response(json.dumps({"sv_name": str(data['image_id'])}), 200)
@app.route('/upload_image', methods=['POST'])
@cross_origin(supports_credentials=True)
def upload():
data = request.get_json(force=True)
# load mask
base64im = data['imageBase64']
# extension = base64im.split('/')[1].split(';')[0]
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
img = Image.open(BytesIO(base64.b64decode(base64im.encode()))).convert('RGB')
org_img = copy.deepcopy(img)
crop_img, bbox_valid = crop_from_bbox(np.asarray(img), data['crop_loc'])
data['bbox_final'] = bbox_valid
crop_img = Image.fromarray(crop_img)
crop_img = crop_img.resize((512, 384))
crop_img = np.asarray(crop_img)
canvas = np.zeros([512, 512, 3], dtype=np.uint8)
canvas[(512 - 384) // 2: (512 + 384) // 2, :, :] = crop_img
canvas = Image.fromarray(canvas, 'RGB')
img_out, img_seg_final, optimized_latent, optimized_noise = tool.run_embedding(canvas)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_wo_crop.jpg'),
np.asarray(org_img).astype(np.uint8))
with open(os.path.join(tool.result_path, data['image_id'] + '.json'), 'w') as f:
json.dump(data, f)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '.jpg'),
img_out[0, 64:448].astype(np.uint8))
seg_vis = colorize_mask(img_seg_final, car_32_palette)
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_mask.png'),
seg_vis)
np.save(os.path.join(tool.result_path, data['image_id'] + '_mask_org.npy'), img_seg_final.astype(np.uint8))
np.save(os.path.join(tool.result_path, data['image_id'] + '_latent.npy'), optimized_latent)
np.save(os.path.join(tool.upload_latent_path, data['image_id'] + '_latent.npy'), optimized_latent)
return flask.make_response(json.dumps({"sv_name": str(data['image_id'])}), 200)
@app.route('/download_image', methods=['POST'])
@cross_origin(supports_credentials=True)
def download_image():
data = request.get_json(force=True)
# load mask
base64im = data['imageBase64']
t = base64im.split('/')[0].split(':')[1]
assert t == 'image', 'Did not get image data!'
base64im = base64im.split(',')[1]
img = Image.open(BytesIO(base64.b64decode(base64im.encode()))).convert('RGB')
if data['image_id'][:8] == "results_":
mask_image_id = copy.deepcopy(data['image_id'])[8:]
image_id = data['image_id']
while image_id[:8] == "results_":
image_id = image_id[8:]
if image_id[:7] == "upload_":
image_id = image_id[7:]
with open(os.path.join(tool.result_path, image_id + '.json'), 'r') as f:
data = json.load(f)
bbox = data['bbox_final']
img_org = np.asarray(Image.open(os.path.join(tool.result_path, image_id + '_wo_crop.jpg'))).astype(np.uint8)
img_full = crop2fullImg(np.asarray(img), bbox, img_org * 0., im_size=img_org.shape)
img_mask = np.load(os.path.join(tool.result_path, mask_image_id + '_mask_org.npy'))
img_mask_final = crop2fullImg(img_mask, bbox, (img_org * 0)[:, :, 0], im_size=img_org.shape)
img_mask_final = cv2.dilate(np.float32(img_mask_final > 0), np.ones((3, 3), np.uint8), iterations=3).astype(
np.uint8)
img_final = poisson_edit(img_full, img_org, img_mask_final, [0, 0])
imageio.imsave(os.path.join(tool.result_path, data['image_id'] + '_final.jpg'),
img_final.astype(np.uint8))
return flask.make_response(json.dumps({"sv_name": str(data['image_id'])}), 200)
@app.route('/upload_vector', methods=['POST'])
@cross_origin(supports_credentials=True)
def upload_vector():
f = request.files['file']
f.save(os.path.join(tool.result_path, 'current_editing_latent_cache.pickle'))
return 'file uploaded successfully'
if __name__ == '__main__':
args = get_args()
app.run(host='0.0.0.0', threaded=True, port=args.port)