Download README.md from Nicholas0205/opened-manhole-classifier: direct link, hf CLI and curl.
- Browser
- Download file 6.96 kB
-
https://huggingface.co/spaces/Nicholas0205/opened-manhole-classifier/resolve/main/README.md
- Command line
-
hf download hf://spaces/Nicholas0205/opened-manhole-classifier/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Nicholas0205/opened-manhole-classifier/resolve/main/README.md
title: Opened Manhole Classifier
sdk: docker
app_port: 7860
Opened Manhole Classifier (Inference-Only)
This repository is a production-ready, inference-only computer vision classifier. It detects open, damaged, or closed manholes from images and overlays localized bounding boxes using Weakly Supervised Object Localization (WSOL) via Grad-CAM.
๐ Live Demo & Showcase
Try the model live in your browser: Live Demo on Hugging Face Spaces
Visual Interface Showcase
Here is a look at the web interface in action:
| 1. Landing & File Select | 2. Inference Result (Safe / Closed) | 3. Grad-CAM Localisation (Unsafe / Open) |
|---|---|---|
![]() |
![]() |
![]() |
Features
It serves a fine-tuned EfficientNet-B0 model with:
POST /predictJSON inference APIGET /visual browser inference showcase UI- Optional WSOL output (Grad-CAM bounding box)
Label Mapping
Binary output mapping is locked as:
- positive class (
sigmoid > threshold):safe(closed manhole) - negative class:
dangerous(open/damaged)
Runtime Components
app/model.py: model architecture + checkpoint loading (b0_v2.pth)app/preprocess.py: notebook-matching preprocessing (224x224, normalize mean/std0.5)app/inference.py: prediction logic + optional Grad-CAM WSOL bboxapp/main.py: FastAPI app (/,/health,/predict)app/static/: frontend assets (index.html,styles.css,app.js)
Legacy notebooks are preserved at notebooks/legacy/ and are excluded from runtime flow.
Local Run
1. Install dependencies
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
2. Start API server
Option A: Mock mode (fastest, no real inference)
Use this to verify UI/API behavior on any laptop:
USE_MOCK_PREDICTOR=1 uvicorn app.main:app --host 0.0.0.0 --port 7860
Option B: Real model on CPU
uvicorn app.main:app --host 0.0.0.0 --port 7860
Then open:
- UI:
http://localhost:7860/ - Health:
http://localhost:7860/health
If /health returns "model_loaded": false, the model file path is likely wrong.
UI Demo Flow
- Open
http://localhost:7860/. - Upload an image file (
jpg,jpeg, orpng;webpcan be selected in UI preview but API validation is JPG/PNG). - Adjust
Decision thresholdif needed. - Optionally enable
Show WSOL / Grad-CAM bboxand tuneCAM threshold. - Click
Predictand wait for inference. - Review label, confidence, safe/danger probabilities, and threshold.
- Open
Raw responseonly for debugging details.
3. Example API calls
Class-only prediction:
curl -X POST http://localhost:7860/predict \
-F "file=@/path/to/image.jpg" \
-F "threshold=0.5" \
-F "include_wsol=false" \
-F "cam_threshold=0.05"
Prediction with WSOL:
curl -X POST http://localhost:7860/predict \
-F "file=@/path/to/image.jpg" \
-F "threshold=0.5" \
-F "include_wsol=true" \
-F "cam_threshold=0.05"
API Contract
GET /health
Returns model/service status.
POST /predict
multipart/form-data fields:
file: required image (jpg,jpeg,png)threshold: optional float in[0, 1](default0.5)include_wsol: optional bool (defaultfalse)cam_threshold: optional float in[0, 1](default0.05)
Response:
{
"label": "safe",
"confidence": 0.91,
"prob_safe": 0.91,
"prob_dangerous": 0.09,
"threshold": 0.5,
"wsol": {
"cam_threshold": 0.05,
"bbox": {
"pixel": {
"x_min": 10,
"y_min": 20,
"x_max": 100,
"y_max": 120,
"width": 90,
"height": 100
},
"normalized": {
"x": 0.04,
"y": 0.09,
"width": 0.40,
"height": 0.45
}
}
}
}
When include_wsol=false, wsol is null.
When include_wsol=true, wsol.bbox may be null if no CAM region passes the selected CAM threshold.
Error responses use a structured format:
{
"error": {
"code": "invalid_threshold",
"message": "threshold must be between 0.0 and 1.0",
"details": {
"threshold": 1.2
}
}
}
Hugging Face Spaces Deployment (CPU First)
This repo includes Docker Space front-matter and a Dockerfile.
- Create a new Hugging Face Space with SDK set to Docker.
- Push this repository to that Space.
- Keep
b0_v2.pthin repo root (or setMODEL_PATHenv var). - Space will build and expose the app on port
7860.
Free CPU hardware is enough for class prediction demos. WSOL is available but slower on CPU.
Optional Railway Deployment
Railway can also run this Dockerized FastAPI app. For demo usage, Hugging Face Spaces CPU is usually the lowest-cost path.
Environment Variables
MODEL_PATH(default:b0_v2.pth)MAX_IMAGE_BYTES(default:8388608, i.e. 8 MB)DISABLE_STARTUP_MODEL_LOAD(default:0, useful in tests)USE_MOCK_PREDICTOR(default:0; set1for pseudo/local testing mode)
Local Docker Run
Build:
docker build -t opened-manhole-classifier:local .
Run real model:
docker run --rm -p 7860:7860 opened-manhole-classifier:local
Run mock mode:
docker run --rm -p 7860:7860 -e USE_MOCK_PREDICTOR=1 opened-manhole-classifier:local
Tests
Run:
pytest -q
Test coverage includes:
- preprocessing shape/range
- label mapping behavior
- CAM bbox fallback with no contour
- API health/predict/error behavior
- root route HTML frontend serving
- WSOL bbox-present and bbox-null API cases
Manual UI Acceptance Checklist
- Selecting an image immediately shows preview and file metadata.
- Predict button disables and shows loading text during inference.
- Non-WSOL prediction shows label, confidence, probabilities, threshold, and image.
- WSOL prediction shows canvas overlay with bbox when bbox is returned.
- WSOL with null bbox shows a clear threshold guidance message.
- Raw JSON remains available only in collapsed
<details>. - UI behavior is valid in both mock mode and real model mode.
- Layout remains readable on mobile-width screens.
Showcase References
- Suggested screenshots directory:
docs/screenshots/(for upload preview, non-WSOL result, WSOL overlay, and mobile view captures). - Model metrics reference location:
results/README.md(add the latest exported evaluation summary used for demos). - Deployment hardening note: dependency versions are pinned in
requirements.txt; review and refresh pins before each release cut.


