report-detect/archive/docs/SEAL_SELECTION_FIX.md

185 lines
4.7 KiB
Markdown
Raw Normal View History

chore(project): conservative cleanup - archive temp scripts and old docs Major cleanup to improve project organization and maintainability. Changes: - Moved 34 temp/debug/test scripts to archive/temp_scripts/ - Moved 9 auxiliary tools to archive/tools/ - Moved 3 CRT test scripts to archive/crt_tests/ - Moved 4 OCR test scripts to archive/ocr_tests/ - Moved 14 old documentation files to archive/docs/ - Deleted 4 useless files (duplicates, temp files) Root directory: - Before: 67 files (cluttered) - After: 10 core files (clean and organized) Core files retained: - test_accuracy_batch_full.py (main script) - cma_extraction_template_primary.py (CMA extraction) - cma_extraction_final.py (backup CMA extraction) - CLAUDE.md (project guide) - TEST_ACCURACY_BATCH_README.md (usage guide) - TEST_ACCURACY_BATCH_DEPENDENCIES.md (dependency docs) - CLEANUP_PLAN.md (cleanup plan) - CLEANUP_SUMMARY.md (this file) - IMPLEMENTATION_SUMMARY.md (implementation summary) - requirements.txt (dependencies) Archive structure: archive/ ├── temp_scripts/ (34 files: test_, debug_, analyze_, etc.) ├── tools/ (9 files: find_, show_, visualize_, etc.) ├── crt_tests/ (3 files: CRT extraction tests) ├── ocr_tests/ (4 files: OCR timeout tests) └── docs/ (14 files: old reports and guides) Benefits: ✓ Cleaner root directory - easier navigation ✓ Better organization - clear separation of concerns ✓ Preserved history - all files archived, not deleted ✓ Improved maintainability - easier to find active files ✓ Better git history - removed 198 deleted files from tracking No functional changes - all core functionality preserved. Related: - TEST_ACCURACY_BATCH_DEPENDENCIES.md - dependency analysis - CLEANUP_PLAN.md - detailed cleanup plan Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-03 14:35:06 +08:00
# 印章检测问题修复
## 问题描述
### 3.pdf的处理结果
**预期结果**
- 机构名称:深圳市中安质量检验认证有限公司
**实际结果**
- 机构名称:县市场监督管理局行政审批
### 根本原因
**检测到了错误的印章!**
```
页面布局:
+--------------------------------------------------+
| |
| [CMA标志] |
| |
| 深圳市中安质量检验认证有限公司 |
| (检验机构印章) | ← 应该检测这个
| |
| |
| 县市场监督管理局 |
| 行政审批专用章 | ← 实际检测到这个
| |
+--------------------------------------------------+
```
### 解扭曲工作正常
查看 `seal_unwarp_0.png` 可以确认:
- ✅ 极坐标解扭曲正确
- ✅ OCR正确识别了解扭曲后的图像
- ❌ 但识别的是**行政审批章**,不是检验机构印章
---
## 问题分析
### 之前的问题
用户报告:"已经解扭曲,但是识别出来的不是解扭曲后的内容"
**实际情况**
1. ✅ 解扭曲工作正常
2. ✅ OCR识别了解扭曲后的图像
3. ❌ 但系统检测到了**错误的印章**
### 根本原因
**缺少印章选择逻辑**
```python
# 之前的代码:处理所有检测到的印章
for reg in all_regions:
if label == 'seal':
seal_boxes.append(box) # 添加所有印章,没有过滤
```
系统会检测页面上的所有印章,但没有优先级选择:
- ❌ 行政审批章(错误的印章)
- ❌ 其他政府公章
- ✅ 检验机构印章(正确的印章)
---
## 解决方案
### 添加印章评分和选择机制
**评分标准**
1. **位置评分**60分
- 上半部分center_y < page_h * 0.5+30分
- 右半部分center_x > page_w * 0.5+30分
- **原因**检验机构印章通常在右上角靠近CMA标志
2. **尺寸评分**20分
- 中等尺寸100-300px+20分
- 较小或较大80-100px或300-400px+10分
- **原因**:检验机构印章通常是中等大小的圆形章
3. **形状评分**20分
- 圆形(宽高比 0.8-1.2+20分
- **原因**:检验机构印章通常是圆形的
### 实现代码
```python
# 评分每个印章
scored_seals = []
for idx, box in enumerate(seal_boxes):
# 计算位置评分(优先右上角)
position_score = 0
if center_y < page_h * 0.5: # 上半部分
position_score += 30
if center_x > page_w * 0.5: # 右半部分
position_score += 30
# 计算尺寸评分(优先中等大小)
size_score = 0
if 100 <= min_dim <= 300:
size_score = 20
# 计算形状评分(优先圆形)
aspect_score = 0
if 0.8 <= aspect_ratio <= 1.2:
aspect_score = 20
total_score = position_score + size_score + aspect_score
scored_seals.append({...})
# 选择得分最高的印章
scored_seals.sort(key=lambda x: x['score'], reverse=True)
selected_seals = scored_seals[:min(2, len(scored_seals))]
```
---
## 预期效果
### 修复前
```
检测到印章 #0: 县市场监督管理局行政审批
位置: 左下角 (200, 1500)
识别结果: "县市场监督管理局\n行政审批"
```
### 修复后
```
检测到印章 #0: 县市场监督管理局行政审批
位置: 左下角 (200, 1500)
评分: 10分 (位置=0, 尺寸=10, 形状=0)
检测到印章 #1: 深圳市中安质量检验认证有限公司
位置: 右上角 (1000, 300)
评分: 90分 (位置=60, 尺寸=20, 形状=10)
选择: 印章 #1(得分最高)
识别结果: "深圳市中安质量检验认证有限公司"
```
---
## 修改的文件
**test_accuracy_batch_full.py**第861-927行
- 添加印章评分逻辑
- 添加印章选择逻辑
- 选择得分最高的2个印章进行处理
---
## 关键改进点
1. **位置优先级** - 优先选择右上角的印章靠近CMA标志
2. **尺寸过滤** - 过滤掉太大或太小的印章
3. **形状过滤** - 优先选择圆形印章
4. **Top-K选择** - 选择得分最高的2个印章确保不会遗漏正确的印章
---
## 验证
重新运行测试:
```bash
python test_accuracy_batch_full.py --pdf 3.pdf
```
预期结果:
- 应该检测到右上角的检验机构印章
- 识别结果应该是 "深圳市中安质量检验认证有限公司"
- 相似度应该接近100%
---
**修复已完成!现在系统会优先选择检验机构印章,而不是行政审批章。**