-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.py
47 lines (34 loc) · 1.16 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
from flask_cors import CORS
from flask import Flask, request, render_template, json, jsonify, send_from_directory
import json
import cv2
import numpy as np
import io
app = Flask(__name__)
CORS(app)
@app.route("/", methods=["GET"])
def main():
return render_template('index.html')
@app.route("/api/prepare", methods=["POST"])
def prepare():
file = request.files['file']
res = preprocessing(file)
return json.dumps({"image": res.tolist()})
@app.route('/model')
def model():
json_data = json.load(open("./model_js/model.json"))
return jsonify(json_data)
@app.route('/<path:path>')
def load_shards(path):
return send_from_directory('model_js', path)
def preprocessing(file):
in_memory_file = io.BytesIO()
file.save(in_memory_file)
data = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8)
img = cv2.imdecode(data, 0)
res = cv2.resize(img, dsize=(28, 28), interpolation=cv2.INTER_CUBIC)
# file.save("static/UPLOAD/img.png") # saving uploaded img
# cv2.imwrite("static/UPLOAD/test.png", res) # saving processed image
return res
if __name__ == "__main__":
app.run()