|
|
@@ -1,3 +1,5 @@
|
|
|
+import sys
|
|
|
+import time
|
|
|
import re
|
|
|
import difflib
|
|
|
import json
|
|
|
@@ -359,44 +361,45 @@ class OCRResultComparator:
|
|
|
f.write(f"`{diff['file2_value'][:50]}{'...' if len(diff['file2_value']) > 50 else ''}` | ")
|
|
|
f.write(f"{diff['description']} |\n")
|
|
|
|
|
|
-def main():
|
|
|
- """主函数"""
|
|
|
- parser = argparse.ArgumentParser(description='OCR结果对比工具')
|
|
|
- parser.add_argument('file1', help='第一个OCR结果文件路径')
|
|
|
- parser.add_argument('file2', help='第二个OCR结果文件路径')
|
|
|
- parser.add_argument('-o', '--output', default='comparison_report',
|
|
|
- help='输出文件名(不含扩展名)')
|
|
|
- parser.add_argument('-f', '--format', choices=['json', 'markdown', 'both'],
|
|
|
- default='markdown', help='输出格式: json, markdown, 或 both')
|
|
|
- parser.add_argument('--ignore-images', action='store_true',
|
|
|
- help='忽略图片内容(默认已启用)')
|
|
|
+def compare_ocr_results(file1_path: str, file2_path: str, output_file: str = "comparison_report",
|
|
|
+ output_format: str = "markdown", ignore_images: bool = True):
|
|
|
+ """
|
|
|
+ 比较两个OCR结果文件
|
|
|
|
|
|
- args = parser.parse_args()
|
|
|
+ Args:
|
|
|
+ file1_path: 第一个OCR结果文件路径
|
|
|
+ file2_path: 第二个OCR结果文件路径
|
|
|
+ output_file: 输出文件名(不含扩展名),默认为"comparison_report"
|
|
|
+ output_format: 输出格式,选项: 'json', 'markdown', 'both',默认为'markdown'
|
|
|
+ ignore_images: 是否忽略图片内容,默认为True
|
|
|
|
|
|
+ Returns:
|
|
|
+ Dict: 比较结果字典
|
|
|
+ """
|
|
|
comparator = OCRResultComparator()
|
|
|
|
|
|
print("🔍 开始对比OCR结果...")
|
|
|
- print(f"📄 文件1: {args.file1}")
|
|
|
- print(f"📄 文件2: {args.file2}")
|
|
|
- print(f"📁 输出格式: {args.format}")
|
|
|
- print(f"🖼️ 图片处理: {'忽略' if args.ignore_images else '对比'}")
|
|
|
+ print(f"📄 文件1: {file1_path}")
|
|
|
+ print(f"📄 文件2: {file2_path}")
|
|
|
+ print(f"📁 输出格式: {output_format}")
|
|
|
+ print(f"🖼️ 图片处理: {'忽略' if ignore_images else '对比'}")
|
|
|
|
|
|
try:
|
|
|
# 执行比较
|
|
|
- result = comparator.compare_files(args.file1, args.file2)
|
|
|
+ result = comparator.compare_files(file1_path, file2_path)
|
|
|
|
|
|
# 添加时间戳
|
|
|
import datetime
|
|
|
result['timestamp'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
# 生成报告
|
|
|
- if args.format in ['json', 'both']:
|
|
|
- json_file = f"{args.output}.json"
|
|
|
+ if output_format in ['json', 'both']:
|
|
|
+ json_file = f"{output_file}.json"
|
|
|
comparator.generate_json_report(result, json_file)
|
|
|
print(f"📄 JSON报告已保存至: {json_file}")
|
|
|
|
|
|
- if args.format in ['markdown', 'both']:
|
|
|
- md_file = f"{args.output}.md"
|
|
|
+ if output_format in ['markdown', 'both']:
|
|
|
+ md_file = f"{output_file}.md"
|
|
|
comparator.generate_markdown_report(result, md_file)
|
|
|
print(f"📝 Markdown报告已保存至: {md_file}")
|
|
|
|
|
|
@@ -416,23 +419,84 @@ def main():
|
|
|
print(f" 文件2: '{diff['file2_value'][:50]}{'...' if len(diff['file2_value']) > 50 else ''}'")
|
|
|
else:
|
|
|
print(f"\n🎉 恭喜!两个文件内容完全一致!")
|
|
|
+
|
|
|
+ # 添加处理统计信息(模仿 ocr_by_vlm.py 的风格)
|
|
|
+ print("\n📊 对比处理统计")
|
|
|
+ print(f" 文件1路径: {result['file1_path']}")
|
|
|
+ print(f" 文件2路径: {result['file2_path']}")
|
|
|
+ print(f" 输出文件: {output_file}")
|
|
|
+ print(f" 输出格式: {output_format}")
|
|
|
+ print(f" 忽略图片: {ignore_images}")
|
|
|
+ print(f" 处理时间: {result['timestamp']}")
|
|
|
+ print(f" 文件1表格数: {result['file1_tables']}")
|
|
|
+ print(f" 文件2表格数: {result['file2_tables']}")
|
|
|
+ print(f" 文件1段落数: {result['file1_paragraphs']}")
|
|
|
+ print(f" 文件2段落数: {result['file2_paragraphs']}")
|
|
|
+
|
|
|
+ return result
|
|
|
|
|
|
except Exception as e:
|
|
|
- print(f"❌ 对比过程中出现错误: {e}")
|
|
|
- return 1
|
|
|
+ import traceback
|
|
|
+ traceback.print_exc()
|
|
|
+ raise Exception(f"OCR对比任务失败: {e}")
|
|
|
+
|
|
|
+def main():
|
|
|
+ """主函数 - 保持向后兼容"""
|
|
|
+ parser = argparse.ArgumentParser(description='OCR结果对比工具')
|
|
|
+ parser.add_argument('file1', help='第一个OCR结果文件路径')
|
|
|
+ parser.add_argument('file2', help='第二个OCR结果文件路径')
|
|
|
+ parser.add_argument('-o', '--output', default='comparison_report',
|
|
|
+ help='输出文件名(不含扩展名)')
|
|
|
+ parser.add_argument('-f', '--format', choices=['json', 'markdown', 'both'],
|
|
|
+ default='markdown', help='输出格式: json, markdown, 或 both')
|
|
|
+ parser.add_argument('--ignore-images', action='store_true',
|
|
|
+ help='忽略图片内容(默认已启用)')
|
|
|
|
|
|
- return 0
|
|
|
+ args = parser.parse_args()
|
|
|
+
|
|
|
+ try:
|
|
|
+ result = compare_ocr_results(
|
|
|
+ file1_path=args.file1,
|
|
|
+ file2_path=args.file2,
|
|
|
+ output_file=args.output,
|
|
|
+ output_format=args.format,
|
|
|
+ ignore_images=args.ignore_images
|
|
|
+ )
|
|
|
+ print("\n🎉 OCR对比完成!")
|
|
|
+ return 0
|
|
|
+ except Exception as e:
|
|
|
+ print(f"❌ OCR对比失败: {e}")
|
|
|
+ return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- # 如果sys.argv没有被传入参数,则提供默认参数用于测试
|
|
|
- import sys
|
|
|
- import time
|
|
|
- if len(sys.argv) == 1:
|
|
|
- sys.argv.extend([
|
|
|
- # './output/至远彩色印刷工业有限公司-2022年母公司_2.md', './sample_data/demo_54fa7ad0_page_1_nohf.md',
|
|
|
- './output/至远彩色印刷工业有限公司-2022年母公司_2.md', './output/至远彩色印刷工业有限公司-2022年母公司_2-GLM4.5V.md',
|
|
|
- '-o', f'./output/comparison_result_{time.strftime("%Y%m%d_%H%M%S")}',
|
|
|
- '-f', 'both',
|
|
|
- '--ignore-images'])
|
|
|
+ parser = argparse.ArgumentParser(description='OCR结果对比工具')
|
|
|
+ parser.add_argument('file1', nargs= '?', help='第一个OCR结果文件路径')
|
|
|
+ parser.add_argument('file2', nargs= '?', help='第二个OCR结果文件路径')
|
|
|
+ parser.add_argument('-o', '--output', default='comparison_report',
|
|
|
+ help='输出文件名(不含扩展名)')
|
|
|
+ parser.add_argument('-f', '--format', choices=['json', 'markdown', 'both'],
|
|
|
+ default='markdown', help='输出格式: json, markdown, 或 both')
|
|
|
+ parser.add_argument('--ignore-images', action='store_true',
|
|
|
+ help='忽略图片内容(默认已启用)')
|
|
|
+
|
|
|
+ args = parser.parse_args()
|
|
|
|
|
|
- exit(main())
|
|
|
+ if args.file1 and args.file2:
|
|
|
+ result = compare_ocr_results(
|
|
|
+ file1_path=args.file1,
|
|
|
+ file2_path=args.file2,
|
|
|
+ output_file=args.output,
|
|
|
+ output_format=args.format,
|
|
|
+ ignore_images=args.ignore_images
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ # 如果sys.argv没有被传入参数,则提供默认参数用于测试
|
|
|
+ result = compare_ocr_results(
|
|
|
+ file1_path='./output/dots.ocr/至远彩色印刷工业有限公司-2022年母公司_2.md',
|
|
|
+ file2_path='./output/Qwen2.5-VL-72B-Instruct-AWQ/至远彩色印刷工业有限公司-2022年母公司_2.md',
|
|
|
+ output_file=f'./output/comparison_result_{time.strftime("%Y%m%d_%H%M%S")}',
|
|
|
+ output_format='both',
|
|
|
+ ignore_images=True
|
|
|
+ )
|
|
|
+ print("\n🎉 OCR对比完成!")
|
|
|
+
|