Last active
July 18, 2018 11:06
-
-
Save MattKovtun/44808cf178fb84a7f21bb118b4450192 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment