This project isn't just an object detector; it's a complete, self-improving system. It uses YOLOv8 to identify pests and a Streamlit web app to create a Human-in-the-Loop (HITL) feedback cycle.
When the model makes a low-confidence prediction, a human (the "farmer") provides the correct label. This feedback is then used to automatically retrain and improve the model, creating a system that learns from its own mistakes.
This is the core value of the project: the model gets smarter with user feedback.
- Model v1 (Initial Guess): Identifies a pest with
74% confidence. - Human Feedback: The user confirms or corrects the label.
- Retraining: The user clicks "Retrain," and a
v2model is automatically trained in the background using this new, verified data. - Model v2 (After Correction): The same image is now identified
with
91% confidence.
This entire project is designed to be run from a single Google Colab notebook.
Your repository should be organized in Google Drive like this:
/PestDetector/
├── app.py # The Streamlit web app
├── pest_data.yaml # YOLO dataset configuration file
├── production_model.pt # The starting "v1" model (you must create this)
├── datasets/ # Folder for your images and labels
│ ├── images/
│ │ ├── train/
│ │ └── val/
│ ├── labels/
│ │ ├── train/
│ │ └── val/
└── Pest_Identifier_Colab.ipynb # Main Colab notebook
Before you can run the app, you need a starting "v1" model.
Mount your Google Drive and install the YOLOv8 library.
from google.colab import drive
drive.mount('/content/drive')
!pip install ultralytics
# Navigate to your project directory
%cd /content/drive/MyDrive/PestDetector/This project requires a dataset in YOLO format.
-
Get Data: You can find a "pest" dataset on Roboflow or Kaggle.
-
Organize: Place your images and labels in the
datasets/folder as shown in the project structure. -
Configure YAML: Create a
pest_data.yamlfile that points to your data. It must contain:- The absolute path to your
datasetsfolder in Google Drive. - The relative paths to your
trainandvalfolders. - The number of classes (
nc) and their names.
Example
pest_data.yaml:path: /content/drive/MyDrive/PestDetector/datasets train: images/train val: images/val nc: 3 names: - 'aphids' - 'fruit_flies' - 'stink_bugs'
- The absolute path to your
Run the YOLOv8 training command.
!yolo train model=yolov8n.pt data=pest_data.yaml epochs=50 imgsz=640After training:
- Find
runs/detect/train/weights/best.pt - Copy it to
/content/drive/MyDrive/PestDetector/ - Rename it to
production_model.pt
!pip install streamlit ultralytics pyngrok%%writefile app.py
# Paste your full app.py code here!ngrok config add-authtoken YOUR_PRIVATE_TOKEN_HEREfrom pyngrok import ngrok
public_url = ngrok.connect(8501)
print(public_url)
!streamlit run app.py- Upload an image → model predicts.
- Give feedback if prediction is wrong.
- Click "Retrain & Activate Model" to generate a smarter model.
- Re-upload the same image to see confidence improve.