Skip to content

fix(ocr): enforce valid Paddle hOCR word bounds

Riccardo Di Mambro requested to merge feature/hocr-bbox-integrity into master

Summary

Fixes invalid zero-area word bounding boxes generated in Paddle hOCR output.

Paddle native word coordinates are now intersected with both the owning line bounds and the page bounds before hOCR serialization. Words whose resulting intersection has no positive area are discarded instead of producing invalid coordinates.

Original defect

For the source TIFF 00000266.tif (4168 × 3638), the previous implementation generated:

<span class="ocrx_word" title="bbox 4168 375 4168 456; x_wconf 80">A</span>

Because x0 == x1, the word had zero width.

Changes

  • Clip native Paddle word boxes to their owning line.
  • Clip word boxes to the page bounds.
  • Drop words whose clipped bounding box has zero or negative area.
  • Prevent discarded native word records from being recreated through the synthetic-word fallback.
  • Add regression coverage for clipping and removal of invalid word boxes.

Automated validation

  • Targeted hOCR tests: 30 passed, 2 warnings
  • Regression suite: 76 passed, 11 skipped, 5 warnings
  • Pre-commit regression suite: 76 passed, 11 skipped, 5 warnings
  • git diff --check: passed
  • CI image build: passed

Runtime validation

Validated using the GPU image:

registry.endurance-italia.com:5050/empati/ocr_engine:2026-10-10T19-19-10_feature-hocr-bbox-integrity_8a48f916_gpu

The regenerated hOCR for 00000266.tif contains:

  • 269 word elements;
  • 0 zero-area word bounding boxes;
  • 0 word bounding boxes outside the image;
  • no occurrence of the original bbox 4168 ... 4168 ... defect.

The previously affected character is retained with a valid positive-area bounding box:

A: bbox 3950 414 3957 443

Merge request reports

Loading