-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
99 lines (75 loc) · 2.36 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# USAGE
# run this file
# 'http://localhost:5000'
#
# import the necessary packages
from PIL import Image
import cv2
import numpy as np
from keras.models import load_model
from keras.preprocessing import image
import sys
import flask
from flask import render_template
from flask import Flask, request, redirect, url_for
import io
import os
from werkzeug.utils import secure_filename
# initialize our Flask application and the Keras model
app = flask.Flask(__name__)
model = None
def loadmodel():
img_width, img_height = 150, 150
global model
model = load_model('con3-model.h5')
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
print("load success")
def load_image(img):
img = image.load_img(img, target_size=(150, 150))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
return img_tensor
#def prepare_image(image,target):
# if the image mode is not RGB, convert it
#if image.mode != "RGB":
# image = image.convert("RGB")
#image = cv2.resize(image,(150,150))
#img = np.reshape(image,[1,150,150,3])
#return img
@app.route("/predict", methods=["POST"])
def predict():
# ensure an image was properly uploaded to our endpoint
if flask.request.method == "POST":
print("POST")
if request.files['file']:
photo = request.files['file']
in_memory_file = io.BytesIO()
photo.save(in_memory_file)
print(photo)
#data = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8)
#print(data)
#color_image_flag = 1
#image = cv2.imdecode(data, color_image_flag)
image = load_image(photo)
#image = prepare_image(image,target=(150, 150))
#print(image)
preds = model.predict(image)
#print(preds[0][0])
#result ="sick-default"
if preds[0][0]>0.5:
result = "sick"
else:
result = "health"
return result
@app.route("/")
def display():
print("load")
return render_template("index.html")
# if this is the main thread of execution first load the model and
# then start the server
if __name__ == "__main__":
loadmodel()
app.run()