forked from lllyasviel/style2paints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
158 lines (128 loc) · 5.58 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
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
157
158
import os
import time
from tricks import *
from ai import *
from config import *
if productive:
from gevent import monkey
monkey.patch_all()
from bottle import route, run, static_file, request, BaseRequest
import base64
import re
import cv2
import datetime
BaseRequest.MEMFILE_MAX = 10000 * 1000
pather_text = open('game/fin.html').read()
@route('/fin/<path>')
def show_wiki_page(path):
return pather_text.replace('<path>', path.replace('$', '/'))
@route('/<filename:path>')
def send_static(filename):
return static_file(filename, root='game/')
@route('/')
def send_static():
return static_file("index.html", root='game/')
@route('/paint', method='POST')
def do_paint():
print('received')
sketchID = request.forms.get("sketchID")
if sketchID == 'new':
dstr = datetime.datetime.now().strftime('%b_%d_%H_%M_%S') + '__' + str(np.random.randint(100, 999))
sketchDataURL = request.forms.get("sketch")
sketchDataURL = re.sub('^data:image/.+;base64,', '', sketchDataURL)
sketchDataURL = base64.urlsafe_b64decode(sketchDataURL)
sketchDataURL = np.fromstring(sketchDataURL, dtype=np.uint8)
sketchDataURL = cv2.imdecode(sketchDataURL, -1)
cv2.imwrite('record/' + dstr + '.sketch.png', sketchDataURL)
else:
dstr = sketchID
sketchDataURL = cv2.imread('record/' + dstr + '.sketch.png', cv2.IMREAD_UNCHANGED)
referenceID = request.forms.get("referenceID")
if referenceID == 'new':
referenceDataURL = request.forms.get("reference")
referenceDataURL = re.sub('^data:image/.+;base64,', '', referenceDataURL)
referenceDataURL = base64.urlsafe_b64decode(referenceDataURL)
referenceDataURL = np.fromstring(referenceDataURL, dtype=np.uint8)
referenceDataURL = cv2.imdecode(referenceDataURL, -1)
referenceID = str(np.random.randint(100, 999))
cv2.imwrite('record/' + dstr + '_' + referenceID + '.reference.png', referenceDataURL)
else:
if referenceID == 'no':
referenceDataURL = None
else:
referenceDataURL = cv2.imread('record/' + dstr + '_' + referenceID + '.reference.png', cv2.IMREAD_UNCHANGED)
hintDataURL = request.forms.get("hint")
hintDataURL = re.sub('^data:image/.+;base64,', '', hintDataURL)
hintDataURL = base64.urlsafe_b64decode(hintDataURL)
hintDataURL = np.fromstring(hintDataURL, dtype=np.uint8)
hintDataURL = cv2.imdecode(hintDataURL, -1)
hstr = str(np.random.randint(100, 999))
cv2.imwrite('record/' + dstr + '_' + hstr + '.hint.png', hintDataURL)
sketchDenoise = request.forms.get("sketchDenoise")
resultDenoise = request.forms.get("resultDenoise")
algrithom = request.forms.get("algrithom")
method = request.forms.get("method")
sketch_config = sketchDenoise + '_' + algrithom + '_' + method
print('sketchID: ' + sketchID)
print('referenceID: ' + referenceID)
print('sketchDenoise: ' + sketchDenoise)
print('resultDenoise: ' + resultDenoise)
print('algrithom: ' + algrithom)
print('method: ' + method)
t = time.time()
sketch = from_png_to_jpg(sketchDataURL)
raw_shape = sketch.shape
local_hint = hintDataURL
if referenceDataURL is not None:
global_hint = k_resize(x=s_enhance(from_png_to_jpg(referenceDataURL), 2.0), k=14)
local_hint[:, :, 0:3] = s_enhance(local_hint[:, :, 0:3], 1.5)
else:
global_hint = None
norm_path = 'record/' + dstr + '_' + sketch_config + '.norm.jpg'
if os.path.exists(norm_path):
sketch = cv2.imread(norm_path, cv2.IMREAD_GRAYSCALE)
else:
if sketchDenoise == 'false':
if algrithom == 'stability':
sketch = cv_denoise(k_resize(sketch, 48))
else:
sketch = cv_denoise(k_resize(sketch, 64))
else:
if algrithom == 'stability':
sketch = m_resize(sketch, min(sketch.shape[0], sketch.shape[1], 512))
sketch = go_tail(sketch, noisy=True)
sketch = k_resize(sketch, 48)
else:
sketch = m_resize(sketch, min(sketch.shape[0], sketch.shape[1], 768))
sketch = go_tail(sketch, noisy=True)
sketch = k_resize(sketch, 64)
if method == 'transfer':
sketch = go_line(sketch)
else:
sketch = cv2.cvtColor(sketch, cv2.COLOR_RGB2GRAY)
cv2.imwrite(norm_path, sketch)
if global_hint is None:
tiny_sketch = sk_resize(sketch, 32)
global_hint = k_resize(go_tail(go_dull(tiny_sketch, n_resize(k8_down_hints(local_hint), tiny_sketch.shape)), False), 14)
cv2.imwrite('record/' + dstr + '.dull.jpg', global_hint)
print('process: ' + str(time.time() - t))
t = time.time()
if algrithom == 'stability':
local_hint = k_down_hints(local_hint)
local_hint = d_resize(local_hint, sketch.shape)
if method == 'render':
painting = go_neck(sketch, global_hint, local_hint)
else:
painting = go_head(sketch, global_hint, local_hint)
print('paint: ' + str(time.time() - t))
t = time.time()
fin = go_tail(painting, noisy=(resultDenoise == 'true'))
fin = s_resize(fin, raw_shape)
print('denoise: ' + str(time.time() - t))
cv2.imwrite('record/' + dstr + '.fin.jpg', fin)
cv2.imwrite('game/results/' + dstr + '.jpg', fin)
return dstr + '*' + referenceID
if productive:
run(host="0.0.0.0", port=80, server='gevent')
else:
run(host="0.0.0.0", port=8000)