File size: 10,033 Bytes
f0f40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7937b8
 
f0f40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85cce87
 
 
f0f40d2
 
85cce87
f0f40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1834769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0f40d2
 
 
 
 
 
 
 
 
 
 
 
 
1834769
f0f40d2
 
 
1834769
 
f0f40d2
 
 
 
1834769
f0f40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1834769
 
f0f40d2
 
 
 
1834769
f0f40d2
 
1834769
f0f40d2
 
 
 
 
1834769
 
f0f40d2
 
 
 
1834769
f0f40d2
 
1834769
f0f40d2
 
 
 
 
85cce87
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import datetime
import os
from pathlib import Path
import sys
from flask import Flask, jsonify, request, send_file, abort
from flask_uploads import UploadSet, configure_uploads, IMAGES
from werkzeug.exceptions import default_exceptions
from werkzeug.exceptions import HTTPException, NotFound 
import json
import torch
import time
import threading
import traceback
from PIL import Image
import numpy as np

PACKAGE_PARENT = '..'
WISE_DIR = '../wise/'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, WISE_DIR)))



from parameter_optimization.parametric_styletransfer import single_optimize
from parameter_optimization.parametric_styletransfer import CONFIG as ST_CONFIG
from parameter_optimization.strotss_org import strotss, pil_resize_long_edge_to
from helpers import torch_to_np, np_to_torch
from effects import get_default_settings, MinimalPipelineEffect 

app = Flask(__name__)

image_folder = 'img_received'
photos = UploadSet('photos', IMAGES)
app.config['UPLOADED_PHOTOS_DEST'] = image_folder
configure_uploads(app, photos)

class Args(object):
    def __init__(self, initial_data):
        for key in initial_data:
            setattr(self, key, initial_data[key])
    def set_attributes(self, val_dict):
        for key in val_dict:
            setattr(self, key, val_dict[key])

default_args = {
        "output_image" : "output.jpg",
        ## values always set by request ##
        "content_image": "",
        "style_image": "",
        "output_vp": "",
        "iters": 500
}


total_task_count = 0

class NeuralOptimizer():
    def __init__(self, args) -> None:
        self.cur_iteration = 0
        self.args = args

    def optimize(self):
        base_dir = f"result/{datetime.datetime.now().strftime(r'%Y-%m-%d %H.%Mh %Ss')}"
        os.makedirs(base_dir)

        content = Image.open(self.args.content_image)
        style = Image.open(self.args.style_image)

        def set_iter(iter):
            self.cur_iteration=iter

        effect, preset, _ = get_default_settings("minimal_pipeline")
        effect.enable_checkpoints()

        reference = strotss(pil_resize_long_edge_to(content, 1024),
                            pil_resize_long_edge_to(style, 1024), content_weight=16.0,
                            device=torch.device("cuda"), space="uniform")

        ref_save_path = os.path.join(base_dir, "reference.jpg")
        resize_to = 720
        reference = pil_resize_long_edge_to(reference, resize_to)
        reference.save(ref_save_path)
        ST_CONFIG["n_iterations"] = self.args.iters
        vp, content_img_cuda = single_optimize(effect, preset, "l1", self.args.content_image, str(ref_save_path),
                                            write_video=False, base_dir=base_dir,
                                            iter_callback=set_iter)

        output = Image.fromarray(torch_to_np(content_img_cuda.detach().cpu() * 255.0).astype(np.uint8))
        output.save(self.args.output_image)
        # torch.save (vp.detach().clone(), self.args.output_vp)
        # preset_tensor = effect.vpd.preset_tensor(preset, np_to_torch(np.array(content)).cuda(), add_local_dims=True)
        np.savez_compressed(self.args.output_vp, vp=vp.detach().cpu().numpy())

        

class StyleTask:
    def __init__(self, task_id, style_filename, content_filename):
        self.content_filename=content_filename
        self.style_filename=style_filename

        self.status = "queued"
        self.task_id = task_id
        self.error_msg = ""
        self.output_filename = content_filename.split(".")[0] + "_output.jpg"
        self.vp_output_filename = content_filename.split(".")[0] + "_output.npz"

        # global neural_optimizer
        # if neural_optimizer is None:
        #     neural_optimizer = NeuralOptimizer(Args(default_args))

        self.neural_optimizer = NeuralOptimizer(Args(default_args))
    
    def start(self):
        self.neural_optimizer.args.set_attributes(default_args)

        self.neural_optimizer.args.style_image = os.path.join(image_folder, self.style_filename)
        self.neural_optimizer.args.content_image = os.path.join(image_folder, self.content_filename)
        self.neural_optimizer.args.output_image = os.path.join(image_folder, self.output_filename)
        self.neural_optimizer.args.output_vp = os.path.join(image_folder, self.vp_output_filename)
 
        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                            # Daemonize thread
        thread.start()                                  # Start the execution

    def run(self):
        self.status = "running"
        try:
            self.neural_optimizer.optimize()
        except Exception as e:
            print("Error in task %d :"%(self.task_id), str(e))
            traceback.print_exc()

            self.status = "error"
            self.error_msg = str(e)
            return

        self.status = "finished"
        del self.neural_optimizer
        torch.cuda.empty_cache() 
        print("finished styling task: " + str(self.task_id))

