SporaScan: An Automated Pipeline for Precise Disc-Based Disease Severity Assessment in Grapevine Downy Mildew
This repository provides a YOLO + Mobile SAM + U-Net pipeline for grapevine leaf disc disease severity estimation. The batch CLI processes a folder of plate photos and writes one annotated JPEG plus one CSV per image.
Use Python 3.10, 3.11, or 3.12. The supplied PyTorch 2.5.1 command is not available for Python 3.13.
Create and activate a fresh Python environment:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pipInstall PyTorch first. Choose the command that matches your machine from https://pytorch.org/get-started/locally/. For a CPU-only Windows environment:
pip install torch==2.5.1 torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cpuThen install the pipeline dependencies:
pip install -r requirements.txtsegmentation-models-pytorch==0.5.0 is required for the supplied
weights/UNet_best.pth checkpoint. Loading that checkpoint with a different
major model-library version can fail or silently change model construction.
The default CLI paths expect:
weights/yolon_best.pt
weights/mobile_sam.pt
weights/UNet_best.pth
Pass custom paths with --yolo-weights, --sam-weights, and --unet-weights
if your files are stored elsewhere.
Run the CLI on a folder of plate photos:
python grapevine_leaf_disc_severity_cli.py path\to\plate_photos --output-dir severity_outputsFor every supported image file (.jpg, .jpeg, .png, .bmp, .tif,
.tiff), the CLI writes:
severity_outputs/<image_stem>_annotated.jpg
severity_outputs/<image_stem>.csv
Each CSV contains one row per detected disc:
disk_id,bbox,leaf_pixels,diseased_pixels,severity_percent
bbox is written as x,y,w,h in source-image pixel coordinates. Pixel counts
are computed from the U-Net mask at 224 x 224 resolution, and
severity_percent is:
diseased_pixels / (diseased_pixels + leaf_pixels) * 100
The CLI does not call cv2.imshow or cv2.waitKey, so it can run unattended on
folders of images.
Run the lightweight test suite:
python -m unittest discover -s testsThese tests cover CLI path handling and CSV output without requiring OpenCV, PyTorch, Ultralytics, or the model weights. Full inference testing requires the environment and model files above.
grapevine_leaf_disc_severity.py still exposes LeafDownSegPipe for direct
Python use. Its display-based get_results() method preserves the original
interactive behavior and opens an OpenCV window when severity=True.

