-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
70 lines (57 loc) · 2.12 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
70
from flask import Flask, render_template, request, jsonify,redirect
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg19 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
from io import BytesIO
model = None
app = Flask(__name__)
def load_model():
#open file with model architecture
json_file = open('model/model.json','r')
loaded_model_json = json_file.read()
json_file.close()
global model
model = model_from_json(loaded_model_json)
#load weights into new model
model.load_weights("model/model.h5")
print(model.summary())
def process_image(image):
#read image
image = Image.open(BytesIO(image))
if image.mode != "RGB":
image = image.convert("RGB")
# resize and convert to tensor
image = image.resize((96, 96))
image = img_to_array(image)
image = preprocess_input(image)
image = np.expand_dims(image, axis=0)
return image
@app.route("/", methods=["POST","GET"])
def index():
predictions = {}
if request.method == "POST":
# only make predictions after sucessfully receiving the file
if request.files:
try:
image = request.files["image"].read()
image = process_image(image)
out = model.predict(image)
# send the predictions to index page
predictions = {"positive":str(np.round(out[0][1],2)),"negative":str(np.round(out[0][0],2))}
except:
predictions ={}
redirect('/')
return render_template("index.html",predictions=predictions)
if __name__ == "__main__":
load_model()
app.run(debug = True, threaded = False)
if __name__ == "app":
load_model()