-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_api.py
68 lines (52 loc) · 1.71 KB
/
flask_api.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
import base64
import json
from cv2 import cv2
import numpy as np
from flask import Flask, request, jsonify
from flask_cors import CORS
import backend
from backend import cv2tob64, b64tocv2
app = Flask(__name__)
CORS(app)
app.debug = True
SHAPE = (384, 384, 1)
class NPEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, np.integer):
return int(o)
if isinstance(o, np.floating):
return float(o)
if isinstance(o, np.ndarray):
return o.tolist()
return json.JSONEncoder.default(self, o)
@app.route("/")
def home():
return jsonify("running")
@app.route("/style_transfer", methods=["POST"])
def trans():
content_img = request.json["content"] # str
style_img = request.json["style"] # str
stylized = backend.make_trans(content_img, style_img)
output = cv2tob64(stylized)
return jsonify({"stylized": output})
@app.route("/get_outline", methods=["POST"])
def getline():
original_img = cv2.resize(b64tocv2(request.json["original"]), (384, 384))
outlines = backend.outline(original_img)
return jsonify({"outline": cv2tob64(outlines)})
@app.route("/gen_step", methods=["POST"])
def gen_step():
lns = request.json["outline"]
sort_method = request.json["sort_method"]
if sort_method in ("ss", "seg_sort"):
content = request.json["content"]
else:
content = None
steps = backend.gen_steps(
cv2.cvtColor(b64tocv2(lns), cv2.COLOR_BGR2GRAY),
sort_method=sort_method,
content_image=b64tocv2(content)
)
return json.dumps({"steps": steps}, cls=NPEncoder)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=25525, ssl_context=('server.crt', 'server.key'))