class StylerQueue:
    queued_tasks = []
    finished_tasks = []
    running_task = None

    def __init__(self):
        thread = threading.Thread(target=self.status_checker, args=())
        thread.daemon = True                            # Daemonize thread
        thread.start()                                  # Start the execution

    def queue_task(self, *args):
        global total_task_count
        total_task_count += 1
        task_id = abs(hash(str(time.time())))
        print("queued task num. ", total_task_count, "with ID", task_id)
        task = StyleTask(task_id, *args)
        self.queued_tasks.append(task)

        return task_id

    def get_task(self, task_id):
        if self.running_task is not None and self.running_task.task_id == task_id:
            return self.running_task
        task = next((task for task in self.queued_tasks + self.finished_tasks if task.task_id == task_id), None)
        return task

    def status_checker(self):
        while True:
            time.sleep(0.3)

            if self.running_task is None:
                if len(self.queued_tasks) > 0:
                    self.running_task = self.queued_tasks[0]
                    self.running_task.start()
                    self.queued_tasks = self.queued_tasks[1:]
            elif self.running_task.status == "finished" or self.running_task.status == "error": 
                self.finished_tasks.append(self.running_task)
                if len(self.queued_tasks) > 0:
                    self.running_task = self.queued_tasks[0]
                    self.running_task.start()
                    self.queued_tasks = self.queued_tasks[1:]
                else:
                    self.running_task = None

styler_queue = StylerQueue()

@app.errorhandler(404)
def resource_not_found(e):
    return jsonify(message=str(e)), 404

@app.errorhandler(500)
def internal_server_error(e):
    return jsonify(message=str(e)), 500

@app.errorhandler(400)
def caught_error(e, *args):
    print(args)
    print(e)
    return jsonify(message=str(e.description)), 400

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    abort(404, "route not found")



@app.route('/upload', methods=['POST'])
def upload():
    if 'style-image' in request.files and \
        'content-image' in request.files:

        style_filename = photos.save(request.files['style-image'])
        content_filename = photos.save(request.files['content-image'])

        job_id = styler_queue.queue_task(style_filename, content_filename)
        print('added new stylization task', style_filename, content_filename)

        return jsonify({"task_id": job_id})
    abort(400, description="request needs style, content image")

@app.route('/get_status')
def get_status():
    if request.args.get("task_id") is None:
        abort(400, description="task_id needs to be supplied as parameter")
    task_id = int(request.args.get("task_id"))
    task = styler_queue.get_task(task_id)

    if task is None:
        abort(400, description="task with id %d not found"%task_id)

    status = {
        "status": task.status,
        "msg": task.error_msg
    }

    if task.status == "running":
        if isinstance(task, StyleTask):
            status["progress"] = float(task.neural_optimizer.cur_iteration) / float(default_args["iters"]) 

    return jsonify(status)

@app.route('/queue_length')
def get_queue_length():
    tasks = len(styler_queue.queued_tasks)
    if styler_queue.running_task is not None:
        tasks += 1

    status = {
        "length": tasks
    }

    return jsonify(status)


@app.route('/get_image')
def get_image():
    if request.args.get("task_id") is None:
        abort(400, description="task_id needs to be supplied as parameter")
    task_id = int(request.args.get("task_id"))
    task = styler_queue.get_task(task_id)

    if task is None:
        abort(400, description="task with id %d not found"%task_id)

    if task.status != "finished":
        abort(400, description="task with id %d not in finished state"%task_id)

    return send_file(os.path.join(image_folder, task.output_filename), mimetype='image/jpg')

@app.route('/get_vp')
def get_vp():
    if request.args.get("task_id") is None:
        abort(400, description="task_id needs to be supplied as parameter")
    task_id = int(request.args.get("task_id"))
    task = styler_queue.get_task(task_id)

    if task is None:
        abort(400, description="task with id %d not found"%task_id)

    if task.status != "finished":
        abort(400, description="task with id %d not in finished state"%task_id)

    return send_file(os.path.join(image_folder, task.vp_output_filename), mimetype='application/zip')


if __name__ == '__main__':
    app.run(debug=False, host="0.0.0.0",port=8600)