π Live demo: HF Space β the Streamlit app, with sample diagrams Β· API on the same host: https://iambatman07-diagram-structure-extractor.hf.space β try
/health,/extractand/docs
Give it a picture of an architecture diagram and it returns the diagram's structure as JSON: every text label, every box, every arrow, every icon, and which components connect to which. Four independent computer-vision detectors run over the image, and a graph builder turns their output into relationships from geometry alone β no hardcoded edges.
The reliability claim is structural rather than statistical. Output is a Pydantic v2 model, validated by construction, and each detector stage is wrapped so a failure produces an empty list instead of a crash. That is what makes the JSON parseable on every call, which is what a downstream RAG indexer or knowledge-graph builder actually needs.
Four evaluation sets, each with an answer key of components and directed connectors:
| Set | Diagrams | What it is | Role |
|---|---|---|---|
data/eval/diagrams_15 |
14 | synthetic renders of public reference architectures (generate_diagrams.py) |
benchmark |
data/eval/diagrams_15 |
1 | search_interview_test.png, a real draw.io export |
benchmark |
data/eval/drawio_style |
8 | synthetic draw.io-style diagrams: 1-px dashed connectors, small heads, elbows, double heads, line labels, icon nodes, rounded containers | tuned against |
data/eval/drawio_heldout |
10 | draw.io-style, built independently in different styles (fonts, 1-2 px and coloured lines, block heads, diagonals, user/cloud/queue/server icons, UML frames) | held out: first scored after tuning was frozen |
Both draw.io-style sets were generated by agents that never saw the detector code, and each was checked against its key by a second, independent agent.
The synthetic renders were redrawn on 2026-10-05. Every arrow used to be aimed from box centre to box centre, stopping about 30 px short of each centre, so the horizontal and near-horizontal ones (15 of 52) ran straight through both labels, which made OCR misread them. Arrows now run edge to edge, as in real diagrams; diagram_01's App Server box (off the canvas) and diagram_11's Orchestrator β Saga Log arrow (through Payment Step) were fixed with it.
The real diagram's answer key was re-audited on 2026-10-05. Three independent readings of zoomed crops agreed on all 8 drawn connectors. The original key, written from an earlier extraction, listed a connector that is not drawn (Server Website β Plant An App - AWS), gave two the wrong end (the long elbow ends at ELSER Model, not the Elasticsearch Serverless container around it; the Database connector ends at the Back-End frame, not the Elastic Language Client inside it), missed ELSER Model β Indices, and recorded three double-headed connectors as one-way. Scores below use the audited key; against the original key the current pipeline scores 0.471 on this diagram.
Relationships are scored as directed source β target pairs. A double-headed connector, detected or in a key, counts as one edge each way, so finding only one of its heads costs a missed edge, and a head the key doesn't have costs a false one.
Relationships macro-F1, full pipeline:
| Previous release | Now | |
|---|---|---|
| 14 synthetic diagrams | 0.992 | 1.000 |
| Real draw.io export (audited key) | 0.250 | 0.900 |
| Draw.io-style set (tuned against) | 0.351 | 0.828 |
| Draw.io-style held-out set | 0.306 | 0.789 |
| Held-out set, arrow stage alone (answer-key boxes) | 0.533 | 0.914 |
"Previous release" is dev at e50ef27 run through the same scoring. The real diagram and the first draw.io set were both used to tune, so their scores flatter the method.
The held-out set is the honest check, with one caveat. It was first scored once the configuration was frozen: 0.711 full pipeline, 0.866 arrow stage. A code review then found bugs, some of them on held-out diagrams, and the fixes were checked against every set, held-out included. So 0.789 is the current code's score but no longer an untouched one; 0.711 is the clean held-out number.
What changed:
- Connectors (
src/arrow_detection/directed_lines_detector.py):- Thin 1-px dashes are bridged before line finding.
- Letters and solid shapes (logos, filled icons and boxes) are dropped by shape. A connector drawn up to a filled shape survives its removal.
- Segments that run along a closed outline (box borders, containers, icons) are dropped, while a connector that only crosses a border survives.
- Collinear pieces cut by a white-backed line label ("VPN", "HTTPS") are rejoined, but never across a box outline.
- Segments meeting at a right angle become elbow polylines, unless an end carries an arrowhead.
- Arrowheads are found as compact solid blobs, at the scale of the line they end. A head pointing back into a line is not its head, and each head ends one connector.
- An end with a head is extended to its tip. Two convincing heads, both clearly pointing outward, make the connector
bidirectional.
- Icon nodes (
src/box_detection/icon_nodes.py): a caption outside every box with a compact graphic just above it is an icon node, such as a database cylinder over "Orders DB". Contour box detection misses these when a connector touches the icon. Two-line captions give one node, and text written inside an icon is not mistaken for its caption. - Mapping (
src/graph/builder.py):- Unlabelled icons and titled frames take the closest caption just below or above them.
- An end inside nested boxes goes to the innermost labelled one. An end that reaches an unlabelled node stops there.
- Lines along a labelled box or container edge are dropped.
- A branch with its own arrowhead that leaves another connector's shared first leg is traced back, along that connector, to its tail.
- Ends snap within 60 px (was 160).
Remaining misses. On the real diagram, two directed edges are missed: Elastic Connector for MS SQL β DB, and the second of Back-End β Database's two small heads.
Across the two draw.io sets, 39 directed edges are still missed:
| Cause | Missed edges |
|---|---|
| Line or arrowhead never found (missed even with the answer-key boxes) | 20 |
| End is an icon or queue node that box detection doesn't find, though its caption is read correctly | 8 |
| Mapping error, with both boxes found | 8 |
| OCR slip, such as "Vertex Al" for "Vertex AI" | 3 |
Source: results/arrow_detector_comparison.csv (python benchmark_arrow_detectors.py)
| Stage | Champion | Precision | Recall | F1 | Runners-up (F1) |
|---|---|---|---|---|---|
| Text | PaddleOCR | 0.927 | 1.000 | 0.962 | EasyOCR 0.950 |
| Boxes | Canny + contours | 1.000 | 1.000 | 1.000 | Hough 0.359, YOLOv8 0.000 |
| Arrows | directed lines | 1.000 | 1.000 | 1.000 | CNN-verified Hough 0.565, plain Hough 0.475, pixel scan 0.226 |
| Icons | Template matching | 1.000 | 1.000 | 1.000 | CLIP 0.667, HSV 0.000 |
Text is scored over all 15 benchmark diagrams; boxes and arrows over the 14 synthetic ones. The arrow row scores raw detector output as unordered pairs against the answer-key boxes: an endpoint must lie within 25 px of a box border, and a connector found twice counts as a false positive. PaddleOCR is the default text detector; Tesseract is wired in but was not scored (no native binary on the benchmark machine). Source: results/phase2_leaderboard.csv
The 15 benchmark diagrams:
| Stage | Components F1 | Relationships F1 | Schema-valid | Ξ relationships |
|---|---|---|---|---|
| A β text only | 0.986 | 0.000 | 1.000 | β |
| B β + boxes | 0.997 | 0.000 | 1.000 | 0.000 |
| C β + arrows | 0.997 | 0.993 | 1.000 | +0.993 |
| D β + ray intersection | 0.997 | 0.993 | 1.000 | 0.000 |
| E β + outside-box gate (opt-in) | 0.997 | 0.905 | 1.000 | β0.089 |
| F β full pipeline (gate off) | 0.997 | 0.993 | 1.000 | +0.089 |
Two things worth stating plainly:
- The outside-box gate is off by default. It was calibrated for the old Hough detector's box-border artifacts. With the directed-lines detector it costs β0.089 relationship F1 on this benchmark, and nothing on the draw.io sets is gained by it: 0.799 vs 0.828 on the tuned set, 0.778 vs 0.789 on the held-out set, and 0.900 either way on the real diagram. It stays available as
outside_box_gate=true/--gate. - Schema validity is 1.000 at every single stage. That is the property the design is actually optimising for, and it never moves. It holds by construction (every result is a Pydantic model); the ablation records it rather than re-validating each output.
Source: results/ablation.csv
| Metric | Day-1 pipeline (measured) | Claude Vision (projection) |
|---|---|---|
| Schema-valid JSON rate | 1.000 | 0.130 |
| Components macro-F1 | 0.763 | n/a |
| Arrows macro-F1 | 0.172 | n/a |
| Avg runtime per diagram | 15.37 s | 2.20 s |
| Cost per diagram | $0.0000 | $0.0177 |
The Claude Vision column was never measured. The benchmark run had no
ANTHROPIC_API_KEY, so that column is a literature-based projection, andresults/frontier_comparison.csvlabels it(PROJECTION)in the header. The harness atbenchmark_claude_vision.pyis live-ready and will replace it with real numbers when re-run with credentials. A live run would score Claude on the redrawn 2026-10-05 renders while the Day-1 column stays on the original ones, so the two columns would not be like-for-like.The Day-1 pipeline column (
DiagraMine (measured)in the CSV) is a frozen Day-1 measurement (results/baseline_metrics.json, 2026-05-25): the original monolithic pipeline on the original benchmark renders. It is not the current pipeline β see the ablation above for that β and the harness that produced it no longer runs.The vision model is also genuinely faster β 2.20 s against 15.37 s β and that row is a loss.
Source: results/frontier_comparison.csv
- Upload a PNG, JPG or WebP through the CLI, the FastAPI endpoint or the Streamlit demo.
src/pipeline.pyselects detectors from aPipelineConfigand wraps each stage so failure degrades to an empty list β this is what guarantees schema-valid output.- Four detectors run: PaddleOCR for text, Canny + contours (plus icon nodes found from their captions) for boxes, directed lines for arrows (any angle, elbows, arrowhead direction, double heads), template matching for icons. Any stage can be set to
noneto skip it. src/graph/builder.pylabels boxes with their text (or the caption under an icon / title over a frame) and derives relationships from geometry β there are no hardcoded edges, and a pytest AST regression fails the build if any reappear.- Emit a typed
ExtractionResult, plus an annotated PNG, anetworkxgraph render and a flat edge CSV.
| Layer | Technology |
|---|---|
| Text | PaddleOCR (champion, default) Β· EasyOCR Β· Tesseract (optional native binary) |
| Boxes | OpenCV Canny + contours + caption-anchored icon nodes Β· Hough Β· YOLOv8 |
| Arrows | directed lines (any-angle Hough, elbows, arrowhead orientation, double heads) Β· Hough lines + thinning Β· CNN verifier |
| Icons | template matching Β· CLIP Β· HSV |
| Graph | networkx (kamada-kawai layout) |
| Schema | Pydantic v2 |
| API | FastAPI Β· Streamlit demo |
| Packaging | Docker |
| Tests | 73 pytest, including a no-hardcoding AST regression |
# 1) Local install β Python 3.8β3.12 (3.11 recommended, as in the Dockerfile);
# paddlepaddle 2.6, behind the default PaddleOCR detector, has no 3.13+ wheels
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
# 2) Run on the bundled test diagram (or any path)
python diagram_analysis.py # uses search_interview_test.png
python diagram_analysis.py path/to/your/diagram.png
# 3) Modular orchestrator with detector overrides
python -m src.pipeline data/eval/diagrams_15/diagram_02.png \
--out /tmp/out --text easyocr --arrow hough_lines
# 4) FastAPI service
uvicorn src.api:app --port 8000
curl -F "[email protected]" http://localhost:8000/extract | jq .
# 5) Streamlit demo
streamlit run app.py
# 6) Tests
pytest tests/ -q
# 7) Benchmarks
python data/eval/generate_diagrams.py # re-render diagram_01..14 + ground_truth.json
python data/eval/derive_ground_truth_boxes.py # pixel box answer key
python data/eval/drawio_style/generate_drawio_style.py # draw.io-style set (tuned against; needs Arial, PNGs are the reference)
python data/eval/drawio_heldout/generate.py # held-out set (Windows fonts; PNGs are the reference)
python benchmark_ablation.py
python benchmark_arrow_detectors.py
python evaluate_phase2a.py && python evaluate_phase2b.py && python results/_build_leaderboard.py
python benchmark_claude_vision.py
# 8) Docker
docker build -t diagramine:4.0 .
docker run -p 8000:8000 diagramine:4.0Tesseract is wired in as an optional text detector but needs the native binary installed separately. Regenerate the diagram with python assets/make_architecture.py.
POST /extract (multipart, [email protected])
Optional query params: ?text=paddleocr&box=canny_contours&arrow=directed_lines&icon=template_matching&outside_box_gate=false. Any detector can be none to skip that stage.
{
"structure": { "...ExtractionResult (typed Pydantic)..." },
"annotated_png_base64": "iVBORw0KG...",
"relationship_csv": "source,target,line_style,direction,relationship\n..."
}GET /health β {"status": "ok", "service": "diagramine", "version": "4.0"}
MIT. See LICENSE.
