import os import cv2 import numpy as np
Data organization
dataset_root = ‘path_to_dataset_root_directory’ categories = [‘healthy’, ‘diseased’]
for category in categories: category_dir = os.path.join(dataset_root, category) images = os.listdir(category_dir) for image_name in images: image_path = os.path.join(category_dir, image_name) # Perform further processing on each image
Preprocessing and disease detection
def preprocess_and_detect_disease(image_path): # Load the image image = cv2.imread(image_path)
# Preprocess the image gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) # Apply image processing techniques (e.g., thresholding) _, thresholded_image = cv2.threshold(blurred_image, 100, 255, cv2.THRESH_BINARY) # Find contours in the image contours, _ = cv2.findContours(thresholded_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Loop over the contours and detect fruit disease for contour in contours: # Calculate the area of the contour area = cv2.contourArea(contour) # Set a threshold for disease detection threshold_area = 5000 if area > threshold_area: # Draw a bounding box around the detected fruit x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) # Display the output image cv2.imshow(‘Fruit Disease Detection’, image) cv2.waitKey(0) cv2.destroyAllWindows()
Example usage
image_path = ‘path_to_your_image.jpg’ preprocess_and_detect_disease(image_path)
submitted by /u/No-Bad-5051
[link] [comments]