-
Notifications
You must be signed in to change notification settings - Fork 0
/
style_server.py
156 lines (136 loc) · 4.31 KB
/
style_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
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
from flask import Flask, request
import style
import requests
import base64
import io
from PIL import Image
from types import SimpleNamespace
import time
import os
from torchelie.recipes.neural_style import NeuralStyle
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '.'
def img_to_data_url(img):
with io.BytesIO() as f:
img = Image.open(img)
img.save(f, 'jpeg')
f.seek(0)
return 'data:image/jpg;base64,' + base64.b64encode(f.read()).decode()
def fname(f):
return os.path.basename(f).split('.')[0]
stylizer = NeuralStyle(device='cuda', visdom_env='style')
@app.route('/go', methods=['GET', 'POST'])
def go():
rargs = request.form
args = SimpleNamespace()
print(request.form)
print(request.files)
args.size = int(rargs['size'])
args.scale = float(rargs['scale'])
args.ratio = float(rargs['ratio'])
args.content_layers = [rargs['content_layer']]
args.preserve_colors = rargs.get('preserve_colors', 'off') == 'on'
args.init_with_content = rargs.get('init_with_content', 'off') == 'on'
args.coarse = float(rargs.get('coarse', '0'))
print('SAVING')
fn = 'content/' + str(int(time.time() * 100)) + '.jpg'
request.files['content'].save(fn)
args.content = fn
fn = 'style/' + str(int(time.time() * 100)) + '.jpg'
request.files['style'].save(fn)
args.style = fn
args.out = 'result/' + fname(args.content) + '_' + fname(
args.style) + '.png'
print('BEFORE')
try:
style.go(args, stylizer)
except KeyboardInterrupt:
print('interrupted')
return '<img src="' + img_to_data_url(args.out) + '" />'
@app.route('/', methods=['GET'])
def index():
return """
<!doctype html>
<html>
<head>
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
</head>
<body>
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
<h1>Neural Style</h1>
<form id="upload" action="/go" method="POST" enctype="multipart/form-data">
<input
type="file"
name="content"
placeholder="content URL"
required/>
<br/>
<input
type="file"
name="style"
placeholder="style URL"
required/>
<br/>
style ratio (bigger means more style, in [0, 1]):<br/>
<input
type="text"
name="ratio"
placeholder="Loss ratio"
value="0.97"/>
<br/>
<input
type="number"
step="any"
name="scale"
placeholder="Style Scale"
value="1"/>
<br/>
Coarse structures importance
<input
type="number"
step="any"
name="coarse"
placeholder="coarse structure importance"
value="1"/>
<br/>
Content shape fidelity:
<select name="content_layer">
<option value="conv3_2">high</option>
<option value="conv4_2">medium</option>
<option value="conv5_2">low</option>
</select>
Result size:
<select name="size">
<option value="128">128px</option>
<option value="256">256px</option>
<option value="512">512px</option>
<option value="720">720px</option>
<option value="1024">1024px</option>
<option value="1366">1366px</option>
<option value="2048">2048px</option>
<option value="-1">input size</option>
</select>
<br/>
Preserve Colors <input type="checkbox" name="preserve_colors" />
<br/>
Init with content<input type="checkbox" name="init_with_content" />
<input type="submit"/>
</form>
<script>
FilePond.parse(document.body);
</script>
</body>
<script>
window.upload.onsubmit = (e) => {
};
</script>
</html>
"""
if __name__ == '__main__':
try:
os.mkdir('content')
os.mkdir('style')
os.mkdir('result')
except:
pass
app.run(host='0.0.0.0', port=8000)