-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plant_Disease_AI.py
60 lines (49 loc) · 2.08 KB
/
Plant_Disease_AI.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
import flask
import numpy as np
import pickle
import cv2
import tensorflow as tf
from sklearn.preprocessing import LabelBinarizer
from keras.models import load_model
from keras.preprocessing import image
from keras.preprocessing.image import img_to_array
import os
from flask import Flask, flash, request, redirect, url_for,send_from_directory
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
labels=pickle.load(open('label_transform.pkl', "rb"))
model=load_model('modelpath')
app = flask.Flask(__name__,template_folder='templates')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def main():
if flask.request.method == 'GET':
return(flask.render_template('index.html'))
if flask.request.method == 'POST':
data = {"success": "Error"}
file = request.files['file']
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
fName=os.path.join(app.config['UPLOAD_FOLDER'], filename)
image = cv2.imread(fName)
if image is not None :
image = cv2.resize(image, (128,128))
tstImg=img_to_array(image)
image = np.expand_dims(tstImg, axis=0)
np_image = np.array(image, dtype=np.float16)
predict=model.predict(np_image)
prediction=model.predict_classes(np_image)
data["Class"]=labels.classes_[prediction][0].replace('_',' ')
data["predict"]='{0:.2f}'.format(predict.max()*100)
data["success"]= "Success"
data["img"]=filename
return flask.render_template('index.html', scroll='frm',result=data)
@app.route('/uploads/<filename>')
def send_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(debug = True)