Skip to content

Pipeline stages and theory

TactiDiagram is a hybrid computer-vision pipeline. OCR and segmentation use trained neural networks; OpenCV operations and project-specific algorithms connect their results and convert them into tactile geometry.

End-to-end pipeline

flowchart TD
  A["Input: PNG, JPG, WebP"] --> B["Decode and normalize"]
  B --> AT{"Use Autotrace?"}
  AT -->|No| PREP["Prepared analysis raster"]
  AT -->|Yes| ATR["Potrace reconstruction + SVG rasterization"]
  ATR --> PREP
  PREP --> I0{"Use Indra OCR?"}
  I0 -->|Yes| I1["PaddleOCR detection"]
  I1 --> I2["Indra recognition"]
  I2 -->|Success| F["Normalize word boxes"]
  I2 -.->|Error or no text| D["Full PaddleOCR detection and recognition"]
  I0 -->|No| D
  D -->|Success| F
  D -.->|Error or no text| E["Tesseract fallback"]
  E --> F
  F --> G["Filter low-confidence or implausible words"]
  G --> H["Group words into text lines"]
  H --> I["Merge stacked lines into semantic labels"]
  I --> J["Science-vocabulary correction, shown for review"]
  J --> K["Identify labels on the drawing"]
  K --> L["Thin-stroke extraction"]
  L --> M["Probabilistic Hough line segments"]
  M --> N["Assign segments to labels and chain broken leaders"]
  N --> O["Leader endpoint / in-figure anchor"]
  B --> P["Foreground mask and structural line analysis"]
  O --> Q["Point prompts"]
  P --> Q
  Q --> R{"Segmentation backend"}
  R -->|Default| S["SlimSAM embedding and mask candidates (WASM)"]
  R -->|Opt-in| S2["SAM 2.1 Hiera Tiny embedding and mask candidates (WebGPU)"]
  S --> T["Mask filtering and selection"]
  S2 --> T
  P --> U["Detailed or Simplified line-art extraction"]
  T --> V["Label-guided line hierarchy"]
  U --> V
  J --> W["Millimetre tactile layout"]
  V --> W
  W --> X["Braille, keys, textures, knockouts"]
  X --> Y["Automated guideline checks"]
  Y --> Z["Transcriber review and correction"]
  Z --> AA{"Diagram output mode"}
  AA -->|Default| A1["Segmented tactile SVG"]
  AA -->|Direct Potrace option| A2["Original source: RGB Sobel + Otsu + Potrace"]
  A2 --> A3["Transparent monochrome paths + independent braille layers"]
  A1 --> AB["Optional SVG editor for manual correction and QC"]
  A3 --> AB
  AB --> AC["Physical proof and tactile QA"]

1. Input normalization

The UI accepts PNG, JPEG/JPG, and WebP. It rejects files over 15 MB and scales the longest image side to at most 1,400 pixels before analysis. The image is drawn over a white canvas, so transparent pixels are composited onto white. This bounds memory and inference time while retaining detail for common textbook diagrams.

The original image is retained in the UI for comparison. Analysis operates on the normalized raster.

2. OCR: find printed labels

Engine chain

The engines are tried in a fixed order, and only a failure or an empty result moves the pipeline down the chain:

  1. Indra OCR (Datalab, v1.1.0), only when the "Use Indra OCR" checkbox is set; experimental, Latin-script only.
  2. PaddleOCR, the default.
  3. Tesseract.js, if the ONNX path itself cannot run.

The purpose is resilience, not ensembling: engines are not voting on the same label. Which engine answered is recorded in the analysis result ("ocrEngine") so evaluation runs can compare them. See OCR engines.

PaddleOCR is the default

The pipeline attempts PaddleOCR first. Its two ONNX models have separate jobs:

  • Text detector: PP-OCRv6 detection-small predicts regions likely to contain text. The implementation uses a DB-style detector and post-processes probability maps into candidate text boxes.
  • English recognizer: PP-OCRv5 English recognition-mobile reads cropped text regions. It uses a CTC-style character sequence output and the character dictionary embedded in the ONNX model metadata.

The app loads both models through ONNX Runtime Web using the WASM execution provider. The detector proposes line/region boxes; recognition reads the crops. The current implementation normalizes recognized lines into approximate word boxes based on character counts. Those boxes are regrouped by the general OCR code.

Tesseract is the fallback

If PaddleOCR cannot load or throws during recognition, the pipeline falls back to Tesseract.js. It uses English traineddata, LSTM-only OCR engine mode (OEM 1), and page segmentation mode 11 (sparse text), suitable for scattered diagram labels rather than full paragraphs.

The fallback makes a second pass on an adaptive-threshold/binarized image and merges overlapping word boxes, retaining the more confident recognition. This can recover text that is difficult to read against a coloured fill.

OCR post-processing

Word boxes are filtered using confidence and geometry rules. Nearby words on a shared baseline become lines. Stacked lines may be merged into one multi-line label unless separate leaders indicate that they are separate labels. Science-vocabulary corrections can repair likely slips; the UI exposes corrections for human review.

OCR does not establish whether a label is scientifically correct or whether it points to the intended part. Those are later association and review tasks.

3. Leader-line detection

Leader lines connect printed labels to diagram parts. They are not the same as the outlines of the parts.

The current method:

  1. Converts the image to grayscale.
  2. Uses morphological black-hat and top-hat operations to highlight thin dark-on-light and light-on-dark strokes.
  3. Removes OCR text boxes from this mask so letters are less likely to be mistaken for leaders.
  4. Uses probabilistic Hough transform to propose straight line segments.
  5. Scores candidate segments by distance from a label, direction away from the label, and length.
  6. Chains nearby, approximately collinear segments to reconstruct broken or dotted leaders.
  7. Uses the far endpoint as an approximate point on the named part.
flowchart TD
  A["OCR label box"] --> C["Candidate line segments"]
  B["Thin-stroke mask"] --> C
  C --> D["Score by proximity and direction"]
  D --> E["Assign best segment"]
  E --> F["Chain collinear gaps"]
  F --> G["Leader tip"]
  G --> H["Segmentation point prompt"]

This is a geometric heuristic, not a learned relationship model. Crossings, arrowheads, curved leaders, or crowded diagrams can produce incorrect endpoints. The review overlay is important.

4. Foreground and structural line analysis

The foreground stage estimates which pixels belong to the diagram rather than the page background. The implementation estimates a background colour from border pixels, thresholds colour distance, removes text boxes, applies morphological opening/closing, fills holes, and removes small components.

The structural line stage is separate from leader detection. It seeks meaningful internal boundaries:

  • Smooths and downsamples the image.
  • Converts colour to Lab space.
  • Clusters drawing colours with k-means.
  • Merges small regions into neighbouring regions to suppress shading speckle.
  • Extracts boundaries between colour regions when there is a real colour step.
  • Includes thin drawn strokes that are darker or more saturated than their surroundings.
  • Excludes text and short decorative marks where possible.

Leader lines communicate label-to-part relationships; structural lines describe the parts themselves.

5. Segment named parts with SlimSAM

SlimSAM is a compact, distilled Segment Anything model. The app computes an image embedding once, then uses point prompts derived from leader tips or points near labels printed inside the drawing.

For each prompt, the model returns multiple candidate masks and confidence scores. The code rejects masks outside configured area fractions. Among candidates whose score is within 0.06 of the best score, it prefers the smaller mask because a label is more likely to name a specific component than an entire surrounding object. The selected mask is intersected with a dilated foreground mask so it does not expand into the page background.

This heuristic constrains a general-purpose segmentation model to diagram parts, but it does not guarantee semantic correctness.

6. Convert visual structure to tactile structure

Two line-art styles are available:

  • Detailed: retains more of the source drawing's structure. Text and leader lines are removed from the figure tracing where possible. Boundaries associated with named-part masks receive stronger emphasis.
  • Simplified: emphasizes boundaries between flat colour regions and thin drawn strokes while suppressing shading and small specks. Nearby lines are merged/thinned more aggressively.

The tracing sensitivity control (0–100, default 55, step 5) changes foreground, edge, colour-region, and thin-stroke recovery. Higher values lower detection thresholds and can recover faint features at the cost of added noise; lower values are more conservative. Changing sensitivity recomputes tracing while reusing OCR and segmentation. It does not disable physical layout or braille-clearance logic. See Application controls and options.

7. Lay out braille and textures

The layout engine works in millimetres for the selected page size. It places uncontracted UEB Grade 1 braille, optional two-letter keys, and up to four textures. It applies line hierarchy, texture insets, braille clearance, and knockouts. See Tactile layout and BANA guidelines.

8. Check and review

The checker measures selected geometric properties such as short fragments, narrow gaps, and braille clearance. It also compares the output with a Canny edge-tracing baseline. These are diagnostics, not a pass/fail determination of tactile usability.

The transcriber must compare every label and part relationship against the source, inspect the tactile graphic and 3D relief, and perform applicable production QA. See Review workflow.

Potrace processing paths

Potrace is used in two different places. They share one locally vendored WebAssembly engine but solve different problems. Autotrace is a pre-analysis input transformation; direct Potrace SVG is a post-analysis output renderer.

A. Autotrace preprocessing

TactiDiagram includes an optional Autotrace control that uses the locally vendored esm-potrace-wasm runtime. It is an opt-in preprocessing step: when enabled, the browser passes the source image through Potrace, renders the resulting SVG back to an image at the original pixel dimensions, and sends that prepared raster through the normal analysis pipeline. OCR, leader detection, foreground and line-art analysis, and segmentation then operate on the prepared image.

Autotrace is not the default path. With the control disabled, analysis uses the original image. The original source image is retained for comparison, and the prepared image is used for the analysis and downstream tactile layout. If Potrace initialization or reconstruction fails, the run reports the error and restores the previous application state rather than silently treating the failed result as valid.

This path changes the analysis input, not the output renderer. It can suppress raster noise and simplify boundaries, but can also merge nearby features, remove faint details, or affect OCR and leader detection. Compare the reconstructed analysis view with the untouched Original view. A failed or empty reconstruction aborts the run rather than silently accepting an empty image. See Models and inference engines.

B. Direct Potrace SVG output (full analysis)

This is an output-rendering branch after ordinary analysis and layout, not a shortcut around them. The full sequence still runs:

  1. Decode and normalize the upload; optionally run Autotrace preprocessing if that separate checkbox is enabled.
  2. Run OCR and label grouping/correction.
  3. Detect and chain leader lines; estimate the named-part anchor.
  4. Compute foreground masks and structural-line analysis.
  5. Run point-prompted SlimSAM or the selected SAM 2.1 backend for every label.
  6. Build the normal physical tactile layout, including page size, braille positions, optional title, texture/layout data, key mapping, collision handling, and clearance.
  7. Run the geometric guideline checks.
  8. Call traceOriginalDiagram on the original uploaded raster, not the Autotrace-prepared raster. Fit the resulting paths to the same millimetre page coordinates.

Outline-image construction

The makeOutlineImage function in src/js/autotrace.js builds Potrace's input:

  • Source alpha is composited against white.
  • OCR-detected label rectangles are expanded by a small margin and excluded. Their pixels are whitened before gradient calculation; the mask and its one-pixel neighbourhood are skipped so the mask boundary is not traced as a false edge.
  • Sobel gradients are evaluated independently for red, green, and blue. The strongest channel magnitude is retained, preserving boundaries between differently coloured regions even when luminance is similar.
  • An Otsu threshold is calculated from non-zero gradient magnitudes and bounded to 24–76.
  • Pixels above the threshold become black; the rest become white. A near-empty edge map is rejected with an actionable error.

Potrace runs in single-colour mode with black foreground and transparent background. SVG normalization validates the result, removes background/shape primitives and style overrides, forces retained paths to black, and rejects output without editable paths.

Direct-output SVG structure

The renderer embeds traced path geometry in the diagram's millimetre coordinate system. The export contains:

  • Potrace object outlines: black editable vector paths on a transparent canvas; no white page rectangle, original colour fills, or opaque label knockouts.
  • Braille title: a separate layer when a title is supplied.
  • One braille layer per placed label: each can be moved independently in an SVG editor. Layer labels include the print label and key annotation when applicable.
  • Key page: the normal key-page renderer and print/PDF inclusion rules remain in effect when labels use keys.

The standard analysis view still exposes OCR boxes, leader estimates, anchors, and SAM masks. These remain essential review evidence even though SAM geometry is not the primary diagram artwork in this mode.

Letter keys in direct-output mode

The ordinary tactile layout is the single source of braille placement and key assignment in both output modes. With Use letter keys for every label enabled, all placed labels use unique two-letter alphabetic keys; the key page maps each key to its full print label. Direct Potrace rendering consumes those same layout cells and coordinates rather than implementing a second placement algorithm.

Limitations and review

The direct trace is an edge-derived outline, not a semantic segmentation result. It can retain internal colour boundaries, omit subtle boundaries below threshold, trace irrelevant details, and lose fine features during vectorization. OCR-masked regions are removed from the edge map, so content immediately adjacent to printed text can also be suppressed. Review paths, label placement, braille/key mapping, and a physical proof before production.

Editable SVG post-processing and quality control

The tactile SVG is an editable vector document sized in millimetres, with named groups for textures, structural line art, outlines, label-clearance knockouts, leader lines, and braille dots. The key-page SVG separates braille from sighted print-reference text. These are vector objects, not a flattened screenshot.

Inkscape is the recommended reference editor because it is free and open-source (FOSS). The SVG is standard vector artwork and can also be edited in other SVG-capable tools, including Adobe Illustrator and Affinity Designer. Inkscape is the documented default because the project targets FOSS workflows; it is not a technical requirement.

Use the vector editor for post-processing: compare with the source and analysis overlays, correct paths and false contours, adjust leaders, and inspect braille, label-to-part correspondence, line hierarchy, textures, clearances, and margins. Preserve the page dimensions and mm scale. Save a separate reviewed copy, then produce a physical proof on the intended swell-paper/fuser system.

Vector editing is an optional manual post-processing and QC stage, not automated validation. Edits do not update TactiDiagram's analysis state or guideline report. Recheck the edited SVG independently, retain the untouched export, and assess the physical proof; visual correctness alone does not establish tactile readability.