Commit
·
edace80
1
Parent(s):
866c7cf
Use FastAPI
Browse files- Dockerfile +1 -1
- app.py +102 -95
- requirements.txt +2 -0
Dockerfile
CHANGED
|
@@ -29,4 +29,4 @@ RUN pip install --no-cache-dir --upgrade -r $HOME/app/requirements.txt
|
|
| 29 |
|
| 30 |
COPY . $HOME/app
|
| 31 |
|
| 32 |
-
CMD ["
|
|
|
|
| 29 |
|
| 30 |
COPY . $HOME/app
|
| 31 |
|
| 32 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template, make_response
|
| 2 |
-
from flask_cors import CORS
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
|
@@ -8,96 +9,102 @@ from src.cyano import Cyanotype
|
|
| 8 |
from src.prediction import predict_img, optimize_img, update_patch
|
| 9 |
from src.utils import cv_to_pil, pil_to_cv
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
app
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from flask import Flask, request, jsonify, render_template, make_response
|
| 2 |
+
# from flask_cors import CORS
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
|
|
|
| 9 |
from src.prediction import predict_img, optimize_img, update_patch
|
| 10 |
from src.utils import cv_to_pil, pil_to_cv
|
| 11 |
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# UPLOAD_FOLDER = './uploads'
|
| 15 |
+
# app = Flask(__name__, template_folder='/client', static_folder='/client')
|
| 16 |
+
#
|
| 17 |
+
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 18 |
+
#
|
| 19 |
+
# CORS(
|
| 20 |
+
# app,
|
| 21 |
+
# supports_credentials=True
|
| 22 |
+
# )
|
| 23 |
+
|
| 24 |
+
@app.get("/")
|
| 25 |
+
def read_root():
|
| 26 |
+
return {"Hello": "World!"}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# @app.route('/api/process', methods=['POST'])
|
| 30 |
+
# def process():
|
| 31 |
+
# imgfile = request.files['img']
|
| 32 |
+
# img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
|
| 33 |
+
# img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
| 34 |
+
#
|
| 35 |
+
# data = request.form
|
| 36 |
+
# hue = int(data["hue"])
|
| 37 |
+
# saturation = float(data["saturation"])
|
| 38 |
+
# lightness = float(data["lightness"])
|
| 39 |
+
# contrast = int(data["contrast"])
|
| 40 |
+
# kelvin = int(data["kelvin"])
|
| 41 |
+
#
|
| 42 |
+
# img = control_contrast(img, contrast)
|
| 43 |
+
# img = control_HSV(img, hue, saturation, lightness)
|
| 44 |
+
#
|
| 45 |
+
# img_pil = cv_to_pil(img)
|
| 46 |
+
# img_pil = control_kelvin(img_pil, kelvin)
|
| 47 |
+
# img = pil_to_cv(img_pil)
|
| 48 |
+
#
|
| 49 |
+
# response = make_response(cv2.imencode('.png', img)[1].tobytes())
|
| 50 |
+
# response.headers.set('Content-Type', 'image/png')
|
| 51 |
+
#
|
| 52 |
+
# return response
|
| 53 |
+
#
|
| 54 |
+
#
|
| 55 |
+
# @app.route('/api/predict/<process_name>', methods=['POST'])
|
| 56 |
+
# def predict(process_name):
|
| 57 |
+
# if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']:
|
| 58 |
+
# return jsonify({ 'error': 'process name is invalid' })
|
| 59 |
+
#
|
| 60 |
+
# imgfile = request.files['img']
|
| 61 |
+
# img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
|
| 62 |
+
# img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
| 63 |
+
#
|
| 64 |
+
# # if 'colorpatch' in request.files:
|
| 65 |
+
# # patchfile = request.files['colorpatch']
|
| 66 |
+
# # patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8)
|
| 67 |
+
# # colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR)
|
| 68 |
+
# # update_patch(process_name, colorpatch)
|
| 69 |
+
#
|
| 70 |
+
# img = predict_img(process_name, img)
|
| 71 |
+
#
|
| 72 |
+
# response = make_response(cv2.imencode('.png', img)[1].tobytes())
|
| 73 |
+
# response.headers.set('Content-Type', 'image/png')
|
| 74 |
+
#
|
| 75 |
+
# return response
|
| 76 |
+
#
|
| 77 |
+
#
|
| 78 |
+
# @app.route('/api/optimize/<process_name>', methods=['POST'])
|
| 79 |
+
# def optimize(process_name):
|
| 80 |
+
# if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']:
|
| 81 |
+
# return jsonify({ 'error': 'process name is invalid' })
|
| 82 |
+
#
|
| 83 |
+
# imgfile = request.files['img']
|
| 84 |
+
# img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
|
| 85 |
+
# img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
| 86 |
+
#
|
| 87 |
+
# # if 'colorpatch' in request.files:
|
| 88 |
+
# # patchfile = request.files['colorpatch']
|
| 89 |
+
# # patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8)
|
| 90 |
+
# # colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR)
|
| 91 |
+
# # update_patch(process_name, colorpatch)
|
| 92 |
+
#
|
| 93 |
+
# (opt_img, preview_img) = optimize_img(process_name, img)
|
| 94 |
+
#
|
| 95 |
+
# h, w = preview_img.shape[:2]
|
| 96 |
+
# if process_name.endswith('full'):
|
| 97 |
+
# opt_img = np.reshape(opt_img, (h, w, 3))
|
| 98 |
+
# else:
|
| 99 |
+
# opt_img = np.reshape(opt_img, (h, w, 1))
|
| 100 |
+
# opt_img = np.array([[[i[0]] * 3 for i in j] for j in opt_img], dtype=np.uint8)
|
| 101 |
+
#
|
| 102 |
+
# img = cv2.hconcat([opt_img, preview_img])
|
| 103 |
+
# response = make_response(cv2.imencode('.png', img)[1].tobytes())
|
| 104 |
+
# response.headers.set('Content-Type', 'image/png')
|
| 105 |
+
#
|
| 106 |
+
# return response
|
| 107 |
+
#
|
| 108 |
+
#
|
| 109 |
+
# if __name__ == "__main__":
|
| 110 |
+
# app.run(debug=True, host="0.0.0.0", port=8000)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-i https://pypi.org/simple
|
|
|
|
|
|
|
| 2 |
absl-py==1.2.0; python_version >= '3.6'
|
| 3 |
astunparse==1.6.3
|
| 4 |
cachetools==5.2.0; python_version ~= '3.7'
|
|
|
|
| 1 |
-i https://pypi.org/simple
|
| 2 |
+
fastapi==0.74.*
|
| 3 |
+
uvicorn[standard]==0.17.*
|
| 4 |
absl-py==1.2.0; python_version >= '3.6'
|
| 5 |
astunparse==1.6.3
|
| 6 |
cachetools==5.2.0; python_version ~= '3.7'
|