Skip to content

Instantly share code, notes, and snippets.

@MattKovtun
Last active July 18, 2018 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattKovtun/44808cf178fb84a7f21bb118b4450192 to your computer and use it in GitHub Desktop.
Save MattKovtun/44808cf178fb84a7f21bb118b4450192 to your computer and use it in GitHub Desktop.
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