Skip to main content

YOLOv8 Cocoa Plant Detection + GIS AI Pipeline with CLI

Project description

Cocoa AI Detector Banner

๐ŸŒฑ Cocoa Plant Detection + GIS Pipeline with YOLOv8 + Streamlit

repo-banner YOLOv8 QGIS License

PyPI version Live Demo GitHub Repo License: MIT

An end-to-end YOLOv8 pipeline to detect cocoa-plants + missed-planting-holes from aerial imagery. Output is QGIS-ready, and available via both a Python CLI and a Streamlit demo.

๐Ÿš€ Quick Install

pip install cocoa-plant-detector

Table of Contents


Demo Notebook

๐Ÿ“Œ Note: GitHub may truncate full notebook preview due to image-heavy output โ€” download or open locally for complete view.


Overview

This repository delivers a production-ready AI pipeline for detecting cocoa plants and planting holes from high-resolution aerial imagery using YOLOv8 and QGIS.

It includes:

  • ๐Ÿงฉ Tiling of large orthomosaic images
  • ๐Ÿง  Batch object detection on tiles
  • ๐Ÿงญ Spatial deduplication and coordinate conversion
  • ๐Ÿ—‚๏ธ GeoJSON/GPKG export for GIS tools

๐Ÿ“ฆ Features

๐Ÿค– YOLOv8 deep learning for cocoa plant & hole detection

๐ŸŒ GeoJSON/GPKG export with spatial deduplication

๐Ÿ–ฅ๏ธ Easy CLI access: cocoa-cli

๐ŸŒ Streamlit App: Upload + detect live

๐Ÿ›ฐ๏ธ Built for drone/orthomosaic imagery

โœ… Packaged & published on PyPI


๐Ÿงช Installation

Clone the repo and install dependencies:

git clone https://github.com/NiiOsa1/cocoa-plant-detector.git
cd cocoa-plant-detector
pip install -r requirements.txt

๐Ÿง  Model Auto-Download

The trained YOLOv8s model will be downloaded automatically when you run the CLI or Streamlit app โ€” no manual setup needed.

๐Ÿ”— Backup link (optional): Download from Google Drive


Project Structure

cocoa-plant-detector/
โ”œโ”€โ”€ cocoa_cli_pipeline/         # ๐Ÿ”ง CLI entrypoints (tile, infer, geoconvert)
โ”‚   โ”œโ”€โ”€ cli.py                  # โ”œโ”€โ”€ CLI command dispatcher
โ”‚   โ”œโ”€โ”€ infer.py                # โ”œโ”€โ”€ CLI: batch inference
โ”‚   โ”œโ”€โ”€ geoconvert.py           # โ”œโ”€โ”€ CLI: label โ†’ GeoJSON conversion
โ”‚   โ””โ”€โ”€ tiler.py                # โ””โ”€โ”€ CLI: tile large .tif orthomosaics
โ”œโ”€โ”€ cocoa_pipeline_core/        # ๐Ÿง  Core reusable logic (non-CLI)
โ”‚   โ”œโ”€โ”€ tile_creator.py         # โ”œโ”€โ”€ Manual tile generator (1024px w/ overlap)
โ”‚   โ”œโ”€โ”€ batch_infer_yolov8.py   # โ”œโ”€โ”€ Scripted batch inference (auto-downloads model)
โ”‚   โ”œโ”€โ”€ deduplicate_and_filter.py  # โ”œโ”€โ”€ Deduplicate + convert YOLO txt to GeoJSON
โ”‚   โ””โ”€โ”€ utils.py                # โ””โ”€โ”€ Utility functions (e.g., download model)
โ”œโ”€โ”€ streamlit_app/              # ๐ŸŒ Web UI (live tile prediction)
โ”‚   โ””โ”€โ”€ app.py
โ”œโ”€โ”€ notebooks/                  # ๐Ÿ““ Training + inference walkthrough
โ”‚   โ””โ”€โ”€ CocoaPlant_YOLOv8_Training_QGIS_Deployment.ipynb
โ”œโ”€โ”€ examples/                   # ๐Ÿ“ธ Visual results: metrics, outputs, overlays
โ”‚   โ”œโ”€โ”€ val_batch0_pred.jpg     # โ”œโ”€โ”€ Predicted validation samples
โ”‚   โ”œโ”€โ”€ StreamlitApp.png        # โ””โ”€โ”€ UI + overlay visualizations
โ”œโ”€โ”€ test_data/                  # ๐Ÿงช Sample tiles + labels for quick testing
โ”‚   โ””โ”€โ”€ tile_31200_22400.tif
โ”œโ”€โ”€ runs/                       # ๐Ÿ“‚ Outputs from inference (auto-created, .gitignored)
โ”œโ”€โ”€ requirements.txt            # ๐Ÿ“ฆ Dependencies for pip install
โ”œโ”€โ”€ setup.py                    # ๐Ÿ“ฆ Enables pip install . and CLI registration
โ”œโ”€โ”€ .gitignore                  # ๐Ÿงผ Cleans output, model weights, build artifacts
โ””โ”€โ”€ README.md                   # ๐Ÿ“– You're looking at it

