Repository navigation
fix(extraction): keep text inside infographic boxes when infographic extraction is off #2839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
00ff799
92e8e98
fe1fd38
dc3ffe9
51bb740
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -865,6 +865,7 @@ def postprocess_page_elements_v3( | |
| annotation_dict: Dict[str, List[List[float]]], | ||
| labels: Optional[List[str]] = None, | ||
| final_score: Optional[Dict[str, float]] = None, | ||
| suppress_text_in_infographics: bool = True, | ||
| ) -> Dict[str, List[List[float]]]: | ||
| """ | ||
| Expand bounding boxes of tables/charts/infographics and titles based on the bounding boxes of the other class. | ||
|
|
@@ -873,6 +874,8 @@ def postprocess_page_elements_v3( | |
| "table", "chart", "infographics", "title", "paragraph", "header_footer". | ||
| labels: ordered class labels corresponding to the annotation dictionary. | ||
| final_score: per-class thresholds used by the final output filter. | ||
| suppress_text_in_infographics: remove text, title, and header/footer boxes contained in | ||
| infographic boxes. Disable when infographic content is not extracted, so that text is kept. | ||
|
|
||
| Returns: | ||
| annotation_dict: same as input, with expanded bboxes for page elements. | ||
|
|
@@ -924,10 +927,12 @@ def postprocess_page_elements_v3( | |
| return {label: [] for label in labels} | ||
|
|
||
| bboxes, confidences, label_idxs, found_title = match_structured_boxes_with_title( | ||
| bboxes, confidences, label_idxs, labels | ||
| bboxes, confidences, label_idxs, labels, match_infographics=suppress_text_in_infographics | ||
| ) | ||
| bboxes, label_idxs, confidences = expand_tables_and_charts(bboxes, confidences, label_idxs, labels, found_title) | ||
| bboxes, label_idxs, confidences = postprocess_included_texts(bboxes, confidences, label_idxs, labels) | ||
| bboxes, label_idxs, confidences = postprocess_included_texts( | ||
| bboxes, confidences, label_idxs, labels, suppress_text_in_infographics=suppress_text_in_infographics | ||
| ) | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| order = np.argsort(bboxes[:, 1] * 10 + bboxes[:, 0]) | ||
| bboxes, confidences, label_idxs = bboxes[order], confidences[order], label_idxs[order] | ||
|
|
@@ -1578,7 +1583,14 @@ def get_overlaps(boxes, other_boxes, normalize="box_only"): | |
| return overlaps | ||
|
|
||
|
|
||
| def postprocess_included(boxes, labels, confs, class_="title", classes=["table", "chart", "title", "infographic"]): | ||
| def postprocess_included( | ||
| boxes, | ||
| labels, | ||
| confs, | ||
| class_="title", | ||
| classes=["table", "chart", "title", "infographic"], | ||
| suppress_text_in_infographics=True, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The repository requires type annotations on every public function parameter. Add Rule Used: All public functions, methods, and class attributes must have type annotations. This includes parameter types, return types, and class-level attributes. Private/internal helpers (prefixed with _) are encouraged but not required. (source) Prompt To Fix With AIThis is a comment left during a code review.
Path: nemo_retriever/src/nemo_retriever/models/nim/primitives/model_interface/yolox.py
Line: 1592
Comment:
**New flags lack types.** The new `suppress_text_in_infographics` parameter has no type annotation. The same applies to `match_infographics` in `match_structured_boxes_with_title` and `suppress_text_in_infographics` in `postprocess_included_texts`.
The repository requires type annotations on every public function parameter. Add `: bool` to these three new parameters; the older untyped parameters are outside this finding.
**Rule Used:** All public functions, methods, and class attributes must have type annotations. This includes parameter types, return types, and class-level attributes. Private/internal helpers (prefixed with _) are encouraged but not required. ([source](https://github.com/nvidia/nemo-retriever/blob/dc3ffe92e7efe27648079031ee5f3b0abc386815/.greptile/config.json))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| ): | ||
| """ | ||
| Post process title predictions. | ||
| - Remove titles that are included in other boxes | ||
|
|
@@ -1588,6 +1600,7 @@ def postprocess_included(boxes, labels, confs, class_="title", classes=["table", | |
| labels (numpy.ndarray [N]): Array of labels. | ||
| confs (numpy.ndarray [N]): Array of confidences. | ||
| class_ (str, optional): Class to postprocess. Defaults to "title". | ||
| suppress_text_in_infographics (bool, optional): Treat infographic boxes as containers. Defaults to True. | ||
| classes (list, optional): Classes. Defaults to ["table", "chart", "title", "infographic"]. | ||
|
|
||
| Returns: | ||
|
|
@@ -1606,7 +1619,7 @@ def postprocess_included(boxes, labels, confs, class_="title", classes=["table", | |
|
|
||
| # other_boxes = boxes[labels != classes.index("title")] | ||
|
|
||
| inclusion_classes = ["table", "infographic", "chart"] | ||
| inclusion_classes = ["table", "infographic", "chart"] if suppress_text_in_infographics else ["table", "chart"] | ||
| if class_ in ["header_footer", "title"]: | ||
| inclusion_classes.append("paragraph") | ||
|
|
||
|
|
@@ -1655,7 +1668,7 @@ def remove_overlapping_boxes_using_wbf(boxes, confs, labels): | |
| return boxes, confs, labels | ||
|
|
||
|
|
||
| def match_structured_boxes_with_title(boxes, confs, labels, classes): | ||
| def match_structured_boxes_with_title(boxes, confs, labels, classes, *, match_infographics=True): | ||
| # Reorder by y, x | ||
| order = np.argsort(boxes[:, 1] * 10 + boxes[:, 0]) | ||
| boxes, confs, labels = boxes[order], confs[order], labels[order] | ||
|
|
@@ -1668,7 +1681,7 @@ def match_structured_boxes_with_title(boxes, confs, labels, classes): | |
| confs, | ||
| labels, | ||
| classes, | ||
| to_match_labels=["chart", "table", "infographic"], | ||
| to_match_labels=["chart", "table", "infographic"] if match_infographics else ["chart", "table"], | ||
| remove_matched_titles=True, | ||
| ) | ||
|
|
||
|
|
@@ -1718,9 +1731,11 @@ def expand_tables_and_charts(boxes, confs, labels, classes, found_title): | |
| return boxes, labels, confs | ||
|
|
||
|
|
||
| def postprocess_included_texts(boxes, confs, labels, classes): | ||
| def postprocess_included_texts(boxes, confs, labels, classes, suppress_text_in_infographics=True): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Missing types on new public options New Devin left this for you to triage: Real repo-rule gap (new bool params lack annotations), but #2839 is already merged. Recommend a tiny follow-up PR off main adding Learn moreThe new options on Example: Recommended fix: Annotate the newly added parameters as Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| for c in ["title", "paragraph", "header_footer"]: | ||
| boxes, labels, confs = postprocess_included(boxes, labels, confs, c, classes) | ||
| boxes, labels, confs = postprocess_included( | ||
| boxes, labels, confs, c, classes, suppress_text_in_infographics=suppress_text_in_infographics | ||
| ) | ||
| return boxes, labels, confs | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Text inside infographic boxes is kept unless infographic content is extracted.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from nemo_retriever.common.modality.page_elements.shared import ( | ||
| _apply_final_score_filter, | ||
| _apply_page_elements_v3_postprocess, | ||
| ) | ||
| from nemo_retriever.models.nim.primitives.model_interface.yolox import postprocess_page_elements_v3 | ||
|
|
||
| # Raw Page Elements v3 detections (before postprocessing) for a scanned one-page menu whose text | ||
| # blocks all lie inside one infographic box. | ||
| _MENU_PAGE = [ | ||
| ("text", (0.3761, 0.4244, 0.9642, 0.5818), 0.7944), | ||
| ("text", (0.3802, 0.6713, 0.8256, 0.7127), 0.7410), | ||
| ("title", (0.4988, 0.0503, 0.9525, 0.0836), 0.6560), | ||
| ("text", (0.8184, 0.1410, 0.9117, 0.1596), 0.5705), | ||
| ("text", (0.0485, 0.1947, 0.3091, 0.3831), 0.5666), | ||
| ("text", (0.4860, 0.1442, 0.5707, 0.1618), 0.5587), | ||
| ("text", (0.2840, 0.9223, 0.9376, 0.9690), 0.5521), | ||
| ("text", (0.0383, 0.7330, 0.1900, 0.7521), 0.4975), | ||
| ("text", (0.6766, 0.1396, 0.7814, 0.1581), 0.4721), | ||
| ("text", (0.0428, 0.9203, 0.2446, 0.9700), 0.3514), | ||
| ("infographic", (0.0232, 0.0000, 1.0000, 1.0000), 0.3491), | ||
| ("text", (0.6796, 0.1390, 0.9124, 0.1589), 0.2593), | ||
| ("text", (0.3659, 0.1422, 0.5756, 0.1616), 0.2331), | ||
| ("title", (0.1100, 0.0428, 0.2808, 0.0997), 0.1624), | ||
| ("text", (0.3518, 0.9236, 0.6853, 0.9438), 0.0883), | ||
| ("title", (0.0507, 0.1556, 0.2132, 0.1785), 0.0789), | ||
| ("text", (0.3650, 0.1420, 0.4239, 0.1591), 0.0483), | ||
| ("text", (0.2650, 0.9539, 0.9348, 0.9678), 0.0327), | ||
| ("text", (0.3159, 0.9225, 0.8857, 0.9467), 0.0219), | ||
| ("text", (0.0480, 0.1986, 0.2085, 0.2467), 0.0167), | ||
| ("text", (0.0493, 0.2986, 0.3085, 0.3813), 0.0165), | ||
| ] | ||
|
|
||
| _TEXT_LABELS = ("text", "title", "header_footer") | ||
|
|
||
|
|
||
| def _detections(): | ||
| return [{"label_name": label, "bbox_xyxy_norm": list(box), "score": score} for label, box, score in _MENU_PAGE] | ||
|
|
||
|
|
||
| def _counts(dets): | ||
| counts = {} | ||
| for det in dets: | ||
| counts[det["label_name"]] = counts.get(det["label_name"], 0) + 1 | ||
| return counts | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("extract_infographics", [False, True]) | ||
| def test_infographic_contained_text_depends_on_infographic_extraction(extract_infographics): | ||
| dets = _apply_page_elements_v3_postprocess(_detections(), extract_infographics=extract_infographics) | ||
| counts = _counts(_apply_final_score_filter(dets)) | ||
| assert counts.get("infographic") == 1 | ||
| text_boxes = sum(counts.get(label, 0) for label in _TEXT_LABELS) | ||
| if extract_infographics: | ||
| assert text_boxes == 0 | ||
| else: | ||
| assert text_boxes > 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("suppress_text_in_infographics", [False, True]) | ||
| def test_table_contained_paragraph_is_removed_either_way(suppress_text_in_infographics): | ||
| labels = ["table", "chart", "title", "infographic", "paragraph", "header_footer"] | ||
| annotations = {label: [] for label in labels} | ||
| annotations["table"] = [[0.2, 0.2, 0.8, 0.6, 0.87]] | ||
| annotations["infographic"] = [[0.1, 0.65, 0.9, 0.95, 0.9]] | ||
| annotations["paragraph"] = [[0.3, 0.3, 0.6, 0.4, 0.93], [0.2, 0.7, 0.8, 0.9, 0.95]] | ||
| result = postprocess_page_elements_v3( | ||
| annotations, labels, suppress_text_in_infographics=suppress_text_in_infographics | ||
| ) | ||
| assert len(result["table"]) == 1 | ||
| assert len(result["paragraph"]) == (0 if suppress_text_in_infographics else 1) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("extract_infographics", [False, True]) | ||
| def test_title_only_infographic_preserves_title_when_extraction_is_off(extract_infographics): | ||
| detections = [ | ||
| {"label_name": "infographic", "bbox_xyxy_norm": [0.1, 0.1, 0.9, 0.9], "score": 0.95}, | ||
| {"label_name": "title", "bbox_xyxy_norm": [0.2, 0.2, 0.5, 0.25], "score": 0.95}, | ||
| ] | ||
| result = _apply_page_elements_v3_postprocess(detections, extract_infographics=extract_infographics) | ||
| counts = _counts(_apply_final_score_filter(result)) | ||
| assert counts.get("infographic") == 1 | ||
| assert counts.get("title", 0) == (0 if extract_infographics else 1) |
Uh oh!
There was an error while loading. Please reload this page.