from flask import Flask, request, render_template_string import tensorflow as tf import numpy as np from PIL import Image import io
app = Flask(name)
model = tf.keras.models.load_model('crop_disease_model.h5')
class_labels = ['Healthy', 'Disease A', 'Disease B', 'Disease C', 'Disease D']
html_form = '''
<title>Crop Disease Detection</title> Predict '''@app.route('/') def home(): return render_template_string(html_form)
@app.route('/predict', methods=['POST']) def predict(): if 'file' not in request.files: return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
image = Image.open(file)
image = image.resize((150, 150)) # Resize the image to match the model input size
image = np.array(image) / 255.0 # Normalize the image
image = np.expand_dims(image, axis=0) # Add batch dimension
prediction = model.predict(image)
predicted_class = class_labels[np.argmax(prediction)] # Get the predicted class
return f"Predicted Disease: {predicted_class}"
if name == 'main': app.run(debug=True)