forked from mohitmore2001/Farmers-Friend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3820c2
commit f83766a
Showing
42 changed files
with
16,284 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
,Crop,N,P,K,pH,soil_moisture | ||
0,rice,80,40,40,5.5,30 | ||
3,maize,80,40,20,5.5,50 | ||
5,chickpea,40,60,80,5.5,60 | ||
12,kidneybeans,20,60,20,5.5,45 | ||
13,pigeonpeas,20,60,20,5.5,45 | ||
14,mothbeans,20,40,20,5.5,30 | ||
15,mungbean,20,40,20,5.5,80 | ||
18,blackgram,40,60,20,5,60 | ||
24,lentil,20,60,20,5.5,90 | ||
60,pomegranate,20,10,40,5.5,30 | ||
61,banana,100,75,50,6.5,40 | ||
62,mango,20,20,30,5,15 | ||
63,grapes,20,125,200,4,60 | ||
66,watermelon,100,10,50,5.5,70 | ||
67,muskmelon,100,10,50,5.5,30 | ||
69,apple,20,125,200,6.5,50 | ||
74,orange,20,10,10,4,60 | ||
75,papaya,50,50,50,6,20 | ||
88,coconut,20,10,30,5,45 | ||
93,cotton,120,40,20,5.5,70 | ||
94,jute,80,40,40,5.5,20 | ||
95,coffee,100,20,30,5.5,20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn app:app --log-level debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-3.6.12 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# Importing essential libraries and modules | ||
|
||
|
||
from flask import Flask, render_template, request, Markup,redirect | ||
import numpy as np | ||
import pandas as pd | ||
from utils.disease import disease_dic | ||
import requests | ||
import pickle | ||
import io | ||
import torch | ||
from torchvision import transforms | ||
from PIL import Image | ||
from utils.model import ResNet9 | ||
# ============================================================================================== | ||
|
||
# -------------------------LOADING THE TRAINED MODELS ----------------------------------------------- | ||
|
||
# Loading plant disease classification model | ||
|
||
disease_classes = ['Apple___Apple_scab', | ||
'Apple___Black_rot', | ||
'Apple___Cedar_apple_rust', | ||
'Apple___healthy', | ||
'Blueberry___healthy', | ||
'Cherry_(including_sour)___Powdery_mildew', | ||
'Cherry_(including_sour)___healthy', | ||
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', | ||
'Corn_(maize)___Common_rust_', | ||
'Corn_(maize)___Northern_Leaf_Blight', | ||
'Corn_(maize)___healthy', | ||
'Grape___Black_rot', | ||
'Grape___Esca_(Black_Measles)', | ||
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', | ||
'Grape___healthy', | ||
'Orange___Haunglongbing_(Citrus_greening)', | ||
'Peach___Bacterial_spot', | ||
'Peach___healthy', | ||
'Pepper,_bell___Bacterial_spot', | ||
'Pepper,_bell___healthy', | ||
'Potato___Early_blight', | ||
'Potato___Late_blight', | ||
'Potato___healthy', | ||
'Raspberry___healthy', | ||
'Soybean___healthy', | ||
'Squash___Powdery_mildew', | ||
'Strawberry___Leaf_scorch', | ||
'Strawberry___healthy', | ||
'Tomato___Bacterial_spot', | ||
'Tomato___Early_blight', | ||
'Tomato___Late_blight', | ||
'Tomato___Leaf_Mold', | ||
'Tomato___Septoria_leaf_spot', | ||
'Tomato___Spider_mites Two-spotted_spider_mite', | ||
'Tomato___Target_Spot', | ||
'Tomato___Tomato_Yellow_Leaf_Curl_Virus', | ||
'Tomato___Tomato_mosaic_virus', | ||
'Tomato___healthy'] | ||
|
||
disease_model_path = 'models/plant_disease_model.pth' | ||
disease_model = ResNet9(3, len(disease_classes)) | ||
disease_model.load_state_dict(torch.load( | ||
disease_model_path, map_location=torch.device('cpu'))) | ||
disease_model.eval() | ||
|
||
|
||
# Loading crop recommendation model | ||
|
||
crop_recommendation_model_path = 'models/RandomForest.pkl' | ||
crop_recommendation_model = pickle.load( | ||
open(crop_recommendation_model_path, 'rb')) | ||
|
||
|
||
# ========================================================================================= | ||
|
||
# Custom functions for calculations | ||
|
||
def predict_image(img, model=disease_model): | ||
""" | ||
Transforms image to tensor and predicts disease label | ||
:params: image | ||
:return: prediction (string) | ||
""" | ||
transform = transforms.Compose([ | ||
transforms.Resize(256), | ||
transforms.ToTensor(), | ||
]) | ||
image = Image.open(io.BytesIO(img)) | ||
img_t = transform(image) | ||
img_u = torch.unsqueeze(img_t, 0) | ||
|
||
# Get predictions from model | ||
yb = model(img_u) | ||
# Pick index with highest probability | ||
_, preds = torch.max(yb, dim=1) | ||
prediction = disease_classes[preds[0].item()] | ||
# Retrieve the class label | ||
return prediction | ||
|
||
# =============================================================================================== | ||
# ------------------------------------ FLASK APP ------------------------------------------------- | ||
|
||
|
||
app = Flask(__name__) | ||
|
||
# render home page | ||
|
||
|
||
@ app.route('/') | ||
def home(): | ||
title = 'Farmers Friend - Home' | ||
return render_template('index.html', title=title) | ||
|
||
# render crop recommendation form page | ||
|
||
|
||
@ app.route('/crop-recommend') | ||
def crop_recommend(): | ||
title = 'Farmers Friend - Crop Recommendation' | ||
return render_template('crop.html', title=title) | ||
|
||
|
||
# =============================================================================================== | ||
|
||
# RENDER PREDICTION PAGES | ||
|
||
# render crop recommendation result page | ||
|
||
|
||
@ app.route('/crop-predict', methods=['POST']) | ||
def crop_prediction(): | ||
title = 'Farmers Friend- Crop Recommendation' | ||
|
||
if request.method == 'POST': | ||
N = int(request.form['nitrogen']) | ||
P = int(request.form['phosphorous']) | ||
K = int(request.form['pottasium']) | ||
temperature = float(request.form['temperature']) | ||
humidity = float(request.form['humidity']) | ||
ph = float(request.form['ph']) | ||
rainfall = float(request.form['rainfall']) | ||
|
||
data = np.array([[N, P, K, temperature, humidity, ph, rainfall]]) | ||
my_prediction = crop_recommendation_model.predict(data) | ||
final_prediction = my_prediction[0] | ||
|
||
return render_template('crop-result.html', prediction=final_prediction, title=title) | ||
|
||
|
||
# render disease prediction result page | ||
|
||
|
||
@app.route('/disease-predict', methods=['GET', 'POST']) | ||
def disease_prediction(): | ||
title = 'Farmers Friend - Disease Detection' | ||
|
||
if request.method == 'POST': | ||
if 'file' not in request.files: | ||
return redirect(request.url) | ||
file = request.files.get('file') | ||
if not file: | ||
return render_template('disease.html', title=title) | ||
try: | ||
img = file.read() | ||
|
||
prediction = predict_image(img) | ||
|
||
prediction = Markup(str(disease_dic[prediction])) | ||
return render_template('disease-result.html', prediction=prediction, title=title) | ||
except: | ||
pass | ||
return render_template('disease.html', title=title) | ||
|
||
|
||
# =============================================================================================== | ||
if __name__ == '__main__': | ||
app.run(debug=False) |
Oops, something went wrong.