Files
ragflow/deepdoc/vision/layout_recognizer.py
Dexterity 18ea0fb7f9 fix(deepdoc): prevent figure and equation crops from merging on scientific PDFs (#15873)
### What problem does this PR solve?

Closes #15872 

On pages that contain both a textless equation and a figure, the layout
recognizer could merge the two unrelated regions into a single cropped
image with concatenated captions. This shows up most often on scientific
and technical PDFs.

The cause is a `layoutno` namespace collision in
`deepdoc/vision/layout_recognizer.py`. Text-overlapping boxes are tagged
per type by `findLayout`: figures become `figure-{ii}` using the
figure-only index, and equations become `equation-{ii}` using the
equation-only index. The fallback loop that handles textless regions,
however, indexes the combined figure plus equation list and always uses
a `figure` prefix.

Because the two paths use different index spaces and prefixes, a page
laid out as `[textless equation, figure with text]` produces two boxes
tagged `figure-0`. `_extract_table_figure` in
`deepdoc/parser/pdf_parser.py` buckets boxes by `f"{page}-{layoutno}"`,
so both fall into the same `page-figure-0` bucket, and `cropout`
stitches the disjoint regions into one image.

**Fix**

The fallback loop now iterates per type, indexes within each type's own
list, and reuses the type as the `layoutno` prefix (`figure-{i}` or
`equation-{i}`). This matches the namespace that `findLayout` already
assigns to text-overlapping boxes. Since the `visited` flag is shared by
reference, each layout is tagged by exactly one path, so per-type
indices stay collision free. Textless equations now land under
`equation-N`, consistent with text-overlapping equations, instead of the
old `figure-N`.

The same fix is applied to `AscendLayoutRecognizer`, which shared the
identical defect.

Downstream consumers of `layoutno` were checked and all treat it as an
opaque equality or bucketing key, so no other code paths are affected.
When a page has no equations, the combined index equals the figure-only
index and the output is unchanged.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
2026-07-22 14:28:44 +08:00

19 KiB