-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
121 lines (91 loc) · 3.44 KB
/
app.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
import base64
import uuid
import os
import cv2
import numpy as np
from flask import Flask, request, jsonify, send_file
from flask.views import MethodView
from celery.result import AsyncResult
from celery import Celery
from cv2 import dnn_superres
app_name = 'app'
app = Flask(app_name)
app.config['UPLOAD_FOLDER'] = 'files'
celery = Celery(
app_name,
backend='redis://localhost:6379/0',
broker='redis://localhost:6379/1',
# backend='redis://db-redis:6379/1',
# broker='redis://db-redis:6379/2',
broker_connection_retry_on_startup=True
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
@celery.task()
def upscale_image(data, image_name):
byte_data = data['image'].encode(encoding='utf-8')
image_bytes = base64.b64decode(byte_data)
# Создаем объект изображения из байтов
img = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
upscale(img, image_name)
def upscale_pickle():
scaler = dnn_superres.DnnSuperResImpl_create()
path = 'EDSR_x2.pb'
scaler.readModel(path)
scaler.setModel('edsr', 2)
return scaler
def upscale(input_image: cv2.typing.MatLike, output_path: str) -> None:
"""
:param input_path: путь к изображению для апскейла
:param output_path: путь к выходному файлу
:param model_path: путь к ИИ модели
:return:
"""
result = my_scaler_object.upsample(input_image)
cv2.imwrite(output_path, result)
class Transformation(MethodView):
def get(self, task_id):
task = AsyncResult(task_id, app=celery)
return jsonify({'status': task.status,
'result': task.result})
def post(self):
image = request.files['image'].read()
byte = base64.b64encode(image)
data = {'image': byte.decode('utf-8')}
image_name = self.name_image_out(request.files.get('image'))
task = upscale_image.delay(data, image_name)
# task = upscale_image(data, image_name)
return jsonify(
{'task_id': task.id}
)
def name_image_out(self, field):
extension = field.filename.split('.')[-1]
name = os.path.join('files', f'{uuid.uuid4()}.{extension}')
return name
def save_image(self, field):
image = request.files.get(field)
extension = image.filename.split('.')[-1]
path = os.path.join('files', f'{uuid.uuid4()}.{extension}')
image.save(path)
return path
def image_out(self, field):
extension = field.split('.')[-1]
name_path_out = field.split('.')[-2]
path = os.path.join(f'{name_path_out}_upscale.{extension}')
return path
class Processed(MethodView):
def get(self, file):
path = os.path.join('files', f'{file}')
return send_file(path, mimetype='image/jpeg')
transformation_view = Transformation.as_view('transformation')
processed_view = Processed.as_view('')
app.add_url_rule('/transformation/<string:task_id>', view_func=transformation_view, methods=['GET'])
app.add_url_rule('/transformation/', view_func=transformation_view, methods=['POST'])
app.add_url_rule('/processed/<string:file>', view_func=processed_view, methods=['GET'])
if __name__ == '__main__':
my_scaler_object = upscale_pickle()
app.run()