-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
53 lines (42 loc) · 1.85 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
from flask import Flask, request,render_template,send_from_directory
from werkzeug.utils import secure_filename
import numpy as np
import tensorflow as tf
import os
from PIL import Image
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
app = Flask(__name__)
class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
def predict(model, img):
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
predictions_arr = [round(100*i,2) for i in predictions[0]]
predicted_class = class_names[np.argmax(predictions[0])]
confidence = round(100 * (np.max(predictions[0])), 2)
return predicted_class, predictions_arr
model = tf.keras.models.load_model('potato_model.h5',compile=False)
@app.route('/predict',methods=['POST'])
def predict_image():
if request.method == 'POST':
print("request received")
print(request.files)
file = request.files['image']
filename = secure_filename(file.filename)
img = Image.open(file.stream)
img = img.resize((256,256))
img_array = np.array(img)
predicted_class,predictions = predict(model,img_array)
response = {"predicted_class": f"{predicted_class}" ,"early": f"{predictions[0]:.2f}%","late": f"{predictions[1]:.2f}%","healthy": f"{predictions[2]:.2f}%"}
img.save(os.path.join('uploads',filename))
print(response)
print(f"Predictions : {predictions}")
return render_template('result.html',result=response,image_path=f'uploads/{filename}')
@app.route('/uploads/<path:filename>')
def serve_uploaded_file(filename):
return send_from_directory('uploads', filename)
@app.route('/')
def index():
return render_template('index.html',mimetype='text/html')
if __name__ == '__main__':
app.run(debug=True)