-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (99 loc) · 3.46 KB
/
main.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
from flask import Flask, jsonify, render_template, request
from core import app as model
import random
import os
import base64
import argparse
parser = argparse.ArgumentParser() # 定义一个参数设置器
# 固定参数
parser.add_argument("--HOST", type=str, default='127.0.0.1')
parser.add_argument("--PATH_RESULTS", type=str, default="./static/results/")
parser.add_argument("--PATH_DATA", type=str, default="./static/data/")
parser.add_argument("--PATH_MODEL", type=str, default="./core/model/")
parser.add_argument("--PATH_STYLE", type=str, default="./core/style_imgs/")
args = parser.parse_args() # 定义参数集合args
app = Flask(__name__)
@app.route("/api/getPicture", methods=['post'])
def getPicture():
output1 = model.trainPicture(request.json)
print(output1)
return jsonify(results=[output1])
def safe_base64_decode(s):
b = base64.b64encode(s.encode('utf-8')) # 因为python3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码
bstr_tmp = str(b, 'utf-8') # 把byte类型的数据转换为utf-8的数据
b_str = bstr_tmp.strip(r'=+') # 用正则把 = 去掉
return b_str
def del_file(path):
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if os.path.isdir(c_path):
del_file(c_path)
else:
os.remove(c_path)
@app.route("/filePath", methods=['post'])
def filePath():
data = request.json
imgurl = data[4]
dataPath = imgurl[22:]
imagedata = base64.b64decode(dataPath)
if (os.path.exists(args.PATH_DATA + 'picture.jpg')):
os.remove(args.PATH_DATA + 'picture.jpg')
file = open(args.PATH_DATA + 'picture.jpg', "wb")
file.write(imagedata)
file.close()
data[4] = args.PATH_DATA + 'picture.jpg'
print(data)
model.main(data, args.PATH_RESULTS, args.PATH_DATA, args.PATH_STYLE, args.PATH_MODEL)
imgPath = [1]
return jsonify(results=[imgPath])
@app.route("/photograph", methods=['post'])
def photograph():
data = request.json
imgurl = data[5]
dataPath = imgurl[22:]
imagedata = base64.b64decode(dataPath)
if (os.path.exists(args.PATH_DATA + 'touxiang.jpg')):
os.remove(args.PATH_DATA + 'touxiang.jpg')
file = open(args.PATH_DATA + 'touxiang.jpg', 'wb')
file.write(imagedata)
file.close()
data[4] = args.PATH_DATA + 'touxiang.jpg'
model.main(data, args.PATH_RESULTS, args.PATH_DATA, args.PATH_STYLE, args.PATH_MODEL)
imgPath = [1]
return jsonify(results=[imgPath])
@app.route("/")
def start():
return render_template("start.html")
@app.route("/result")
def result():
url = getPath()
resultPath = getResultPath()
return render_template("result.html", url=url, resultPath=resultPath)
@app.route("/index", methods=['post', 'get'])
def main():
return render_template("index.html")
def getPath():
path = args.PATH_DATA
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if ("result" in c_path):
url = "http://"+args.HOST+":5000" + c_path[1:]
print(url)
return url
def getResultPath():
path = args.PATH_RESULTS
ls = os.listdir(path)
url = []
for i in ls:
c_path = os.path.join(path, i)
if ("jpg" in c_path):
url.append("http://"+args.HOST+":5000" + c_path[1:])
return url
@app.route("/hello")
def hello():
return render_template("hello.html")
if __name__ == "__main__":
app.debug = False
app.run()