- Removed excessive blank lines in various Python scripts, markdown files, and configuration files to improve overall code clarity and maintainability. - Ensured consistent formatting throughout the project, contributing to a more organized codebase.
385 lines
13 KiB
Python
385 lines
13 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
测试并行OCR性能,对比优化前后的提升
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import time
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
from PIL import Image
|
||
import numpy as np
|
||
|
||
def test_serial_ocr(image_path: str, num_tasks: int = 3):
|
||
"""测试串行OCR(优化前的方式)"""
|
||
print("=" * 70)
|
||
print("测试1: 串行OCR(优化前)")
|
||
print("=" * 70)
|
||
print(f"任务数量: {num_tasks}")
|
||
print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||
print()
|
||
|
||
# 加载图片
|
||
try:
|
||
img = Image.open(image_path)
|
||
if img.mode != 'RGB':
|
||
img = img.convert('RGB')
|
||
img_np = np.array(img)
|
||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] 无法加载图片: {e}")
|
||
return None
|
||
|
||
# 初始化OCR
|
||
try:
|
||
from operations.ocr_operations import _ocr_processor
|
||
_ocr_processor._ensure_ocr()
|
||
|
||
if _ocr_processor.ocr is None:
|
||
print("[ERROR] OCR引擎未初始化")
|
||
return None
|
||
|
||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||
return None
|
||
|
||
# 串行执行OCR任务
|
||
start_time = time.time()
|
||
results = []
|
||
|
||
print("开始串行OCR测试...")
|
||
for i in range(num_tasks):
|
||
task_start = time.time()
|
||
|
||
try:
|
||
if _ocr_processor.ocr_type == "easyocr":
|
||
result = _ocr_processor.ocr.readtext(img_np)
|
||
else:
|
||
result = _ocr_processor.ocr.ocr(img_np)
|
||
|
||
# 提取文本
|
||
from Underlying_Operations import underlying_operations
|
||
ops = underlying_operations()
|
||
text = ops.extract_ocr_content(result)
|
||
|
||
task_elapsed = (time.time() - task_start) * 1000
|
||
results.append(text)
|
||
print(f" 任务 {i+1}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {text[:30] if text else '(空)'}")
|
||
except Exception as e:
|
||
print(f" 任务 {i+1}/{num_tasks}: 失败 - {e}")
|
||
results.append(None)
|
||
|
||
total_elapsed = (time.time() - start_time) * 1000
|
||
|
||
print()
|
||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||
print(f"平均每个任务: {total_elapsed/num_tasks:.0f}ms")
|
||
print()
|
||
|
||
return total_elapsed
|
||
|
||
def test_parallel_ocr(image_path: str, num_tasks: int = 3):
|
||
"""测试并行OCR(优化后的方式)"""
|
||
print("=" * 70)
|
||
print("测试2: 并行OCR(优化后)")
|
||
print("=" * 70)
|
||
print(f"任务数量: {num_tasks}")
|
||
print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||
|
||
# 获取最优线程数
|
||
try:
|
||
from operations.screenshot_service import get_optimal_workers
|
||
optimal_workers = get_optimal_workers()
|
||
print(f"线程池大小: {optimal_workers}")
|
||
except:
|
||
optimal_workers = 6
|
||
print(f"线程池大小: {optimal_workers} (默认)")
|
||
print()
|
||
|
||
# 加载图片
|
||
try:
|
||
img = Image.open(image_path)
|
||
if img.mode != 'RGB':
|
||
img = img.convert('RGB')
|
||
img_np = np.array(img)
|
||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] 无法加载图片: {e}")
|
||
return None
|
||
|
||
# 初始化OCR
|
||
try:
|
||
from operations.ocr_operations import _ocr_processor
|
||
_ocr_processor._ensure_ocr()
|
||
|
||
if _ocr_processor.ocr is None:
|
||
print("[ERROR] OCR引擎未初始化")
|
||
return None
|
||
|
||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||
return None
|
||
|
||
# 定义OCR任务函数
|
||
def ocr_task(task_id):
|
||
"""单个OCR任务"""
|
||
try:
|
||
if _ocr_processor.ocr_type == "easyocr":
|
||
result = _ocr_processor.ocr.readtext(img_np)
|
||
else:
|
||
result = _ocr_processor.ocr.ocr(img_np)
|
||
|
||
# 提取文本
|
||
from Underlying_Operations import underlying_operations
|
||
ops = underlying_operations()
|
||
text = ops.extract_ocr_content(result)
|
||
|
||
return text
|
||
except Exception as e:
|
||
print(f" 任务 {task_id} 失败: {e}")
|
||
return None
|
||
|
||
# 并行执行OCR任务
|
||
start_time = time.time()
|
||
results = []
|
||
|
||
print("开始并行OCR测试...")
|
||
with ThreadPoolExecutor(max_workers=optimal_workers) as executor:
|
||
# 提交所有任务
|
||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||
|
||
# 收集结果
|
||
task_times = {}
|
||
for future in futures:
|
||
task_id = futures[future]
|
||
task_start = time.time()
|
||
try:
|
||
result = future.result()
|
||
task_elapsed = (time.time() - task_start) * 1000
|
||
task_times[task_id] = task_elapsed
|
||
results.append(result)
|
||
print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}")
|
||
except Exception as e:
|
||
print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}")
|
||
results.append(None)
|
||
|
||
total_elapsed = (time.time() - start_time) * 1000
|
||
|
||
print()
|
||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||
if task_times:
|
||
avg_task_time = sum(task_times.values()) / len(task_times)
|
||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||
print(f"并行效率: {avg_task_time * num_tasks / total_elapsed:.1f}x (理想情况下应该接近{num_tasks}x)")
|
||
print()
|
||
|
||
return total_elapsed
|
||
|
||
def test_parallel_ocr_old_config(image_path: str, num_tasks: int = 3):
|
||
"""测试并行OCR(使用旧的配置:OMP_NUM_THREADS=1, max_workers=6)"""
|
||
print("=" * 70)
|
||
print("测试3: 并行OCR(旧配置:OMP=1, 线程池=6)")
|
||
print("=" * 70)
|
||
print(f"任务数量: {num_tasks}")
|
||
|
||
# 临时设置旧配置
|
||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||
os.environ['OMP_NUM_THREADS'] = '1'
|
||
print(f"OMP_NUM_THREADS: 1 (临时设置)")
|
||
print(f"线程池大小: 6 (固定)")
|
||
print()
|
||
|
||
# 加载图片
|
||
try:
|
||
img = Image.open(image_path)
|
||
if img.mode != 'RGB':
|
||
img = img.convert('RGB')
|
||
img_np = np.array(img)
|
||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] 无法加载图片: {e}")
|
||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||
return None
|
||
|
||
# 重新初始化OCR(使用新的OMP设置)
|
||
try:
|
||
from operations.ocr_operations import _ocr_processor
|
||
# 注意:OCR引擎已经初始化,OMP设置可能不会立即生效
|
||
# 但我们可以测试线程池的效果
|
||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||
print("注意: OMP设置可能需要重启程序才能完全生效")
|
||
print()
|
||
except Exception as e:
|
||
print(f"[ERROR] OCR引擎检查失败: {e}")
|
||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||
return None
|
||
|
||
# 定义OCR任务函数
|
||
def ocr_task(task_id):
|
||
"""单个OCR任务"""
|
||
try:
|
||
if _ocr_processor.ocr_type == "easyocr":
|
||
result = _ocr_processor.ocr.readtext(img_np)
|
||
else:
|
||
result = _ocr_processor.ocr.ocr(img_np)
|
||
|
||
# 提取文本
|
||
from Underlying_Operations import underlying_operations
|
||
ops = underlying_operations()
|
||
text = ops.extract_ocr_content(result)
|
||
|
||
return text
|
||
except Exception as e:
|
||
print(f" 任务 {task_id} 失败: {e}")
|
||
return None
|
||
|
||
# 并行执行OCR任务(使用旧的线程池大小)
|
||
start_time = time.time()
|
||
results = []
|
||
|
||
print("开始并行OCR测试(旧配置)...")
|
||
with ThreadPoolExecutor(max_workers=6) as executor:
|
||
# 提交所有任务
|
||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||
|
||
# 收集结果
|
||
task_times = {}
|
||
for future in futures:
|
||
task_id = futures[future]
|
||
task_start = time.time()
|
||
try:
|
||
result = future.result()
|
||
task_elapsed = (time.time() - task_start) * 1000
|
||
task_times[task_id] = task_elapsed
|
||
results.append(result)
|
||
print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}")
|
||
except Exception as e:
|
||
print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}")
|
||
results.append(None)
|
||
|
||
total_elapsed = (time.time() - start_time) * 1000
|
||
|
||
# 恢复OMP设置
|
||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||
|
||
print()
|
||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||
if task_times:
|
||
avg_task_time = sum(task_times.values()) / len(task_times)
|
||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||
print()
|
||
|
||
return total_elapsed
|
||
|
||
def main():
|
||
"""主函数"""
|
||
# 默认图片路径
|
||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||
|
||
# 从命令行参数获取图片路径
|
||
if len(sys.argv) > 1:
|
||
image_path = sys.argv[1]
|
||
else:
|
||
image_path = default_image
|
||
|
||
# 转换为绝对路径
|
||
if not os.path.isabs(image_path):
|
||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||
image_path = os.path.join(project_root, image_path)
|
||
|
||
# 检查文件是否存在
|
||
if not os.path.exists(image_path):
|
||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||
return
|
||
|
||
# 获取任务数量(默认3个,减少测试时间)
|
||
num_tasks = 3
|
||
if len(sys.argv) > 2:
|
||
try:
|
||
num_tasks = int(sys.argv[2])
|
||
except:
|
||
pass
|
||
|
||
print("=" * 70)
|
||
print("并行OCR性能对比测试")
|
||
print("=" * 70)
|
||
print()
|
||
print(f"测试图片: {os.path.basename(image_path)}")
|
||
print(f"任务数量: {num_tasks}")
|
||
print()
|
||
|
||
# 获取当前配置信息
|
||
cpu_count = os.cpu_count() or 4
|
||
omp_threads = os.environ.get('OMP_NUM_THREADS', '未设置')
|
||
print(f"系统信息:")
|
||
print(f" CPU核心数: {cpu_count}")
|
||
print(f" OMP_NUM_THREADS: {omp_threads}")
|
||
try:
|
||
from operations.screenshot_service import get_optimal_workers
|
||
optimal_workers = get_optimal_workers()
|
||
print(f" 推荐线程池大小: {optimal_workers}")
|
||
except:
|
||
pass
|
||
print()
|
||
print()
|
||
|
||
# 测试1: 串行OCR
|
||
serial_time = test_serial_ocr(image_path, num_tasks)
|
||
|
||
print()
|
||
print()
|
||
|
||
# 测试2: 并行OCR(优化后)
|
||
parallel_time = test_parallel_ocr(image_path, num_tasks)
|
||
|
||
print()
|
||
print()
|
||
|
||
# 测试3: 并行OCR(旧配置)
|
||
parallel_old_time = test_parallel_ocr_old_config(image_path, num_tasks)
|
||
|
||
# 性能对比总结
|
||
print()
|
||
print("=" * 70)
|
||
print("性能对比总结")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
if serial_time and parallel_time:
|
||
speedup = serial_time / parallel_time
|
||
print(f"串行OCR: {serial_time:.0f}ms ({serial_time/1000:.2f}秒)")
|
||
print(f"并行OCR: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)")
|
||
print(f"性能提升: {speedup:.2f}x ({(1 - parallel_time/serial_time)*100:.1f}% 时间减少)")
|
||
print()
|
||
|
||
if parallel_old_time and parallel_time:
|
||
speedup_vs_old = parallel_old_time / parallel_time
|
||
print(f"旧配置并行: {parallel_old_time:.0f}ms ({parallel_old_time/1000:.2f}秒)")
|
||
print(f"新配置并行: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)")
|
||
print(f"配置优化提升: {speedup_vs_old:.2f}x ({(1 - parallel_time/parallel_old_time)*100:.1f}% 时间减少)")
|
||
print()
|
||
|
||
if serial_time and parallel_time and parallel_old_time:
|
||
total_speedup = serial_time / parallel_time
|
||
print(f"总体优化效果:")
|
||
print(f" 串行 → 并行: {serial_time/parallel_time:.2f}x")
|
||
print(f" 旧配置 → 新配置: {parallel_old_time/parallel_time:.2f}x")
|
||
print(f" 串行 → 新配置: {total_speedup:.2f}x")
|
||
print()
|
||
|
||
print("=" * 70)
|
||
print("测试完成")
|
||
print("=" * 70)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
|