-
Notifications
You must be signed in to change notification settings - Fork 26
/
demo_web.py
64 lines (43 loc) · 1.64 KB
/
demo_web.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 sys
sys.path.append('./face_modules/')
from demo.lib import swap_faces
import cv2
import time
from flask import render_template, request, Flask
app = Flask(__name__, template_folder='demo/templates', static_url_path='', static_folder='demo/static')
def write_image(path, img):
img = cv2.convertScaleAbs(img, alpha=(255.0))
cv2.imwrite(path, img)
@app.route('/', methods=["GET"])
def index():
return render_template('index.html', )
@app.route('/', methods=["GET", "POST"])
def process():
id = str(int(time.time()))
if request.method == 'GET':
return render_template('index.html')
source = request.files['source']
source.save(f'./demo/static/source_{id}.jpg')
target = request.files['target']
target.save(f'./demo/static/target_{id}.jpg')
Xs_raw = cv2.imread(f'./demo/static/source_{id}.jpg')
Xt_raw = cv2.imread(f'./demo/static/target_{id}.jpg')
try:
s2t = swap_faces(Xs_raw, Xt_raw)
write_image(f'./demo/static/result_s2t_{id}.jpg', s2t)
Xs_raw = cv2.imread(f'./demo/static/target_{id}.jpg')
Xt_raw = cv2.imread(f'./demo/static/source_{id}.jpg')
t2s = swap_faces(Xs_raw, Xt_raw)
write_image(f'./demo/static/result_t2s_{id}.jpg', t2s)
except Exception as e:
error = 'Exception: ' + str(e)
return render_template('index.html', error=error)
data = {
"source": f"source_{id}.jpg",
"target": f"target_{id}.jpg",
"result_s2t": f"result_s2t_{id}.jpg",
"result_t2s": f"result_t2s_{id}.jpg"
}
return render_template('index.html', data=data)
if __name__ == '__main__':
app.run(debug=True)