ππ Dive into the cutting-edge realm of Apple Leaf Disease Detection using Python and Machine Learning! πΏπ» Unlock the mysteries behind accurate disease identification with our step-by-step guide, Safeguard your apple orchards. Subscribe now for in-depth coding revelations and to stay ahead in the realm of agricultural technology! ππ±
Introduction
In the ever-evolving landscape of agriculture, technology plays a pivotal role in enhancing crop management practices...
The Problem: Apple Tree Diseases
Apple trees are susceptible to various diseases, including Apple Scab, Black Rot, and Cedar Apple Rust, which can severely impact crop yield and quality. Timely detection and management of these diseases are critical for maintaining a healthy orchard and ensuring a successful harvest.
Tools and Technologies
Python
PyTorch
Flask
PIL (Pillow)
Building the Model
- Importing Libraries
Importing the required libraries
import io
import string
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision import models
from flask import Flask, jsonify, request, render_template
from PIL import Image
- Loading and Configuring the Model
Creating a Flask application instance
app = Flask(name)
Loading the pre-trained Googlenet model
model = models.googlenet()
num_inftr = model.fc.in_features
model.fc = nn.Linear(num_inftr, 4)
model.load_state_dict(torch.load('t_googlenet.pth'), strict=False)
model.eval()
Disease labels
label = ['Apple Scab', 'Black Rot', 'Cedar Apple Rust', 'Healthy']
- Image Transformation Function
Image transformation function
def transform_image(img):
my_transforms = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image = Image.open(io.BytesIO(img))
return my_transforms(image).unsqueeze(0)
- Prediction Function
Prediction function
def get_prediction(img):
tensor = transform_image(img=img)
outputs = model.forward(tensor)
_, prediction = torch.max(outputs, 1)
return label[prediction]
- Disease Information
Disease information
diseases = {
"Healthy": "Your Plant is Healthy",
"Apple Scab": "Apple Scab, a condition affecting apple trees, is induced by the fungus Venturia inaequalis...",
"Cedar Apple Rust": "A gall-producing disease especially of the apple caused by a rust fungus...",
"Black Rot": "Black rot is a fungus disease that can cause serious losses in apple orchards...",
}
Web Application Development with Flask
- Web Application Structure
Web application structure
@app.route('/', methods=['GET', 'POST'])
def upload_file():
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('index.html')
img_bytes = file.read()
prediction_name = get_prediction(img_bytes)
return render_template('result.html', name=prediction_name.lower(), description=diseases[prediction_name])
return render_template('index.html')
if name == 'main':
app.run(debug=True)
Conclusion
In this blog post, we've explored the process of building an Apple Leaf Disease Detection system using Python, Machine Learning, and Flask...
Top comments (0)