Skip to content

Fix block coverage #3237

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

Merged
merged 5 commits into from
Aug 1, 2025
Merged

Fix block coverage #3237

merged 5 commits into from
Aug 1, 2025

Conversation

myhloli
Copy link
Collaborator

@myhloli myhloli commented Aug 1, 2025

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Please describe the motivation of this PR and the goal you want to achieve through this PR.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here and update the documentation.

Checklist

Before PR:

  • Pre-commit or other linting tools are used to fix the potential lint issues.
  • Bug fixes are fully covered by unit tests, the case that causes the bug should be added in the unit tests.
  • The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, like docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with those projects.
  • CLA has been signed and all committers have signed the CLA in this PR.

@myhloli myhloli requested a review from Copilot August 1, 2025 07:10
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 1, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors table merging logic and adds a new function to handle overlapping blocks with low confidence scores. The main purpose is to improve block coverage by consolidating overlapping elements and removing redundant low-confidence blocks.

  • Simplified table polygon coordinate assignment using list comprehension
  • Added new function to remove overlapping blocks based on confidence scores
  • Integrated the new overlap removal logic into the main processing pipeline
  • Refactored text image block detection logic for better readability

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 21 comments.

File Description
mineru/utils/model_utils.py Added new overlap removal function and simplified table coordinate assignment
mineru/backend/pipeline/model_json_to_middle_json.py Refactored text image block detection logic with clearer conditional flow

Comment on lines +252 to +263
# 计算每个block的坐标和面积
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))

blocks_to_remove = []

# 检查每个block内部是否有3个及以上的小block
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 计算每个block的坐标和面积
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# 检查每个block内部是否有3个及以上的小block
# Calculate the coordinates and area of each block.
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # Default to 0.5 if the 'score' field is missing.
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# Check if there are three or more smaller blocks inside each block.

Copilot uses AI. Check for mistakes.

xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
score = block.get('score', 0.5) # If the 'score' field is missing, default to 0.5

Copilot uses AI. Check for mistakes.

Comment on lines +258 to +263
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))

blocks_to_remove = []

# 检查每个block内部是否有3个及以上的小block
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# 检查每个block内部是否有3个及以上的小block
score = block.get('score', 0.5) # Default to 0.5 if the 'score' field is missing
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# Check if each block contains three or more smaller blocks inside

Copilot uses AI. Check for mistakes.

Comment on lines +263 to +275
# 检查每个block内部是否有3个及以上的小block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# 查找内部的小block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]

# 如果内部有3个及以上的小block
if len(blocks_inside) >= 3:
# 计算小block的平均分数
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)

# 比较大block的分数和小block的平均分数
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 检查每个block内部是否有3个及以上的小block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# 查找内部的小block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]
# 如果内部有3个及以上的小block
if len(blocks_inside) >= 3:
# 计算小block的平均分数
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)
# 比较大block的分数和小block的平均分数
# Check if there are three or more small blocks inside each block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# Find the small blocks inside the current block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]
# If there are three or more small blocks inside
if len(blocks_inside) >= 3:
# Calculate the average score of the small blocks
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)
# Compare the score of the large block with the average score of the small blocks

Copilot uses AI. Check for mistakes.

Comment on lines +252 to +283
# 计算每个block的坐标和面积
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))

blocks_to_remove = []

# 检查每个block内部是否有3个及以上的小block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# 查找内部的小block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]

# 如果内部有3个及以上的小block
if len(blocks_inside) >= 3:
# 计算小block的平均分数
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)

# 比较大block的分数和小block的平均分数
if score > avg_score:
# 保留大block,扩展其边界
# 首先将所有小block标记为要删除
for j, _, j_block in blocks_inside:
if j_block not in blocks_to_remove:
blocks_to_remove.append(j_block)

# 扩展大block的边界以包含所有小block
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 计算每个block的坐标和面积
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # 如果没有score字段,默认为0.5
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# 检查每个block内部是否有3个及以上的小block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# 查找内部的小block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]
# 如果内部有3个及以上的小block
if len(blocks_inside) >= 3:
# 计算小block的平均分数
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)
# 比较大block的分数和小block的平均分数
if score > avg_score:
# 保留大block,扩展其边界
# 首先将所有小block标记为要删除
for j, _, j_block in blocks_inside:
if j_block not in blocks_to_remove:
blocks_to_remove.append(j_block)
# 扩展大block的边界以包含所有小block
# Calculate the coordinates and area of each block
block_info = []
for block in combined_res_list:
xmin, ymin = int(block['poly'][0]), int(block['poly'][1])
xmax, ymax = int(block['poly'][4]), int(block['poly'][5])
area = (xmax - xmin) * (ymax - ymin)
score = block.get('score', 0.5) # Default to 0.5 if the 'score' field is missing
block_info.append((xmin, ymin, xmax, ymax, area, score, block))
blocks_to_remove = []
# Check if there are three or more small blocks inside each block
for i, (xmin, ymin, xmax, ymax, area, score, block) in enumerate(block_info):
# 查找内部的小block
blocks_inside = [(j, j_score, j_block) for j, (xj_min, yj_min, xj_max, yj_max, j_area, j_score, j_block) in
enumerate(block_info)
if i != j and is_inside(block_info[j], block_info[i], overlap_threshold)]
# If there are three or more small blocks inside
if len(blocks_inside) >= 3:
# Calculate the average score of the small blocks
avg_score = sum(s for _, s, _ in blocks_inside) / len(blocks_inside)
# Compare the score of the large block with the average score of the small blocks
if score > avg_score:
# Keep the large block and expand its boundaries
# First, mark all small blocks for removal
for j, _, j_block in blocks_inside:
if j_block not in blocks_to_remove:
blocks_to_remove.append(j_block)
# Expand the boundaries of the large block to include all small blocks

Copilot uses AI. Check for mistakes.

for span in span_in_block_list
)

# 计算block面积
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 计算block面积
# Calculate the block area

Copilot uses AI. Check for mistakes.

# 计算block面积
block_area = (block['bbox'][2] - block['bbox'][0]) * (block['bbox'][3] - block['bbox'][1])

# 判断是否符合文本图条件
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 判断是否符合文本图条件
# Determine whether the block qualifies as a text block

Copilot uses AI. Check for mistakes.

if block_area > 0 and spans_area / block_area > 0.25:
should_add_to_text_blocks = True

# 根据条件决定添加到哪个列表
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international Contributors.

Suggested change
# 根据条件决定添加到哪个列表
# Decide which list to add the block to based on the conditions

Copilot uses AI. Check for mistakes.


# 根据条件决定添加到哪个列表
if should_add_to_text_blocks:
block.pop('group_id', None) # 移除group_id
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
block.pop('group_id', None) # 移除group_id
block.pop('group_id', None) # Remove the group_id key from the block if it exists

Copilot uses AI. Check for mistakes.

should_add_to_text_blocks = False

if ocr_enable:
# 找到与当前block重叠的text spans
Copy link
Preview

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for better code maintainability and accessibility to international contributors.

Suggested change
# 找到与当前block重叠的text spans
# Find text spans that overlap with the current block

Copilot uses AI. Check for mistakes.

@myhloli myhloli merged commit 4d0b6a0 into opendatalab:dev Aug 1, 2025
1 check passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant