-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
64 lines (47 loc) · 1.77 KB
/
server.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
import base64
import os
import re
import test
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
@app.route("/")
def view_index_template():
return render_template('index.html')
def save_base64_image(image_b64, folder, filename):
# Converts the Base64 to Binary image data
imgstr = re.search(r'data:image/png;base64,(.*)', image_b64).group(1)
# Save the converted binary data
output = open(os.path.join(folder, filename), 'wb')
decoded = base64.b64decode(imgstr)
output.write(decoded)
output.close()
@app.route('/gan', methods=['POST'])
def generate():
save_dir = 'coco_pretrained/test_latest/synthesized_image/'
output_dir = 'static/output/'
image_b64 = request.values['imageBase64']
filename = request.values['filename']
style_index = int(request.values['style_image'])
drawing_folder = 'datasets/coco_stuff/val_drawing'
save_base64_image(image_b64, drawing_folder, filename)
# Execute the command
print('\n', "*****" * 20, "\nStarted Executing TEST\n", "*****" * 20)
test.clear_images(exclude_file=filename, max_img_buffer=4)
test.pre_process_semantic_input(style_index=style_index)
test.generate_from_data()
print('\n', "*****" * 20, "\n Ended Executing TEST\n", "*****" * 20)
# Check if the output has obtained
msg = 'successful' if os.path.exists(output_dir + save_dir + filename) else 'un-successful'
# Create the response dictionary
resp_dic = {
'img': save_dir + filename,
'msg': msg
}
# Convert to JSON Object
resp = jsonify(resp_dic)
resp.headers['Access-Control-Allow-Origin'] = '*'
# Return the JSON response
print('Generation of Image was "', msg, '"')
return resp
if __name__ == '__main__':
app.run()