--- title: Crack Detection And Measurement emoji: 🧱 colorFrom: gray colorTo: red sdk: docker app_port: 7860 pinned: false --- # Crack Detection & Measurement Upload a photo of a wall; the app detects the crack, measures its width and length in real centimetres, and shows the segmented result. > The block above is Hugging Face Spaces configuration. It is ignored when > running locally and tells the Space to build from the `Dockerfile`. ## Pipeline 1. **Scale & rectification (ArUco)** — a printed ArUco marker in the photo gives four points with known real-world coordinates. The wall plane's homography is recovered and the image is warped to a fronto-parallel view where **1 pixel = a fixed known mm**. This corrects camera angle and fixes scale. See `aruco_scale.py`. 2. **Segmentation (ML)** — `best_crack_model.pth`, a ResNet34-Unet, *locates* the crack and produces a binary mask. See `crack_pipeline.py`. 3. **Measurement (computer vision)** — within the ML-detected region, a black-tophat operator (local intensity contrast) plus Otsu thresholding re-segments the crack tightly on its actual dark pixels, not the model's wider "crack zone". Width is then sampled along the crack centreline. See `cv_crack.py`. Reported: **max width**, **min width** (both excluding the crack's tapering tips), and **total length**. The refined crack mask is shown as the segmented image. ## Print the marker ```bash ./venv/bin/python make_marker.py # -> aruco_marker_60mm_A4.pdf ``` Print at **100% / Actual Size** (no scaling); verify the black square is exactly **6.0 cm**. Tape it flat on the wall next to the crack and photograph wall + marker together. The app serves the PDF at `/marker`. ## Run ```bash ./run.sh ``` Creates the virtualenv on first run, then serves at (port 5000 is taken by macOS AirPlay). Override with `PORT=8080 ./venv/bin/python app.py`. ## Files | File | Purpose | |------------------------|--------------------------------------------------| | `aruco_scale.py` | ArUco detection, plane rectification, mm scaling | | `crack_pipeline.py` | Model loading, ML segmentation, pipeline, overlay| | `cv_crack.py` | Intensity-profile crack-width measurement | | `make_marker.py` | Generates the printable marker PDF | | `app.py` | Flask server (`/`, `/analyze`, `/marker`, `/health`) | | `templates/index.html` | Upload UI and results view | | `best_crack_model.pth` | Trained segmentation weights | ## API `POST /analyze` — multipart form, field `image`. Returns JSON: base64 PNG `overlay`, a `measurements` object (`summary` / `summary_mm` with max width, min width, total length), and an `aruco` object describing marker detection. ## Deploying The app is Flask + PyTorch (CPU). For a public deployment: - Replace the Flask dev server with a production server (`gunicorn`). - Pin dependency versions in `requirements.txt`. - It needs ~1.5–2 GB RAM (PyTorch + the 98 MB model). - Good hosts: **Hugging Face Spaces** (free, built for ML demos), or a Docker container on Fly.io / Render / Railway.