-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
42 lines (33 loc) · 993 Bytes
/
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
from flask import Flask, request, jsonify
import numpy as np
import cv2
import tensorflow as tf
app = Flask(__name__)
model = None
# Load the pre-trained model
def load_model():
global model
model = tf.keras.models.load_model("handwritten.h5")
# Preprocess the input data
def preprocess_input(data):
img = np.array(data)
img = cv2.resize(img, (28, 28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.invert(img)
img = img.reshape((1, 28, 28, 1))
img = img / 255.0
return img
# Predict the digit
@app.route("/predict", methods=["POST"])
def predict():
try:
data = request.json["data"] # The drawing data from the JavaScript app
img = preprocess_input(data)
prediction = model.predict(img)
digit = np.argmax(prediction)
return jsonify({"digit": int(digit)})
except Exception as e:
return jsonify({"error": str(e)}), 400
if __name__ == "__main__":
load_model()
app.run(port=5000)