-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
69 lines (53 loc) · 1.82 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import base64
import numpy as np
import io
from PIL import Image
import keras
from keras.models import Sequential, load_model
from keras.preprocessing.image import ImageDataGenerator, img_to_array
from flask import Flask, request, render_template, jsonify
app = Flask(__name__)
classes = ["cloud", "moon", "rainbow", "star", "sun"]
def get_model():
global model
model = load_model("model_new.h5")
print("Model Loaded")
def preprocess_img(image, target_size, inv):
image = image.convert("L")
image = image.resize(target_size)
if inv==True :
image=np.invert(image)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
return image
print("loading model...")
get_model()
@app.route('/')
def index():
return render_template("index.html")
@app.route("/predict-image/", methods = ["GET","POST"])
def predict_img():
message = request.get_json(force=True)
encoded = message["image"]
decoded = base64.b64decode(encoded)
image = Image.open(io.BytesIO(decoded))
processed_img = preprocess_img(image, target_size=(28,28), inv=False)
pred = model.predict(processed_img)
idx = np.argmax(np.array(pred[0]))
response = {
'predictionImg' : str(classes[idx])
}
return jsonify(response)
@app.route("/predict-drawing/", methods = ["GET","POST"])
def predict_draw():
message = request.get_json(force=True)
encoded = message["image"]
decoded = base64.b64decode(encoded)
image = Image.open(io.BytesIO(decoded))
processed_img = preprocess_img(image, target_size=(28,28), inv=True)
pred = model.predict(processed_img)
idx = np.argmax(np.array(pred[0]))
response = {
'predictionDraw' : str(classes[idx])
}
return jsonify(response)