YOLOv8 Cocoa Plant Detection + GIS AI Pipeline with CLI
Project description
๐ฑ Cocoa Plant Detection + GIS Pipeline with YOLOv8 + Streamlit
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
- Overview
- Project Structure
- Full Detection Pipeline
- QGIS Integration
- Streamlit Web Demo
- Command-Line Interface (CLI)
- Geo-Referencing Details
- Dataset Notes
- Model Performance
- Reproducibility
- Example Visuals
- License
- Maintainer
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
-
๐งฑ Tile Large Aerial Image
python cocoa_pipeline_core/tile_creator.pyInput: Image_4.tif
Output: tiles1/ (1024ร1024 px tiles with 22% overlap)
-
๐ง 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.ptfrom Google Drive if missing -
Predict on all
.tiftiles in the folder -
Save detection labels and visual outputs
Inference across all tiles
Confidence: conf=0.22, IoU: 0.73
Outputs YOLO .txt format
-
๐ Convert to GeoJSON/GPKG
python cocoa_pipeline_core/deduplicate_and_filter.pypython 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.
๐ Streamlit Web Demo
A live demo is available to test cocoa plant and planting hole detection directly in your browser:
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.
Predicted 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.
๐ก 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:
-
Tile image python cocoa_pipeline_core/tile_creator.py
-
Run inference python cocoa_pipeline_core/batch_infer_yolov8.py
-
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
Precision vs Confidence
๐ง Sample Predictions on Validation Tiles
Sample Prediction โ val_batch0
Sample Prediction โ val_batch1
๐งช 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13c00b95162ad9d1aa82799f72944ba20c9221821d11a64551de24ab9b87efdb
|
|
| MD5 |
8c0dba95860b84cc1694a62e007bb458
|
|
| BLAKE2b-256 |
c9457c3375a15c19becc002f130aaa0df579c26eef3171d9073bc1351e25ade7
|
File details
Details for the file cocoa_plant_detector-0.1.4-py3-none-any.whl.
File metadata
- Download URL: cocoa_plant_detector-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bdbe2e9eb59812ccd65be587d8b63e7373c15646ea549079c8a7d322e33a3aa
|
|
| MD5 |
f802672ce8ee87097ab94ec0cac89567
|
|
| BLAKE2b-256 |
01272a9f82460a2588de111d119d27911b56dd99d79de0c8b861a540d9d82e65
|