-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
51 lines (41 loc) · 1.45 KB
/
server.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
#model
from tensorflow.python.keras.models import load_model
import numpy as np
import os
from PIL import Image
from flask import Flask, request, render_template, make_response
app = Flask(__name__,static_folder='C:\\Python projects\\Garbage classifier\\Static')
#model
model = load_model('keras_model.h5')
model._make_predict_function()
def model_predict(img_path):
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
imag = Image.open(img_path)
imag = imag.resize((224, 224))
image_array = np.asarray(imag)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data[0] = normalized_image_array
pred = model.predict(data)
return pred
@app.route('/predict',methods=['POST'])
def predict():
if request.method == 'POST':
# Get the image from post request
image_file = request.files['imagefile']
filename = image_file.filename
filepath = os.path.join('C:\\Python projects\\Garbage classifier\\uploads', filename)
image_file.save(filepath)
# Make prediction
preds = model_predict(filepath)
if preds[0][0]>0.5:
prediction = "BIO"
else:
prediction = "NON-BIO"
return make_response(prediction,200)
@app.route('/',methods=['GET'])
def index():
return render_template('myproject.html')
@app.route('/demo',methods=['GET'])
def demo():
return "working"
app.run()