Image

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if you are a geek, or a person looking forward to understanding how deep learning can transform agriculture. In this project, we will guide you on how to design a crop disease detection using YOLOv8 system that is able to process images and videos to detect crop diseases in real-time.


Sounds interesting right? Let us get into it!

Project Overview

Here, you'll discover how to implement an enhanced crop disease identification model based on YOLOv8, which is among the currently leading object detection tools. We will be helping you with how you can install it on Google Colab and take advantage of cloud computing. We will then connect to Roboflow to manage datasets and display a clean and engaging interface with Gradio. The result of this work is going to be a system that can analyze crop images or videos and differentiate diseases. Suppose a farmer can take a picture of his crops and have a diagnosis of the problem in a matter of minutes -that is the future for today's farmer!


Key Feature

Finally, you will have an operational system that takes images or videos and outputs the diseases that are depicted in these images or videos. Farmers, agricultural professionals, or even researchers can employ it in techniques of crop health real time monitoring.


Prerequisites

Before we jump into the code, here's what you'll need:

  • Google Colab Account (Google account free and provide you with free and full GPU access)

  • Roboflow API Key (which is absolutely necessary if you are planning to manipulate the dataset).

  • A good understanding of the Python programming language and passion in Machine learning.

  • Knowledge in the use of YOLOv8 (Ultralytics), roboflow, gradio, numpy,matplotlib,opencv,PIL


Approach

We'll follow a structured approach to build this project:

  • Data Collection: We'll be using a dataset of crop images from Roboflow which will consist of images belonging both to diseased and healthy crops.
  • Model Training: To this dataset, we will fine-tune the YOLOv8 model to detect the crop diseases accurately.
  • Interactive Interface: After that, it will be connected to Gradio so that users can have a nice frontend to upload their own images or videos and see results immediately.

Workflow and Methodology

The overall workflow of this project includes:

  • Data collection: We will take a well-structured dataset from Roboflow. To improve the performance of the model, we applied some augmentation techniques in roboflow when downloading the dataset to increase the dataset size.
  • Data preprocessing: Following that, the data is imported and then resized to give the model more diversity.
  • Train YOLOv8: After that the model is trained through 100 epochs on dataset. The model learns how to recognize diseases like a professional!Test & Evaluate: After the training, we give new data to the model. By doing this the performance is checked and the model is optimized for better accuracy.
  • Build the Interface: Finally, we deploy a simple Gradio interface for the user where the user can upload an image or video and the system provides disease detection results.

The methodology involves:

  • Data Preprocessing: Preprocessing the collected images by resizing them. It resizes the required input dimensions. Then augmentation technique is applied. It enhances the diversity of training data and prevents overfitting.
  • Model Architecture: YOLOv8 is used for real-time object detection. It is trained to locate and classify diseases in crop images.
  • Evaluation: Testing the model with unseen crop disease images. Using (mAP) that evaluates the model's performance.

Data Collection

The first and foremost important aspect of this type of project is dataset collection. We are using Roboflow, where we are able to gather, annotate, and collect high quality datasets for object detection tasks like these. The data set that we utilize includes images of fields of the various crops in healthy as well as disease prone health conditions. Possessing such a diverse dataset allows our model to distinguish between healthy plants and the diseases that were previously mentioned.


Data Preparation

  • Cleaning the Data: First of all, low-quality and off-topic images can create confusion for the model. Those are filtered out. This is significant in order to eliminate the chances of the model learning from useless data.
  • Resizing Images: All images are then resized to YOLOv8 input size which is 640x640 through image resizing. This ensures that the model does not get easily over-fitted, thus enhancing the rate of learning in the network.
  • Data Augmentation: To alleviate the effect of overfitting we augment the data by rotating flipping images and also changing their brightness. This makes a more diverse dataset. This means the model can learn to generalize in an enhanced manner based on tests on new data.

Data Preparation Workflow

  • Downloading the dataset from Roboflow: The dataset is collected from Roboflow. It is a platform providing high-quality datasets for machine learning projects.

  • Initializes a Connection to Roboflow Using Your API Key: A connection is established to Roboflow using a unique API key. API keys grant access to the dataset repository and ensure secure data retrieval.

  • Access a Specific Project in the Workspace: The project contains a labeled dataset. It has been created for disease detection, It is named "crops-diseases-detection-and-classification."

  • YOLOv8 Configured Dataset: The 12 version of this dataset is downloaded. It is pre-configured and optimized for use with the YOLOv8 architecture.


