Commit 6f1f8d16 authored by lochanar99's avatar lochanar99

finalized image processing model

parent a211f1e1
*.hdf5 filter=lfs diff=lfs merge=lfs -text
1. Training.ipynb file contain the training code which i used to train the model first. I used sequential model to train our model using new Plant leaves disease dataset.
2. After training of our model is done I used that trained file in my GUI.
3. Then I used keras model to Predict the results according to our trained model.
4. The trained system can only pridict the diseases of the plant on which it is trained on.
**************************
1. I have an image processing model which detect dieseased plants according to the leaf.
Trained over many data ( 1gb ), i will provide you the model and the enviorment files.
The model should be added to my flask back end application as a function.
2. The flask backend application code will also be given to you, the database which is used is mongoDB.
3. Host the flask application on heroku.( or locally ).
4. From mongoDB take images and process through the python image processing model( now in flask app),
and give the result in the backend as diseased or not
\ No newline at end of file
import json
from flask import Flask, request, render_template, json
from flask_pymongo import PyMongo
from PIL import Image
from h5py._hl import datatype
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import pandas as pd
import base64
model = load_model('train_data.hdf5')
model.load_weights('weights.hdf5')
# This function will return the label for the given image
def prediction(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img / 255
pred = model.predict(img)
y = np.argmax(pred)
classname = pd.read_csv('classes.csv', encoding='latin1')
result = classname.iloc[y][1:3]
return result.to_json()
# initializing the flask and mongodb instance
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb+srv://flask-db:agri1234567@afcluster0.uyijz.mongodb.net/flask-backend?retryWrites=true&w=majority"
mongo = PyMongo(app)
# Currenlty I am uploading image thorough form request.
@app.route('/')
def index():
return render_template('index.html')
# This route shall accept the file from your react native app to be uploaded to the server and it wll add that image to the database and
# the uploads folder where all the images will be stored
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
data = request.form['file']
# wriet the data to a file
with open('encoded.txt', 'w') as f:
f.write(data)
file = open('encoded.txt', 'rb')
byte = file.read()
file.close()
decodeit = open('uploaded_image.jpg', 'wb')
decodeit.write(base64.b64decode((byte)))
decodeit.close()
imageclass = prediction('uploaded_image.jpg')
mongo.db.image.insert({'db_image_name': data, 'image_class': imageclass})
data2 = json.loads(imageclass)
return "Predicted Disease is " + data2['classes'] + " and Recommended Pesticide is " + data2['pesticide']
else:
return "Error"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
import json
from flask import Flask, request, render_template, json
#from flask_pymongo import PyMongo
from PIL import Image
from h5py._hl import datatype
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import pandas as pd
import base64
model = load_model('train_data.hdf5')
model.load_weights('weights.hdf5')
# This function will return the label for the given image
def prediction(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img / 255
pred = model.predict(img)
y = np.argmax(pred)
classname = pd.read_csv('classes.csv', encoding='latin1')
result = classname.iloc[y][1:3]
return result.to_json()
#return "Predicted Disease is " + str(diseaseName) + " and Recommended Pesticide is " + str(pesticidename)
# initializing the flask and mongodb instance
app = Flask(__name__)
# app.config["MONGO_URI"] = "mongodb://localhost:27017/leafdiseasedetection"
# mongo = PyMongo(app)
# Currenlty I am uploading image thorough form request.
@app.route('/')
def index():
return render_template('index.html')
# This route shall accept the file from your react native app to be uploaded to the server and it wll add that image to the database and
# the uploads folder where all the images will be stored
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
data = request.form['file']
# wriet the data to a file
with open('encoded.txt', 'w') as f:
f.write(data)
file = open('encoded.txt', 'rb')
byte = file.read()
file.close()
decodeit = open('uploaded_image.jpg', 'wb')
decodeit.write(base64.b64decode((byte)))
decodeit.close()
imageclass = prediction('uploaded_image.jpg')
# mongo.db.image.insert({'db_image_name': data, 'image_class': imageclass})
data = json.loads(imageclass)
return "Predicted Disease is " + data['classes'] + " and Recommended Pesticide is " + data['pesticide']
else:
return "Error"
if __name__ == "__main__":
app.run()
,label
Apple___Apple_scab,0
Apple___Black_rot,1
Apple___Cedar_apple_rust,2
Apple___healthy,3
Blueberry___healthy,4
Cherry_(including_sour)___Powdery_mildew,5
Cherry_(including_sour)___healthy,6
Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot,7
Corn_(maize)___Common_rust_,8
Corn_(maize)___Northern_Leaf_Blight,9
Corn_(maize)___healthy,10
Grape___Black_rot,11
Grape___Esca_(Black_Measles),12
Grape___Leaf_blight_(Isariopsis_Leaf_Spot),13
Grape___healthy,14
Orange___Haunglongbing_(Citrus_greening),15
Peach___Bacterial_spot,16
Peach___healthy,17
"Pepper,_bell___Bacterial_spot",18
"Pepper,_bell___healthy",19
Potato___Early_blight,20
Potato___Late_blight,21
Potato___healthy,22
Raspberry___healthy,23
Soybean___healthy,24
Squash___Powdery_mildew,25
Strawberry___Leaf_scorch,26
Strawberry___healthy,27
Tomato___Bacterial_spot,28
Tomato___Early_blight,29
Tomato___Late_blight,30
Tomato___Leaf_Mold,31
Tomato___Septoria_leaf_spot,32
Tomato___Spider_mites Two-spotted_spider_mite,33
Tomato___Target_Spot,34
Tomato___Tomato_Yellow_Leaf_Curl_Virus,35
Tomato___Tomato_mosaic_virus,36
Tomato___healthy,37
label,pesticide,classes
0,Tebuconazole,Apple Apple scab
1,Myclobutanil,Apple Black rot
2,Myclobutanil (Immunox),Apple Cedar apple rust
3,,Apple healthy
4,,Blueberry healthy
5,Merivon fluxapyroxad+pyraclostrobin,Cherry (including sour) Powdery mildew
6,,Cherry (including sour) healthy
7,Patch Pro.,Corn (maize) Cercospora leaf spot Gray leaf spot
8,Common Fungicides,Corn (maize) Common rust
9,"
Shamrock Al-sach With Logo Label Mark M45 Mancozeb ",Corn (maize) Northern Leaf Blight
10,,Corn (maize) healthy
11,"Mancozeb, and Ziram",Grape Black rot
12,"Mancozeb, and Ziram",Grape Esca (Black Measles)
13,"Bordeaux mixture (1.0%),Mancozeb (0.2%), Topsin-M (0.1%), Ziram (0.35%) or
Captan (0.2%)",Grape Leaf blight (Isariopsis Leaf Spot)
14,,Grape healthy
15,"No cure, Redo Process",Orange Haunglongbing (Citrus greening)
16,"Copper, oxytetracycline",Peach Bacterial spot
17,,Peach healthy
18,Copper and Mancozeb sprays,Pepper bell Bacterial spot
19,,Pepper bell healthy
20,Avoid Overhead Irrigation and allow sufficient aeration,Potato Early blight
21,prophylactic spray ofmancozeb,Potato Late blight
22,,Potato healthy
23,,Raspberry healthy
24,,Soybean healthy
25,Apply sulfur or copper-based fungicides,Squash Powdery mildew
26,Copper-Count-N at 1.5 to 2 quart/100 gal water. Group M1 fungicide,Strawberry Leaf scorch
27,,Strawberry healthy
28,Bonide Liquid Copper Fungicide concentrate or Bonide Tomato & Vegetable,Tomato Bacterial spot
29,Bonide Liquid Copper Fungicide concentrate or Bonide Tomato & Vegetable,Tomato Early blight
30,Bonide Liquid Copper Fungicide concentrate or Bonide Tomato & Vegetable,Tomato Late blight
31,Bonide Liquid Copper Fungicide concentrate or Bonide Tomato & Vegetable,Tomato Leaf Mold
32,Bonide Liquid Copper Fungicide concentrate or Bonide Tomato & Vegetable,Tomato Septoria leaf spot
33,bifenthrin and permethrin,Tomato Spider mites Two-spotted spider mite
34,bifenthrin and permethrin,Tomato Target Spot
35,Imidacloprid,Tomato Tomato Yellow Leaf Curl Virus
36,"Viral Infection, Remove all infected Plants",Tomato Tomato mosaic virus
37,,Tomato healthy
This diff is collapsed.
This diff is collapsed.
numpy~=1.19.2
pandas~=1.3.3
flask~=1.1.2
pillow~=8.3.1
tensorflow~=2.6.0
gunicorn~=20.1.0
\ No newline at end of file
import os
import pickle
import predict
import numpy as np
Apple_diseases=['Apple___Apple_scab','Apple___Black_rot','Apple___Cedar_apple_rust','Apple___healthy']
Badam_diseases=['bad','good']
Blueberry_diseases=['Blueberry___healthy']
Cherry_diseases=['Cherry_(including_sour)___healthy','Cherry_(including_sour)___Powdery_mildew']
Corn_diseases=['Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot','Corn_(maize)___Common_rust_','Corn_(maize)___healthy','Corn_(maize)___Northern_Leaf_Blight']
Grape_diseases=['Grape___Black_rot','Grape___Esca_(Black_Measles)','Grape___healthy','Grape___Leaf_blight_(Isariopsis_Leaf_Spot)']
Orange_diseases=['Orange___Haunglongbing_(Citrus_greening)']
Paddy_diseases=['Bacterial leaf blight','Brown spot','Leaf smut']
Peach_diseases=['Peach___Bacterial_spot','Peach___healthy']
Pepper_diseases=['Pepper,_bell___Bacterial_spot','Pepper,_bell___healthy']
Potato_diseases=['Potato___Early_blight','Potato___healthy','Potato___Late_blight']
Raspberry_diseases=['Raspberry___healthy']
Rose_diseases=['anthracnose','mildew','rust','spot']
Soyabean_diseases=['Soybean___healthy']
Squash_diseases=['Squash___Powdery_mildew']
Strawberry_diseases=['Strawberry___healthy','Strawberry___Leaf_scorch']
Sunflower_diseases=['blight','mildew','rust']
Tomato_diseases=['Tomato___Bacterial_spot','Tomato___Early_blight','Tomato___healthy','Tomato___Late_blight','Tomato___Leaf_Mold','Tomato___Septoria_leaf_spot','Tomato___Spider_mites Two-spotted_spider_mite','Tomato___Target_Spot','Tomato___Tomato_mosaic_virus','Tomato___Tomato_Yellow_Leaf_Curl_Virus']
diseases = [Apple_diseases,Badam_diseases,Blueberry_diseases,Cherry_diseases,Corn_diseases,Grape_diseases,Orange_diseases,Paddy_diseases,Peach_diseases,Pepper_diseases,Potato_diseases,Raspberry_diseases,Rose_diseases,Soyabean_diseases,Squash_diseases,Strawberry_diseases,Sunflower_diseases,Tomato_diseases]
Species = ["Apple","Badam","Blueberry","Cherry","Corn","Grape","Orange","Paddy","Peach","Pepper","Potato","Raspberry","Rose","Soyabean","Squash","Strawberry","Sunflower","Tomato"]
def classify_disease(imgPath, species='unknown'):
if species=='unknown':
species = classify_species(imgPath)[0]
#open the list of inference models and build respective modelPath
pickle_in = open(model_detail,"rb")
models_dict = pickle.load(pickle_in)
modelPath = "trainedModels/inferenceModels/protobuf/diseases/"+species+'/'+models_dict[species][0]
#the largest probability is provided for the class in prediction
prediction = predict.predict(imgPath, modelPath)
pred = np.argmax(prediction)
pred_class = diseases[models_dict[species][2]][pred]
return [pred_class, str(max(prediction))]
from PIL import Image
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import pandas as pd
import base64
model = load_model('train_data.hdf5')
model.load_weights('weights.hdf5')
def prediction(img_path):
# PIL image loading
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img/255
pred = model.predict(img)
y = np.argmax(pred)
print(y)
classes = pd.read_csv('classes.csv', encoding='latin1')
prediction = classes.iloc[1][1:3]
return prediction.to_json()
# file = open('encoded.txt', 'rb')
# byte = file.read()
# file.close()
# decodeit = open('uploaded_image.jpg', 'wb')
# decodeit.write(base64.b64decode((byte)))
# decodeit.close()
print(prediction('images/blackrot.jpg'))
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=, initial-scale=1.0">
<title>Leaf Disease Prediction</title>
</head>
<body>
Please download the app to get prediction
</body>
</html>
\ No newline at end of file
File added
This diff is collapsed.
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment