Finding every photo on a scanned album page (YOLO11 segmentation, running in the browser)

If you scan a page from a photo album you get one image containing six photos — white borders, sometimes a caption, sometimes two prints overlapping. Every generic cropping tool then asks you to drag a box six times, per page. With 2,000 photos that's a week of dragging.

I built a free tool that does it in one pass, and this post is about the technical decisions behind it — the model, the post-processing, and the parts that were much harder than "just run YOLO".

Why edge detection isn't enough

The obvious approach is classical CV: grayscale → Canny → findContours → approxPolyDP → take quadrilaterals. It demos beautifully on a clean scan of a single print on black background. It collapses on real album pages:

  • A white print on a white page has no contrast edge on one or two sides.
  • Sepia prints and faded borders fall inside the same luminance band as the page.
  • Prints photographed at an angle (not scanned) have perspective, shadows, and a page curl.
  • Album pages often have decorative borders, or two photos sharing an edge.

Every heuristic you add (adaptive threshold, morphological closing, aspect-ratio filters) buys one case and breaks another. I stopped fighting it and treated the problem as instance segmentation: the thing I actually want is a per-photo pixel mask, not a rectangle.

The model

yolo11n-seg, four classes, trained at 640×640, exported to ONNX. The four classes are positions/photograph types rather than semantic categories — the useful distinction turned out to be how a photo sits on the page (isolated, paired, in a grid, partially cut at the edge), because that changes which post-processing path is worth trying.

Two decisions that mattered more than the architecture:

Segmentation, not detection. A bounding box around a tilted photo includes background. With masks I get the actual footprint, and from the mask I can take the boundary pixels, build a convex hull, and fit the best quadrilateral — that ordering (mask → hull → quad) is what lets a slightly rotated print be straightened by a perspective warp instead of cropped with slivers of page.

Killing the augmentation. The default Ultralytics recipe (mosaic, mixup, rotation, shear, perspective) is designed for photos of objects in the wild. My inputs are flat scans with a near-constant camera angle. Rotation and perspective augmentation taught the model to produce tilted masks for prints that were perfectly axis-aligned in the source, which then cost accuracy in post-processing. The shipped config disables mosaic, mixup, rotation, shear, perspective and vertical flip, and keeps only a scale jitter (0.5) and a horizontal flip. Counter-intuitive, but the model got better by training on less variety — the variety that exists in the domain is border contrast, print age and layout, not camera geometry.

Running it in the browser

The export is fp32, ~11 MB. I run it with onnxruntime-web 1.19.2, WASM backend with SIMD enabled, numThreads = 1.

  • fp32, not int8. Quantising the segmentation head degraded mask edges more than it saved in size; a 2 px error on the mask boundary becomes a visible 3–4 px offset after the warp. 11 MB is cached by the service worker after first load, so the cost is a one-time download.
  • One thread, deliberately. Multi-threaded WASM needs cross-origin isolation (COOP/COEP headers). That's fine for a standalone app but breaks embedded/iframe usage and complicates a Cloudflare Pages deploy. Single-threaded inference on a modern phone is fast enough for an interactive tool, and it behaves identically everywhere.
  • Nothing is uploaded. Inference runs on the user's CPU. That's a product decision (old family photos are private) that also removes the entire server bill.

The preprocessing is the usual letterbox: scale to fit 640×640, pad the shorter side, keep the scale factor and the offsets. The bug that cost me the most time: YOLO's ONNX output is in 640-space pixels, not normalised coordinates. Every coordinate needs the inverse letterbox transform applied before you draw a box or crop a mask, otherwise your crops drift toward the top-left corner in proportion to how rectangular the input was.

Post-processing

  1. conf = 0.25 — deliberately low. A missed photo is a visitor re-doing the page by hand; a false positive is one box they delete. Asymmetric cost, asymmetric threshold.
  2. NMS at IoU = 0.45 to collapse the duplicate predictions the low threshold produces.
  3. Per surviving instance: mask → threshold → boundary pixels → convex hull → choose the 4 hull points that maximise a quadrilateral score (area × right-angle-ness).
  4. Perspective-warp that quadrilateral to a rectangle, so the output is a straight, upright photo.

Where it still fails

Honest list, because "AI crops your photos" invites the wrong expectation:

  • Prints touching each other with no gap merge into one instance. Two prints, one crop.
  • Very small prints (contact-sheet thumbnails) fall below the 640-input resolution — the model sees texture, not a boundary.
  • Extremely low-contrast scans (white print, white page, no shadow) still produce a partial mask on one edge.
  • Non-photo rectangles — a page with a postcard or a ticket — get cropped as a photo. Usually correct, occasionally surprising.

The tool is at smartphotocropper.com — free, no signup, no upload. If you're working on the same problem, the parts worth stealing are the mask→quadrilateral ordering and disabling the geometric augmentations; the rest is plumbing.

If you've got a scan that breaks it, I'd genuinely like to see it — those are the cases that become the next training batch.