Code Explanation

STEP 1:

Connecting Google Drive

You can mount your Google Drive in a Google Colab notebook with this piece of code. This makes it easy to view files saved in Google Drive. In Colab, you can change and analyze data. You can also train models.

from google.colab import drive
drive.mount('/content/drive')

Install the necessary packages

Install the roboflow, gradio, and ultralytics Python packages. roboflow is used for managing datasets, and ultralytics is used for working with YOLO models.

!pip install roboflow
!pip install ultralytics
!pip install gradio ultralytics

Downloading the Dataset from Roboflow for Crop Disease Detection

The Roboflow API is initialized using the API key. To download the "crops-diseases-detection-and-classification" dataset for YOLOv8.

from roboflow import Roboflow
rf = Roboflow(api_key="PYIrYvf8WFPOSaItVRYl")
project = rf.workspace("graduation-project-2023").project("crops-diseases-detection-and-classification")
version = project.version(12)
dataset = version.download("yolov8")

STEP 2:

Import the necessary packages

The necessary libraries such as OpenCV, matplotlib, and ultralytics are imported to handle image processing, visualization , and model training.

import ultralytics
ultralytics.checks()
from PIL import Image
from ultralytics import YOLO
import gradio as gr
import ultralytics
import numpy as np
from cv2 import imread
from matplotlib import pyplot as plt
from matplotlib.image import imread
import cv2
import gradio as gr

Choosing an AI Model

For this project, YOLOv8 (You Only Look Once) was chosen due to its exceptional ability. To perform real-time object detection with this model. YOLO models are known for their speed and accuracy in identifying objects within images, making them ideal for tasks such as detecting crop diseases.


Train YOLOv8 Model for Crop Disease Detection

The model is trained on the downloaded dataset using the YOLOv8 model. Parameters such as epochs, batch size, and image size are set.

!yolo train model=yolov8n.pt data="/content/drive/MyDrive/crops-Diseases-Detection-and-Classification/data.yaml" epochs=100 imgsz=640

Visualization of Training Result

This step is useful for visualizing and assessing the performance of your model.

Image.open(f'/content/drive/MyDrive/crop_disease_detection/runs/detect/train/confusion_matrix.png').resize((600,600))
Image.open(f'/content/drive/MyDrive/crop_disease_detection/runs/detect/train/results.png').resize((1000,500))
Image.open(f'/content/drive/MyDrive/crop_disease_detection/runs/detect/train/train_batch1.jpg').resize((1000,500))

After training, visualize the results such as confusion matrix, results summary, and sample training batches.



STEP 3:

Model Validation

  • Loads the best model (based on training) from the specified directory.
  • Runs validation on the model using the validation set and collects various metrics:
    • map: Mean Average Precision across IoU thresholds.
    • map50: Mean Average Precision at IoU threshold 0.50.
    • map75: Mean Average Precision at IoU threshold 0.75.
    • maps: List of per-class mAPs.
data_yaml_path = '/content/drive/MyDrive/crops-Diseases-Detection-and-Classification-12/data.yaml'
model = YOLO('/content/drive/MyDrive/crop_disease_detection/runs/detect/train/weights/best.pt')
metrics = model.val(data=data_yaml_path)
metrics.box.map
metrics.box.map50
metrics.box.map75
metrics.box.maps

Visualizing Validation Results

  • Opens and resizes images generated during validation, such as the confusion matrix and labeled batch images.
  • These images help you visualize how well the model is performing on the validation set.

Display a validation batch with labels to visualize the model's accuracy on unseen data.

Image.open(f'/content/drive/MyDrive/crop_disease_detection/runs/detect/val/confusion_matrix.png').resize((1000,500))
Image.open(f'/content/drive/MyDrive/crop_disease_detection/runs/detect/val/val_batch0_labels.jpg').resize((1000,500))

STEP 4:

