Transcript: This session will demonstrate how to detect plant leaf disease using Google Teachable Machine. To begin, a suitable dataset is required for training the model. This project uses the "New Plant Disease Data Set (Augmented)" available on Kaggle.com, which is approximately 3GB. After downloading and extracting the archive, you will find 'train' and 'valid' folders containing images for various plant categories, such as 'Apple_healthy', 'Apple_scab', and different tomato diseases.
The next step involves training the model using the Google Teachable Machine web interface. First, navigate to the Teachable Machine website, get started, and select a new "Image Project" using the "Standard Image Model." On the training page, you will create separate classes for the specific diseases you want to detect; this example uses 'Tomato_healthy', 'Tomato_virus', 'Apple_healthy', and 'Apple_cider_apple_rust'. For each class, click "Upload" and drag and drop the corresponding folder of images, then copy the folder name to use as the class label.
Once all the image classes are uploaded, you can begin the training process. Click the "Train Model" button and wait for the training to complete, ensuring you do not switch browser tabs, as this can interrupt the process. After training finishes, click "Export Model," select the "TensorFlow" option, and then click "Download my model" to save the model files.
While the model is downloading, you must set up the local project environment. Download the zip file from the provided "teachable-machine" code repository. Move both the downloaded repository zip and the downloaded model zip file into your main data set folder. Extract both archives; the repository extraction provides the Python script, and the model extraction provides the Keras model.H5 and labels.txt files.
The included Python script uses the CVZone library to run predictions. Before running the script, you must install the cvzone package using your Python ID's package manager. The script imports the Classifier from cvzone, along with cv2 and globe. It initializes the classifier by loading the Keras model.H5 and labels.txt files. A for loop, combined with the globe module, is used to read all images from a specified test folder path. The classifier.getPrediction function is then called on each image to generate a prediction.
Finally, you can run the script to test the model on new images. Update the path variable in the script to point to your folder of test images. When you run the code, it will open a window for each test image and display the predicted class. The demonstration shows the model correctly identifying 'Apple healthy' leaves, 'Tomato virus' leaves, 'Apple cider apple rust' (a diseased leaf), and 'Tomato healthy' leaves, confirming that the model was successfully trained using this simple method.
from cvzone.ClassificationModule import Classifier
import cv2
import glob
cs=Classifier('keras_model.h5','labels.txt')
path = r"C:\Users\freed\Downloads\archive\test\test/*.*"
for file in glob.glob(path):
img = cv2.imread(file)
img=cv2.resize(img, (1020, 500))
prediction = cs.getPrediction(img)
cv2.imshow("IMG",img)
cv2.waitKey(0)
cv2.destroyAllWindows
Google Colaboratory
Leaf identification
You can identify plant diseases using Teachable Machine by creating an image classification model with a dataset of healthy and diseased plant images. After training the model on the platform, you can export it for use in other applications, such as a Python script or a mobile app, to classify new images of plant leaves in real-time. This automated process helps in early disease detection, which is more efficient and potentially more accurate than traditional manual methods.