fix: keep extracted text aligned with rotated PDF page images in hi_res - #17
Conversation
Source PR: Unstructured-IO#4367 Source head: 6e7f122
⛔ Shipwright · BlockedRecommendation: do not merge PR #17 · Tier
Findings (6)
Fireworks usage: 29,680 input · 935 output · 30,615 total tokens · $0.0071 · 14s · 0 fix iteration(s) Open the Shipwright check for full evidence and the audit bundle. Use |
| angle = ( | ||
| rotation_corrections[page_number] | ||
| if rotation_corrections is not None and page_number < len(rotation_corrections) | ||
| else 0 |
There was a problem hiding this comment.
Shipwright · HIGH
The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path.
Impact: The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path. If merge_inferred_with_extracted_layout consumes element_coords from the pdfminer layout and compares them against the object-detection layer, the fix is incomplete for any element whose coordinates are stored or recomputed elsewhere. The diff only mutates layout.element_coords at pdfminer…
Suggested fix: Review the cited evidence, fix the risk if confirmed, and rerun Shipwright.
| angle = ( | ||
| rotation_corrections[page_number] | ||
| if rotation_corrections is not None and page_number < len(rotation_corrections) | ||
| else 0 |
There was a problem hiding this comment.
Shipwright · HIGH
The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path.
Impact: The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path. If merge_inferred_with_extracted_layout consumes element_coords from the pdfminer layout and compares them against the object-detection layer, the fix is incomplete for any element whose coordinates are stored or recomputed elsewhere. The diff only mutates layout.element_coords at pdfminer…
Suggested fix: Review the cited evidence, fix the risk if confirmed, and rerun Shipwright.
| angle = ( | ||
| rotation_corrections[page_number] | ||
| if rotation_corrections is not None and page_number < len(rotation_corrections) | ||
| else 0 |
There was a problem hiding this comment.
Shipwright · HIGH
The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path.
Impact: The rotation correction is applied to layout.element_coords but not to the per-element coordinates used later in the merge path. If merge_inferred_with_extracted_layout consumes element_coords from the pdfminer layout and compares them against the object-detection layer, the fix is incomplete for any element whose coordinates are stored or recomputed elsewhere. The diff only mutates layout.element_coords at pdfminer…
Suggested fix: Review the cited evidence, fix the risk if confirmed, and rerun Shipwright.
Problem
On PDF pages with a non-zero
/Rotate, the hi_res object-detection layer (which runs on the rendered page image) and the pdfminer-extracted text layer could end up in different coordinate frames, off by the page rotation. The merge then placed extracted text in the wrong locations, scattering it across the output.Fix
unstructured-inferencemay rotate a rendered page image to make its dominant text upright and reports that angle aspdf_rotation_correctionin the page image metadata. This change mirrors that same rotation onto the pdfminer-extracted coordinates so both layers share one coordinate frame and merge correctly._rotate_bboxesrotates bounding boxes to match PIL'srotate(angle, expand=True).process_data_with_pdfminer/process_file_with_pdfmineraccept a per-pagerotation_correctionslist and apply it to element coordinates and link bounding boxes.partition_pdfreadspdf_rotation_correctionfrom the inferred layout's image metadata and threads it into the pdfminer pass.unstructuredperforms no orientation detection of its own — it simply mirrors the correction reported by the renderer, so the two layers stay aligned by construction.Tests
_rotate_bboxescovering the 0/90/180/270 directions, round-trip, and bbox validity.Requires the paired
unstructured-inferencechange that emitspdf_rotation_correction.🤖 Generated with Claude Code
Summary by cubic
Keeps extracted text aligned with rotated PDF page images in hi_res by mirroring the renderer’s rotation onto
pdfminercoordinates, fixing scattered text on pages with non-zero/Rotate.Bug Fixes
rotation_corrections(frompdf_rotation_correctionimage metadata) throughpartition_pdfintoprocess_*_with_pdfminer(file and data paths); rotate element coords and link bboxes to mirror PILrotate(angle, expand=True)._rotation_corrections_from_layoutand_rotate_bboxeswith tests covering default-to-0, pass-through into pdfminer, and 0/90/180/270 rotation behavior.Dependencies
unstructured-inferenceminimum to>=1.6.12which emitspdf_rotation_correction.Written for commit 6e7f122. Summary will update on new commits.
Source merge-base:
19857c193657b5f1b0ca922562a3015b38cc6fb8Source head:
6e7f1221a99c587451b0c80cf30b4b544e74757a