-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Fix block coverage #3237
Conversation
There was a problem hiding this 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 |
# 计算每个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 |
There was a problem hiding this comment.
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.
# 计算每个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 |
There was a problem hiding this comment.
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.
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.
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 |
There was a problem hiding this comment.
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.
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.
# 检查每个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的平均分数 |
There was a problem hiding this comment.
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.
# 检查每个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.
# 计算每个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 |
There was a problem hiding this comment.
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.
# 计算每个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面积 |
There was a problem hiding this comment.
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.
# 计算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]) | ||
|
||
# 判断是否符合文本图条件 |
There was a problem hiding this comment.
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.
# 判断是否符合文本图条件 | |
# 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 | ||
|
||
# 根据条件决定添加到哪个列表 |
There was a problem hiding this comment.
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.
# 根据条件决定添加到哪个列表 | |
# 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 |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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.
# 找到与当前block重叠的text spans | |
# Find text spans that overlap with the current block |
Copilot uses AI. Check for mistakes.
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:
After PR: