AB2003 commited on
Commit
161ee83
·
verified ·
1 Parent(s): 14cb797

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Import necessary libraries
2
+ from flask import Flask, render_template, request
3
+
4
+ import numpy as np
5
+ import os
6
+
7
+ from keras.preprocessing.image import load_img
8
+ from keras.preprocessing.image import img_to_array
9
+ from keras.models import load_model
10
+
11
+ #load model
12
+ model =load_model("model/v4_1_pred_stra_dis.h5")
13
+
14
+ print('@@ Model loaded')
15
+
16
+
17
+ def pred_cot_dieas(cott_plant):
18
+ test_image = load_img(cott_plant, target_size = (150, 150)) # load image
19
+ print("@@ Got Image for prediction")
20
+
21
+ test_image = img_to_array(test_image)/255 # convert image to np array and normalize
22
+ test_image = np.expand_dims(test_image, axis = 0) # change dimention 3D to 4D
23
+
24
+ result = model.predict(test_image).round(3) # predict diseased palnt or not
25
+ print('@@ Raw result = ', result)
26
+
27
+ pred = np.argmax(result) # get the index of max value
28
+
29
+ if pred == 0:
30
+ return "Diseased Strawberry Plant", 'angular_leafspot.html' # if index 0 burned leaf
31
+ elif pred == 1:
32
+ return 'Diseased Strawberry Plant', 'grey_mold.html' # # if index 1
33
+ elif pred == 2:
34
+ return 'Diseased Strawberry Plant', 'leaf_spot.html' # if index 2 fresh leaf
35
+ else:
36
+ return "Diseased Strawberry Plant", 'powdery_mildew_leaf.html' # if index 3
37
+
38
+ #------------>>pred_cot_dieas<<--end
39
+
40
+ # Create flask instance
41
+ app = Flask(__name__)
42
+
43
+ # render index.html page
44
+ @app.route("/", methods=['GET', 'POST'])
45
+ def home():
46
+ return render_template('index1.html')
47
+
48
+
49
+ # get input image from client then predict class and render respective .html page for solution
50
+ @app.route("/predict", methods = ['GET','POST'])
51
+ def predict():
52
+ if request.method == 'POST':
53
+ file = request.files['image'] # fet input
54
+ filename = file.filename
55
+ print("@@ Input posted = ", filename)
56
+
57
+ file_path = os.path.join('static/user uploaded', filename)
58
+ file.save(file_path)
59
+
60
+ print("@@ Predicting class......")
61
+ pred, output_page = pred_cot_dieas(cott_plant=file_path)
62
+
63
+ return render_template(output_page, pred_output = pred, user_image = file_path)
64
+
65
+ # For local system & cloud
66
+ if __name__ == "__main__":
67
+ app.run(threaded=False,)
68
+
69
+