Full Detection Pipeline

  1. ๐Ÿงฑ Tile Large Aerial Image

    python cocoa_pipeline_core/tile_creator.py
    

    Input: Image_4.tif

    Output: tiles1/ (1024ร—1024 px tiles with 22% overlap)

  2. ๐Ÿง  YOLOv8 Batch Inference

    You can now run the inference script with your own tile folder like this:

    python cocoa_pipeline_core/batch_infer_yolov8.py \
     --tiles test_data/ \
     --output runs/test_output
    

๐Ÿ” The script will automatically:

  • Download best.pt from Google Drive if missing

  • Predict on all .tif tiles in the folder

  • Save detection labels and visual outputs

    Inference across all tiles

    Confidence: conf=0.22, IoU: 0.73

    Outputs YOLO .txt format

  1. ๐ŸŒ Convert to GeoJSON/GPKG

    python cocoa_pipeline_core/deduplicate_and_filter.py
    

    python deduplicate_and_filter_final.py

    Converts to map-based UTM coordinates

    Deduplicates detections from overlapping tiles

    Saves:

    qgis_ready6_detections.geojson

QGIS Integration

This repo includes a ready-to-open QGIS project:

๐Ÿ“„ Project_Cocoa.qgz

Includes:

Image_4.tif as the basemap

Pre-styled detection layer (holes + cocoa plants)

Correct CRS, colors, symbology

๐Ÿš€ To use: Open QGIS (v3.28+ recommended)

Load Project_Cocoa.qgz

All layers and styles will auto-load ๐ŸŽฏ

๐Ÿ—บ๏ธ GIS Integration Output

Final Detection Overlay in QGIS Bounding boxes converted to UTM, deduplicated, and displayed on the original orthomosaic.

QGIS Overlay

๐Ÿš€ Streamlit Web Demo

A live demo is available to test cocoa plant and planting hole detection directly in your browser:

๐Ÿ‘‰ Launch the Streamlit App

You can upload .tif, .jpg, or .png tiles and get YOLOv8-predicted outputs in real time โ€” powered by the same model used in the pipeline.

๐Ÿš€ Streamlit Demo

Live Tile Prediction (via Streamlit) This tile was uploaded through the Streamlit UI and predicted live using the trained model.

Streamlit App

Predicted Detection Output

Detection Output

Command-Line Interface (CLI)

The full pipeline is also usable via a single terminal command. After installing the package with:

pip install .

You get access to:

cocoa-cli --help

Available Subcommands:

Command	      Description
tile	      Tile large aerial image
infer	      Run YOLOv8 batch inference
geoconvert    Convert YOLO labels to deduplicated GeoJSON

Example Usage:

 1. Tile large .tif image
cocoa-cli tile --input path/to/Image_4.tif --output tiles/

 2. Run batch inference
cocoa-cli infer --tiles tiles/ --output predictions/

 3. Convert YOLO txt โ†’ GeoJSON (deduplicated, GIS-ready)
cocoa-cli geoconvert --input predictions/ --output output.geojson

