|
from flask import Flask, render_template, request |
|
import numpy as np |
|
import pandas as pd |
|
import pickle |
|
import os |
|
from config import IMAGE_BASE_URL |
|
from image_utils import get_base64_images |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
IMAGES = get_base64_images() |
|
|
|
|
|
@app.context_processor |
|
def utility_processor(): |
|
def get_image_url(path): |
|
return IMAGE_BASE_URL + path |
|
return dict(get_image_url=get_image_url, images=IMAGES) |
|
|
|
|
|
try: |
|
|
|
with open('model.pkl', 'rb') as f: |
|
model = pickle.load(f) |
|
with open('standscaler.pkl', 'rb') as f: |
|
sc = pickle.load(f) |
|
with open('minmaxscaler.pkl', 'rb') as f: |
|
ms = pickle.load(f) |
|
print("Models loaded successfully") |
|
except Exception as e: |
|
print(f"Error loading models: {e}") |
|
try: |
|
|
|
data = pd.read_csv('Crop_recommendation.csv') |
|
features = ['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall'] |
|
X = data[features].values |
|
y = data['label'].values |
|
|
|
|
|
from sklearn.preprocessing import StandardScaler, MinMaxScaler |
|
ms = MinMaxScaler() |
|
ms.fit(X) |
|
|
|
|
|
X_minmax = ms.transform(X) |
|
|
|
sc = StandardScaler() |
|
sc.fit(X_minmax) |
|
|
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
model = RandomForestClassifier(n_estimators=100, random_state=42) |
|
model.fit(sc.transform(X_minmax), y) |
|
print("Models fitted successfully with dataset") |
|
|
|
|
|
with open('model.pkl', 'wb') as f: |
|
pickle.dump(model, f) |
|
with open('standscaler.pkl', 'wb') as f: |
|
pickle.dump(sc, f) |
|
with open('minmaxscaler.pkl', 'wb') as f: |
|
pickle.dump(ms, f) |
|
|
|
except Exception as e: |
|
print(f"Error fitting models: {e}") |
|
|
|
sample_data = np.array([[90, 40, 40, 20, 80, 7, 200], |
|
[20, 30, 10, 25, 60, 6, 100]]) |
|
|
|
ms = MinMaxScaler() |
|
ms.fit(sample_data) |
|
|
|
sc = StandardScaler() |
|
sc.fit(sample_data) |
|
|
|
model = RandomForestClassifier() |
|
model.fit(sample_data, [1, 2]) |
|
print("Using default models") |
|
|
|
|
|
@app.route('/') |
|
def home(): |
|
return render_template('home.html') |
|
|
|
@app.route('/info') |
|
def info(): |
|
return render_template('info.html') |
|
|
|
@app.route('/recommendation', methods=['GET', 'POST']) |
|
def recommendation(): |
|
result = None |
|
N = P = K = temp = humidity = ph = rainfall = None |
|
|
|
if request.method == 'POST': |
|
try: |
|
|
|
N = float(request.form['Nitrogen']) |
|
P = float(request.form['Phosporus']) |
|
K = float(request.form['Potassium']) |
|
temp = float(request.form['Temperature']) |
|
humidity = float(request.form['Humidity']) |
|
ph = float(request.form['Ph']) |
|
rainfall = float(request.form['Rainfall']) |
|
|
|
|
|
feature_list = [N, P, K, temp, humidity, ph, rainfall] |
|
single_pred = np.array(feature_list).reshape(1, -1) |
|
|
|
|
|
try: |
|
scaled_features = ms.transform(single_pred) |
|
final_features = sc.transform(scaled_features) |
|
|
|
|
|
prediction = model.predict(final_features) |
|
|
|
|
|
crop_dict = {1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange", |
|
8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana", |
|
14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans", |
|
19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"} |
|
|
|
if prediction[0] in crop_dict: |
|
crop = crop_dict[prediction[0]] |
|
result = "{} is the best crop to be cultivated right there".format(crop) |
|
else: |
|
result = "Sorry, we could not determine the best crop to be cultivated with the provided data." |
|
|
|
except Exception as e: |
|
print(f"Error in prediction: {e}") |
|
result = "An error occurred during prediction. Please try again." |
|
|
|
except Exception as e: |
|
print(f"Error processing form data: {e}") |
|
result = f"An error occurred: {str(e)}" |
|
|
|
return render_template('recommendation.html', result=result, |
|
N=N, P=P, K=K, temp=temp, |
|
humidity=humidity, ph=ph, rainfall=rainfall) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860) |
|
|