# SMPAS (Shiitake Mushroom Pattern Analysis System)

SMPAS is an AprilTag-calibrated analysis system for dried shiitake mushroom caps. It measures real-world diameter, estimates crack/pattern ratio, and classifies pattern type using a YOLO detector and SAM-based segmentation. It ships with a web UI for image upload and visualization.

**Key Features**
- AprilTag-based calibration for real-world measurements
- Diameter measurement (mm)
- Crack/pattern ratio estimation
- Pattern type classification
- Web app for interactive upload and results
- CLI tools for model download and web startup

**System Requirements**
- Python 3.10+
- CUDA GPU recommended for speed (CPU is supported)
- Network access to download models

**Install**

```bash
pip install smpas
```

SAM must be installed separately (PyPI does not allow direct Git dependencies):

```bash
pip install git+https://github.com/facebookresearch/segment-anything.git
```

**Download Models**

```bash
smpas-download-models
```

This downloads:
- `yolo_mushroom.pt` (default URL hosted by you)
- `sam_vit_b.pth` (official SAM checkpoint)

To override URLs:

```bash
smpas-download-models --yolo-url <URL> --sam-url <URL>
```

To download only one model:

```bash
smpas-download-models --no-yolo
smpas-download-models --no-sam
```

**Run the Web App**

```bash
smpas-web
```

Default address:
```
http://localhost:5000
```

**Default Directories**
SMPAS creates these under `~/.smpas` automatically:

```
~/.smpas/
├── models/      # model weights
├── data/        # optional input images
├── uploads/     # web uploads
└── outputs/     # optional outputs
```

**Environment Variables**
- `SMPAS_HOME`: base directory (default `~/.smpas`)
- `SMPAS_MODELS_DIR`: model directory override
- `SMPAS_DATA_DIR`: data directory override
- `SMPAS_UPLOAD_DIR`: upload directory override
- `SMPAS_DEVICE`: `cuda` or `cpu`
- `SMPAS_YOLO_URL`: default YOLO download URL for `smpas-download-models`
- `SMPAS_SAM_URL`: default SAM download URL for `smpas-download-models`
- `SMPAS_YOLO_MODEL`: explicit YOLO model path
- `SMPAS_SAM_MODEL`: explicit SAM model path
- `SMPAS_APRILTAG_SIZE`: AprilTag size in mm (default `37.58`)

**CLI Options (smpas-web)**
- `--host`, `--port`: bind address
- `--base-dir`: base directory (overrides `SMPAS_HOME`)
- `--models-dir`: model directory
- `--data-dir`: data directory
- `--uploads-dir`: upload directory
- `--device`: `cuda` or `cpu`
- `--apriltag-size`: AprilTag size in mm
- `--max-workers`: parallel workers
- `--no-parallel`: disable parallel processing

**Data Requirements**
- Images should include an AprilTag (tag16h5, ID 25)
- AprilTag black square size: `37.58 mm`
- Prefer top-down images with minimal perspective distortion
- Recommended resolution >= 2000×2000

**Output (Web Response)**
The web API returns:
- AprilTag detection status
- px/mm calibration
- Diameter (mm)
- Crack/pattern ratio (%)
- Pattern type classification
- Flatness score and suspected “ban-gu” flag

**Notes**
- Model weights are not bundled with the package.
- SAM must be installed manually from GitHub.
- GPU is optional but strongly recommended.

**License**
Proprietary. See `LICENSE.txt`.
