-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (44 loc) · 1.59 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
from flask import Flask, request, url_for
from flask_cors import CORS
import image_service
import time
import os
from ultralytics import YOLO
import json
from enums.TransferMode import Mode
STATIC_FOLDER = "static"
YOLO_MODEL_PATH = "models/seg/yolov8n-seg.pt"
app = Flask(__name__, static_folder=STATIC_FOLDER)
app.yolo_model = YOLO(YOLO_MODEL_PATH)
app.static_folder = STATIC_FOLDER
CORS(app)
@app.route("/process-image", methods=["POST"])
def process_image_controller():
start_time = time.time()
image_file = request.files["image"]
style_model_name, transfer_mode = parse_info(request.files["info"])
processed_image_path = image_service.process_image(
image_file, style_model_name, transfer_mode
)
processed_image_url = url_for(
STATIC_FOLDER, filename=processed_image_path, _external=True
)
end_time = time.time()
return {
"processed_image_path": processed_image_url,
"execution_time": format(end_time - start_time, ".2f"),
}
@app.route("/get-classes", methods=["GET"])
def read_collection_classes_controller():
return image_service.get_segmentation_classes()
@app.route("/get-styles", methods=["GET"])
def read_collection_styles_controller():
return image_service.get_styles(request)
def parse_info(style_file):
info_content = style_file.read()
info_data = json.loads(info_content)
return info_data.get("style"), Mode(info_data.get("mode"))
if __name__ == "__main__":
processed_images_dir = os.path.join(app.static_folder, "processed_images")
os.makedirs(processed_images_dir, exist_ok=True)
app.run()