shikibu9419 commited on
Commit
edace80
·
1 Parent(s): 866c7cf

Use FastAPI

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. app.py +102 -95
  3. 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 ["gunicorn", "app:app", "--bind", "0.0.0.0:5000"]
 
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
- UPLOAD_FOLDER = './uploads'
12
- app = Flask(__name__, template_folder='/client', static_folder='/client')
13
-
14
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
15
-
16
- CORS(
17
- app,
18
- supports_credentials=True
19
- )
20
-
21
-
22
- @app.route('/api/process', methods=['POST'])
23
- def process():
24
- imgfile = request.files['img']
25
- img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
26
- img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
27
-
28
- data = request.form
29
- hue = int(data["hue"])
30
- saturation = float(data["saturation"])
31
- lightness = float(data["lightness"])
32
- contrast = int(data["contrast"])
33
- kelvin = int(data["kelvin"])
34
-
35
- img = control_contrast(img, contrast)
36
- img = control_HSV(img, hue, saturation, lightness)
37
-
38
- img_pil = cv_to_pil(img)
39
- img_pil = control_kelvin(img_pil, kelvin)
40
- img = pil_to_cv(img_pil)
41
-
42
- response = make_response(cv2.imencode('.png', img)[1].tobytes())
43
- response.headers.set('Content-Type', 'image/png')
44
-
45
- return response
46
-
47
-
48
- @app.route('/api/predict/<process_name>', methods=['POST'])
49
- def predict(process_name):
50
- if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']:
51
- return jsonify({ 'error': 'process name is invalid' })
52
-
53
- imgfile = request.files['img']
54
- img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
55
- img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
56
-
57
- # if 'colorpatch' in request.files:
58
- # patchfile = request.files['colorpatch']
59
- # patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8)
60
- # colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR)
61
- # update_patch(process_name, colorpatch)
62
-
63
- img = predict_img(process_name, img)
64
-
65
- response = make_response(cv2.imencode('.png', img)[1].tobytes())
66
- response.headers.set('Content-Type', 'image/png')
67
-
68
- return response
69
-
70
-
71
- @app.route('/api/optimize/<process_name>', methods=['POST'])
72
- def optimize(process_name):
73
- if not process_name in ['cyanotype_mono', 'cyanotype_full', 'salt', 'platinum']:
74
- return jsonify({ 'error': 'process name is invalid' })
75
-
76
- imgfile = request.files['img']
77
- img_array = np.asarray(bytearray(imgfile.stream.read()), dtype=np.uint8)
78
- img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
79
-
80
- # if 'colorpatch' in request.files:
81
- # patchfile = request.files['colorpatch']
82
- # patch_array = np.asarray(bytearray(patchfile.stream.read()), dtype=np.uint8)
83
- # colorpatch = cv2.imdecode(colorpatch_array, cv2.IMREAD_COLOR)
84
- # update_patch(process_name, colorpatch)
85
-
86
- (opt_img, preview_img) = optimize_img(process_name, img)
87
-
88
- h, w = preview_img.shape[:2]
89
- if process_name.endswith('full'):
90
- opt_img = np.reshape(opt_img, (h, w, 3))
91
- else:
92
- opt_img = np.reshape(opt_img, (h, w, 1))
93
- opt_img = np.array([[[i[0]] * 3 for i in j] for j in opt_img], dtype=np.uint8)
94
-
95
- img = cv2.hconcat([opt_img, preview_img])
96
- response = make_response(cv2.imencode('.png', img)[1].tobytes())
97
- response.headers.set('Content-Type', 'image/png')
98
-
99
- return response
100
-
101
-
102
- if __name__ == "__main__":
103
- app.run(debug=True, host="0.0.0.0", port=8000)
 
 
 
 
 
 
 
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'