Inference for Image Prediction

  • Loads a trained model and uses it to predict objects in a specific image.
  • Saves the prediction and then loads and displays the predicted image using matplotlib
model = YOLO('/content/drive/MyDrive/crop_disease_detection/runs//train/weights/best.pt')
results = model('/content/drive/MyDrive/crops-Diseases-Detection-and-Classification/test/images/022_jpg.rf.db03fecdb4b0082ef982753c6efe4528.jpg',show_conf=False,save=True)

Display Predicted Image

In this code, we load an image to predict. With our best model, we will predict in the image. It will show crop disease detection.

predicted_img_paths = [
    '/content/drive/MyDrive/crop_disease_detection/runs/detect/predict/Prediction_Image1.jpg',
    '/content/drive/MyDrive/crop_disease_detection/runs/detect/predict/Prediction_Image2.jpg.jpg'
]
fig, axes = plt.subplots(nrows=1, ncols=len(predicted_img_paths), figsize=(16, 8))
# Iterate through images and display them in subplots
for i, path in enumerate(predicted_img_paths):
    predicted_img = imread(path)
    axes[i].imshow(predicted_img)
    axes[i].axis('off')
plt.tight_layout()
plt.show()

STEP 5:

Loading the YOLOv8 Model

Loads the trained YOLOv8 model from the specified file path. This model will be used to detect objects in both images and videos.

model = YOLO('/content/drive/MyDrive/crop_disease_detection/runs/detect/train/weights/best.pt')

Set Up Gradio Interface for Crop Disease Detection

The `predict_images` function reads through a list of image file paths, loads the image, converts the image into the required format, and passes it through the model. It overlays rectangles bounding boxes and text to specify what objects it found in the images along with a label and score.

