- 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.
144 lines
4.5 KiB
Python
144 lines
4.5 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_parallel_ocr_quick(image_path: str, num_tasks: int = 3):
|
|
"""快速测试并行OCR"""
|
|
print("=" * 70)
|
|
print("并行OCR性能测试(快速版)")
|
|
print("=" * 70)
|
|
print()
|
|
|
|
# 检查文件
|
|
if not os.path.exists(image_path):
|
|
print(f"[ERROR] 图片文件不存在: {image_path}")
|
|
return None
|
|
|
|
# 加载图片
|
|
try:
|
|
img = Image.open(image_path)
|
|
if img.mode != 'RGB':
|
|
img = img.convert('RGB')
|
|
img_np = np.array(img)
|
|
print(f"图片: {os.path.basename(image_path)}")
|
|
print(f"尺寸: {img.size[0]}x{img.size[1]}")
|
|
print(f"任务数: {num_tasks}")
|
|
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(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
|
|
|
from operations.screenshot_service import get_optimal_workers
|
|
optimal_workers = get_optimal_workers()
|
|
print(f"线程池大小: {optimal_workers}")
|
|
print()
|
|
except Exception as e:
|
|
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
|
return None
|
|
|
|
# 定义OCR任务
|
|
def ocr_task(task_id):
|
|
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:
|
|
return f"错误: {e}"
|
|
|
|
# 执行并行OCR
|
|
print("开始并行OCR测试...")
|
|
start_time = time.time()
|
|
|
|
with ThreadPoolExecutor(max_workers=optimal_workers) as executor:
|
|
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
|
results = []
|
|
|
|
for future in futures:
|
|
task_id = futures[future]
|
|
try:
|
|
result = future.result()
|
|
results.append(result)
|
|
print(f" 任务 {task_id}: 完成, 结果: {result[:30] if result else '(空)'}")
|
|
except Exception as e:
|
|
print(f" 任务 {task_id}: 失败 - {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()
|
|
|
|
# 估算串行时间(基于单次OCR耗时)
|
|
print("性能估算:")
|
|
print("-" * 70)
|
|
single_task_time = total_elapsed / num_tasks
|
|
estimated_serial_time = single_task_time * num_tasks
|
|
speedup = estimated_serial_time / total_elapsed if total_elapsed > 0 else 0
|
|
|
|
print(f"估算串行时间: {estimated_serial_time:.0f}ms ({estimated_serial_time/1000:.2f}秒)")
|
|
print(f"实际并行时间: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
|
print(f"性能提升: {speedup:.2f}x (节省 {(1 - total_elapsed/estimated_serial_time)*100:.1f}% 时间)")
|
|
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)
|
|
|
|
num_tasks = 3
|
|
if len(sys.argv) > 2:
|
|
try:
|
|
num_tasks = int(sys.argv[2])
|
|
except:
|
|
pass
|
|
|
|
test_parallel_ocr_quick(image_path, num_tasks)
|
|
|
|
print("=" * 70)
|
|
print("测试完成")
|
|
print("=" * 70)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
|