โœ… CLI Prediction in Action Here's a sample CLI batch inference run, showing per-tile predictions, detection speeds, and saved outputs.

CLI Results

๐Ÿ’ก The trained model is automatically downloaded if missing. You do not need to manually specify --weights unless using a custom model.

Geo-Referencing Details

These values were extracted from the original orthomosaic (Image_4.tif) and used to convert pixel detections into map-based coordinates (UTM):

Origin X      = 525765.6597     โ† Upper-left X in UTM meters
Origin Y      = 8702143.3199    โ† Upper-left Y in UTM meters
Pixel Width   = 0.01298099167   โ† ~1.3 cm per pixel
Pixel Height  = -0.01298099167  โ† Negative = North-up

๐Ÿ“Œ Important: These values are dataset-specific. Always extract your raster metadata when working with a different image.

Dataset Notes

Total training images: 1500

1089 expertly labeled

411 cleaned pseudo-labeled

Classes:

0 = cocoa-plant

1 = hole

Model Performance

| Set        | mAP@0.5 | Precision | Recall | F1 Score |
|------------|---------|-----------|--------|----------|
| Train      | 0.907   | 0.861     | 0.876  | 0.868    |
| Validation | 0.844   | 0.846     | 0.797  | 0.821    |
| Test       | 0.779   | 0.835     | 0.738  | 0.784    |

โœ… Model: YOLOv8s (22.6MB)

โšก Fast inference: ~16ms per image

๐ŸŽฏ Balanced detection for both classes

๐Ÿ”’ No signs of overfitting despite small dataset

Reproducibility

Run the pipeline locally:

  1. Tile image python cocoa_pipeline_core/tile_creator.py

  2. Run inference python cocoa_pipeline_core/batch_infer_yolov8.py

  3. Convert & deduplicate python cocoa_pipeline_core/deduplicate_and_filter.py

Optional: Streamlit UI streamlit run streamlit_app/app.py


Example Visuals

Hereโ€™s a quick walkthrough of the pipeline in action:

A few visual highlights from model performance and predictions:

๐Ÿ“Š Training Metrics & Model Behavior

F1 Score vs Confidence F1 Curve

Precision vs Confidence Precision Curve


๐Ÿง  Sample Predictions on Validation Tiles

Sample Prediction โ€“ val_batch0 Prediction 0

Sample Prediction โ€“ val_batch1 Prediction 1


๐Ÿงช Try It Yourself

Want to test the pipeline locally with included sample tiles?

streamlit run streamlit_app/app.py

Upload from test_data/ and see the same prediction pipeline running on your machine.

License

This repository is licensed under the MIT License. Youโ€™re free to use, modify, and distribute โ€” with credit to the author.

Maintainer

Michael Mensah Ofeor ๐Ÿ”— GitHub โ€“ @NiiOsa1 ๐Ÿ“ฉ michaelofeor2011@yahoo.com

โšก Built for real-world cocoa analysis. Powered by geospatial logic, YOLOv8, and a deep love for clean AI pipelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cocoa_plant_detector-0.1.4.tar.gz (876.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cocoa_plant_detector-0.1.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file cocoa_plant_detector-0.1.4.tar.gz.

File metadata

  • Download URL: cocoa_plant_detector-0.1.4.tar.gz
  • Upload date:
  • Size: 876.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for cocoa_plant_detector-0.1.4.tar.gz
Algorithm Hash digest
SHA256 13c00b95162ad9d1aa82799f72944ba20c9221821d11a64551de24ab9b87efdb
MD5 8c0dba95860b84cc1694a62e007bb458
BLAKE2b-256 c9457c3375a15c19becc002f130aaa0df579c26eef3171d9073bc1351e25ade7

See more details on using hashes here.

File details

Details for the file cocoa_plant_detector-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for cocoa_plant_detector-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3bdbe2e9eb59812ccd65be587d8b63e7373c15646ea549079c8a7d322e33a3aa
MD5 f802672ce8ee87097ab94ec0cac89567
BLAKE2b-256 01272a9f82460a2588de111d119d27911b56dd99d79de0c8b861a540d9d82e65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page