def predict_images(filepaths):
    if filepaths is None:
        return []
    output_images = []
    for filepath in filepaths:
        try:
            image = Image.open(filepath)
            img_np = np.array(image)
            img_np_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
            results = model(img_np_bgr)
            for result in results[0].boxes:
                x1, y1, x2, y2 = map(int, result.xyxy[0].cpu().numpy())
                label = int(result.cls.cpu().numpy())
                conf = float(result.conf.cpu().numpy())
                label_text = f"{model.names[label]}: {conf:.2f}"
                cv2.rectangle(img_np_bgr, (x1, y1), (x2, y2), (255, 0, 0), 2)
                cv2.putText(img_np_bgr, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
            img_np_rgb = cv2.cvtColor(img_np_bgr, cv2.COLOR_BGR2RGB)
            output_image = Image.fromarray(img_np_rgb)
            output_images.append(output_image)
        except Exception as e:
            print(f"Error: {e}")
            output_images.append(None)
    return output_images

The predict_videos function starts by taking a file path to the video as input. It opens the video using OpenCV, initializing a list to store processed frames for easier access and modification. To display the video results later, it sets up a video writer to save the processed output. Each frame in the video is read and then passed through a YOLO model for object detection, where bounding boxes and labels are drawn around the detected objects in each frame. These annotated frames are collected and saved to create a new video file, which provides a complete view of all detected objects across the video. Finally, the function outputs the path to this processed video file, ready for viewing or further analysis.

def predict_videos(filepath):
    if filepath is None:
        return None, None
    try:
        cap = cv2.VideoCapture(filepath)
        output_frames = []
        temp_input_video_path = '/content/input_video.mp4'
        cap_in = cv2.VideoWriter(temp_input_video_path, cv2.VideoWriter_fourcc(*'mp4v'), 20,
                                 (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
        if not cap.isOpened():
            print("Error: Could not open video.")
            return None, None
        frame_count = 0
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            cap_in.write(frame)
            results = model(frame)
            for result in results[0].boxes:
                x1, y1, x2, y2 = map(int, result.xyxy[0].cpu().numpy())
                label = int(result.cls.cpu().numpy())
                conf = float(result.conf.cpu().numpy())
                label_text = f"{model.names[label]}: {conf:.2f}"
                cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
                cv2.putText(frame, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
            output_frames.append(frame)
            frame_count += 1
        cap.release()
        cap_in.release()
        if frame_count == 0:
            print("Error: No frames processed.")
            return None, None
        height, width, _ = output_frames[0].shape
        temp_output_video_path = '/content/processed_video.mp4'
        out = cv2.VideoWriter(temp_output_video_path, cv2.VideoWriter_fourcc(*'mp4v'), 20, (width, height))
        for frame in output_frames:
            out.write(frame)
        out.release()
        print("Video processed and saved.")
        return temp_output_video_path
    except Exception as e:
        print(f"Error: {e}")
        return None

The process_files function streamlines object detection by combining the predict_images and predict_videos functions to handle both images and videos. It takes as input a list of image files along with a single video file and then calls the appropriate functions to process each type. This approach allows for seamless handling of both image and video formats, enabling object detection across diverse file types in a single operation.

def process_files(images, video):
    output_images = predict_images(images)
    processed_video = predict_videos(video) if video else None
    return output_images, video, processed_video

The Gradio interface setup provides a user-friendly way to interact with the model for object detection. Users can upload multiple image files and a video file as inputs using Gradio's gr.File for images and gr.Video for the video. Once processed, the interface displays a gallery of the annotated images alongside the original and processed video files, giving users a clear view of the detection results on both images and video in a single platform.

interface = gr.Interface(
    fn=process_files,
    inputs=[gr.File(file_count="multiple", type="filepath", label="Upload Images"),
            gr.Video(label="Upload Video")],
    outputs=[gr.Gallery(label="Output Images"), gr.Video(label="Input Video"), gr.Video(label="Processed Video")],
)

Launching the Interface

Launches the Gradio interface with debugging enabled, allowing users to interact with the system by uploading images and videos, and receiving processed outputs with detected objects.

interface.launch(debug=True)

This code provides a comprehensive solution for detecting objects in both images and videos using a trained YOLOv8 model, all within an easy-to-use web interface.


Project Conclusion

Here in this project, you came up with a strong crop disease detection system that can prevent crop loss and increase yields greatly. The YOLOv8 model was incredibly accurate and now, thanks to Gradio, it can be used by anyone with a phone or a computer. AI can definitely solve such issues and the scope of implementing it is very broad.


During this process, we have employed different areas of machine learning such as data augmentation and model evaluation to make the system more reliable. Our model can now help agricultural professionals by offering quick and accurate identification of crop disease. You're actively involved in a product that can protect crops, increase yield, and potentially solve world hunger problems!


Challenges and Troubleshooting

As cool as it is to build something like this, it of course doesn't always run like clockwork, but that's part of the fun!

Here are a few bumps you might hit:

  • Slow Training Time: According to the size of the data that is being used for training it may sometimes take a lot of time in the process. Google Colab has a powerful feature - you can activate the GPU to make things run faster!
  • Data Quality Issues: If your dataset is not properly labeled, your model will have problems with whether or not it will make correct predictions.
  • Gradio Crashes: This is often caused when the input files are large to work through in the computer program, or more than one input file is being processed. All of this is to say that just don't make your video or image excessively large.

FAQ

  1. What is the main goal of using the YOLOv8 model to identify crop diseases?

    • Answer: The study uses the YOLOv8 model to identify disease in crop images. This offers a precise and effective agricultural diagnostic.

  2. Why is data resizing important in crop disease detection?

    • Answer: By adding variances to the training sets, data resizing enhances the model. This improves the model's performance on unseen crop images and helps prevent overfitting.

  3. How does the YOLOv8 model perform in crop disease detection?

    • Answer: For real-time object detection, YOLOv8 is employed. It is quite effective in spotting diseases in crop pictures. It can analyze pictures quickly and accurately.

  4. Which particular difficulties can I face during the training?

    • Answer: It takes computing resources to get enough data. The GPU capabilities of Google Colab assisted in solving these problems.

  5. How did optimizing the learning rate improve performance on validation and test sets?

    • Answer: Implementing dynamic learning rate. Scheduling allowed the model to converge more effectively during training. This resulted in better performance on validation and test sets. By reducing overfitting and improving overall detection accuracy.


You can do this:

Want to build it yourself? One can download the code, go through the instructions on the site, and then modify the type of crop you prefer.


Spread the word! Spread this project across your network, let's change how we view crop health management.


The good news is that you can start using this great system and make a big difference in agriculture right now very easily and fast.

Code Editor