|
from flask import Flask, request, jsonify, render_template, make_response |
|
from flask_cors import CORS |
|
import numpy as np |
|
import cv2 |
|
|
|
from src.color_controls import control_kelvin, control_contrast, control_HSV |
|
from src.cyano import Cyanotype |
|
from src.prediction import predict_img, optimize_img, update_patch |
|
from src.utils import cv_to_pil, pil_to_cv |
|
|
|
UPLOAD_FOLDER = './uploads' |
|
app = Flask(__name__, template_folder='/client', static_folder='/client') |
|
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
|
CORS( |
|
app, |
|
supports_credentials=True |
|
) |
|
|
|
|
|
@app.route('/api/process', methods=['POST']) |
|
def process(): |
|
imgfile = request.files['img'] |
|
img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8) |
|
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) |
|
|
|
data = request.form |
|
hue = int(data["hue"]) |
|
saturation = float(data["saturation"]) |
|
lightness = float(data["lightness"]) |
|
contrast = int(data["contrast"]) |
|
kelvin = int(data["kelvin"]) |
|
|
|
img = control_contrast(img, contrast) |
|
img = control_HSV(img, hue, saturation, lightness) |
|
|
|
img_pil = cv_to_pil(img) |
|
img_pil = control_kelvin(img_pil, kelvin) |
|
img = pil_to_cv(img_pil) |
|
|
|
response = make_response(cv2.imencode('.png', img)[1].tobytes()) |
|
response.headers.set('Content-Type', 'image/png') |
|
|
|
return response |
|
|
|
|
|
@app.route('/api/predict/<process_name>', methods=['POST']) |
|
def predict(process_name): |
|
if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']: |
|
return jsonify({ 'error': 'process name is invalid' }) |
|
|
|
imgfile = request.files['img'] |
|
img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8) |
|
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) |
|
|
|
if 'colorpatch' in request.files: |
|
patchfile = request.files['colorpatch'] |
|
patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8) |
|
colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR) |
|
update_patch(process_name, colorpatch) |
|
|
|
img = predict_img(process_name, img) |
|
|
|
response = make_response(cv2.imencode('.png', img)[1].tobytes()) |
|
response.headers.set('Content-Type', 'image/png') |
|
|
|
return response |
|
|
|
|
|
@app.route('/api/optimize/<process_name>', methods=['POST']) |
|
def optimize(process_name): |
|
if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']: |
|
return jsonify({ 'error': 'process name is invalid' }) |
|
|
|
imgfile = request.files['img'] |
|
img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8) |
|
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) |
|
|
|
if 'colorpatch' in request.files: |
|
patchfile = request.files['colorpatch'] |
|
patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8) |
|
colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR) |
|
update_patch(process_name, colorpatch) |
|
|
|
(opt_img, preview_img) = optimize_img(process_name, img) |
|
|
|
h, w = preview_img.shape[:2] |
|
if process_name.endswith('full'): |
|
opt_img = np.reshape(opt_img, (h, w, 3)) |
|
else: |
|
opt_img = np.reshape(opt_img, (h, w, 1)) |
|
opt_img = np.array([[[i[0]] * 3 for i in j] for j in opt_img], dtype=np.uint8) |
|
|
|
img = cv2.hconcat([opt_img, preview_img]) |
|
response = make_response(cv2.imencode('.png', img)[1].tobytes()) |
|
response.headers.set('Content-Type', 'image/png') |
|
|
|
return response |
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True, port=8000) |
|
|