chore: Clean up blank lines across multiple files to enhance readability
- 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.
This commit is contained in:
1 parent
ace3bbfa04
commit
07c387751d
62 files changed
+2432
-1734
No files matched your search
@@ -0,0 +1,365 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
真实场景并行OCR性能对比测试
|
||||
模拟实际使用中的多区域并行OCR识别
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
||||
def create_test_regions(full_image_path):
|
||||
"""从完整截图中创建多个测试区域(模拟真实OCR场景)"""
|
||||
try:
|
||||
img = Image.open(full_image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
|
||||
width, height = img.size
|
||||
|
||||
# 判断是否是完整截图(1080x2400)还是裁剪区域
|
||||
is_full_screenshot = width >= 1000 and height >= 2000
|
||||
|
||||
if is_full_screenshot:
|
||||
# 完整截图:创建多个测试区域
|
||||
# 模拟真实的OCR区域(基于福袋详情页的常见区域)
|
||||
regions = {
|
||||
"福袋倒计时_分钟": (400, 1050, 500, 1150), # 倒计时分钟区域
|
||||
"福袋倒计时_秒": (480, 1110, 570, 1230), # 倒计时秒区域(用户提供的区域)
|
||||
"福袋内容": (50, 800, 500, 1000), # 福袋内容区域
|
||||
"福袋价值": (200, 1300, 400, 1400), # 价值区域
|
||||
"任务列表": (50, 600, 500, 750), # 任务列表区域
|
||||
"参与按钮": (300, 1500, 800, 1600), # 按钮状态区域
|
||||
}
|
||||
else:
|
||||
# 裁剪区域:使用当前图片作为测试区域之一,创建其他相似大小的区域
|
||||
# 从文件名提取坐标信息(如果存在)
|
||||
coords_from_filename = None
|
||||
if "(480,1110)-(570,1230)" in full_image_path:
|
||||
coords_from_filename = (480, 1110, 570, 1230)
|
||||
|
||||
# 创建多个相似大小的测试区域
|
||||
# 如果图片较小,创建多个相同大小的区域用于测试
|
||||
# 这些区域会在完整截图的合理位置
|
||||
regions = {}
|
||||
|
||||
# 使用当前图片作为第一个区域
|
||||
if coords_from_filename:
|
||||
x1, y1, x2, y2 = coords_from_filename
|
||||
regions["福袋倒计时_秒"] = (x1, y1, x2, y2)
|
||||
# 创建其他相邻区域
|
||||
offset_x = x2 - x1
|
||||
offset_y = y2 - y1
|
||||
regions["福袋倒计时_分钟"] = (x1 - offset_x - 20, y1, x1 - 20, y2)
|
||||
regions["福袋内容"] = (x1, y1 - offset_y - 20, x2, y1 - 20)
|
||||
regions["福袋价值"] = (x1, y2 + 20, x2, y2 + offset_y + 20)
|
||||
else:
|
||||
# 如果无法从文件名提取坐标,使用图片本身作为唯一测试区域
|
||||
# 创建多个相同大小的区域(通过复制当前图片)
|
||||
regions["区域1"] = (0, 0, width, height)
|
||||
regions["区域2"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域3"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域4"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域5"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域6"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
|
||||
# 裁剪并保存区域图片
|
||||
region_images = {}
|
||||
temp_dir = tempfile.gettempdir()
|
||||
|
||||
for name, (x1, y1, x2, y2) in regions.items():
|
||||
try:
|
||||
# 确保坐标在图片范围内
|
||||
x1 = max(0, min(x1, img.width))
|
||||
y1 = max(0, min(y1, img.height))
|
||||
x2 = max(x1, min(x2, img.width))
|
||||
y2 = max(y1, min(y2, img.height))
|
||||
|
||||
if x2 > x1 and y2 > y1:
|
||||
crop_img = img.crop((x1, y1, x2, y2))
|
||||
temp_path = os.path.join(temp_dir, f"ocr_test_{name}.png")
|
||||
crop_img.save(temp_path)
|
||||
region_images[name] = {
|
||||
'path': temp_path,
|
||||
'size': crop_img.size,
|
||||
'coords': (x1, y1, x2, y2)
|
||||
}
|
||||
except (ValueError, OSError, IOError) as e:
|
||||
print(f"[WARNING] 无法裁剪区域 {name}: {e}")
|
||||
|
||||
return region_images, img.size
|
||||
except (OSError, IOError, ValueError) as e:
|
||||
print(f"[ERROR] 无法处理图片: {e}")
|
||||
return {}, None
|
||||
|
||||
def ocr_single_region(image_path, region_name, ocr_processor):
|
||||
"""对单个区域进行OCR识别"""
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
if ocr_processor.ocr_type == "easyocr":
|
||||
result = ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
return {
|
||||
'region': region_name,
|
||||
'text': text,
|
||||
'time': elapsed,
|
||||
'success': True
|
||||
}
|
||||
except (OSError, IOError, ValueError, AttributeError) as e:
|
||||
return {
|
||||
'region': region_name,
|
||||
'text': '',
|
||||
'time': 0,
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
def test_parallel_ocr_real(region_images, config_name, omp_threads, max_workers):
|
||||
"""测试真实场景的并行OCR"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f"测试: {config_name}")
|
||||
print(f"{'='*70}")
|
||||
print(f"配置: OMP_NUM_THREADS={omp_threads}, max_workers={max_workers}")
|
||||
print(f"区域数量: {len(region_images)}")
|
||||
print()
|
||||
|
||||
# 设置OMP线程数
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = str(omp_threads)
|
||||
|
||||
# 初始化OCR(使用新的OMP设置)
|
||||
try:
|
||||
from operations.ocr_operations import OCRProcessor
|
||||
ocr_processor = OCRProcessor()
|
||||
# 访问受保护方法以初始化OCR引擎(测试代码中需要)
|
||||
# 使用 getattr 来避免 pylance 警告
|
||||
ensure_ocr = getattr(ocr_processor, '_ensure_ocr')
|
||||
ensure_ocr()
|
||||
|
||||
if ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {ocr_processor.ocr_type}")
|
||||
print()
|
||||
except (ImportError, AttributeError, RuntimeError) as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 显示区域信息
|
||||
print("OCR区域信息:")
|
||||
for name, info in region_images.items():
|
||||
print(f" {name}: {info['size'][0]}x{info['size'][1]} 像素, 坐标: {info['coords']}")
|
||||
print()
|
||||
|
||||
# 并行执行OCR
|
||||
print("开始并行OCR识别...")
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
def ocr_task(name, path):
|
||||
return ocr_single_region(path, name, ocr_processor)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
# 提交所有任务
|
||||
futures = {
|
||||
executor.submit(ocr_task, name, info['path']): name
|
||||
for name, info in region_images.items()
|
||||
}
|
||||
|
||||
# 收集结果
|
||||
for future in futures:
|
||||
region_name = futures[future]
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
status = "✓" if result['success'] else "✗"
|
||||
print(f" {status} {region_name}: {result['time']:.0f}ms, 结果: {result['text'][:30] if result['text'] else '(空)'}")
|
||||
except (RuntimeError, AttributeError, KeyError) as e:
|
||||
print(f" ✗ {region_name}: 失败 - {e}")
|
||||
results.append({
|
||||
'region': region_name,
|
||||
'text': '',
|
||||
'time': 0,
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
})
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 恢复OMP设置
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
# 统计结果
|
||||
successful_results = [r for r in results if r['success']]
|
||||
total_task_time = sum(r['time'] for r in successful_results)
|
||||
avg_task_time = total_task_time / len(successful_results) if successful_results else 0
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"任务总耗时: {total_task_time:.0f}ms")
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
if total_task_time > 0:
|
||||
efficiency = total_task_time / total_elapsed
|
||||
print(f"并行效率: {efficiency:.2f}x (理想值: {len(region_images):.1f}x)")
|
||||
print()
|
||||
|
||||
return {
|
||||
'total_time': total_elapsed,
|
||||
'task_times': [r['time'] for r in successful_results],
|
||||
'results': results,
|
||||
'config': config_name
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
print("=" * 70)
|
||||
print("真实场景并行OCR性能对比测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(f"测试图片: {os.path.basename(image_path)}")
|
||||
print()
|
||||
|
||||
# 创建测试区域
|
||||
print("准备测试区域...")
|
||||
region_images, full_size = create_test_regions(image_path)
|
||||
|
||||
if not region_images:
|
||||
print("[ERROR] 无法创建测试区域")
|
||||
return
|
||||
|
||||
if full_size is not None:
|
||||
print(f"完整截图尺寸: {full_size[0]}x{full_size[1]}")
|
||||
else:
|
||||
print("完整截图尺寸: 未知")
|
||||
print(f"成功创建 {len(region_images)} 个测试区域")
|
||||
print()
|
||||
|
||||
# 获取系统信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
print("系统信息:")
|
||||
print(f" CPU核心数: {cpu_count}")
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f" 推荐线程池大小: {optimal_workers}")
|
||||
except (ImportError, AttributeError):
|
||||
optimal_workers = 6
|
||||
print()
|
||||
|
||||
# 测试1: 优化前配置(OMP=1, workers=6)
|
||||
result_old = test_parallel_ocr_real(
|
||||
region_images,
|
||||
"优化前配置",
|
||||
omp_threads=1,
|
||||
max_workers=6
|
||||
)
|
||||
|
||||
# 等待一下,让系统稳定
|
||||
time.sleep(1)
|
||||
|
||||
# 测试2: 优化后配置(OMP=4, workers=10)
|
||||
result_new = test_parallel_ocr_real(
|
||||
region_images,
|
||||
"优化后配置",
|
||||
omp_threads=4,
|
||||
max_workers=optimal_workers
|
||||
)
|
||||
|
||||
# 性能对比总结
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("性能对比总结")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if result_old and result_new:
|
||||
old_time = result_old['total_time']
|
||||
new_time = result_new['total_time']
|
||||
speedup = old_time / new_time if new_time > 0 else 0
|
||||
|
||||
print(f"优化前总耗时: {old_time:.0f}ms ({old_time/1000:.2f}秒)")
|
||||
print(f"优化后总耗时: {new_time:.0f}ms ({new_time/1000:.2f}秒)")
|
||||
print(f"性能提升: {speedup:.2f}x")
|
||||
print(f"时间减少: {(1 - new_time/old_time)*100:.1f}%")
|
||||
print()
|
||||
|
||||
# 详细对比
|
||||
if result_old['task_times'] and result_new['task_times']:
|
||||
old_avg = sum(result_old['task_times']) / len(result_old['task_times'])
|
||||
new_avg = sum(result_new['task_times']) / len(result_new['task_times'])
|
||||
|
||||
print("任务级性能对比:")
|
||||
print(f" 优化前平均任务耗时: {old_avg:.0f}ms")
|
||||
print(f" 优化后平均任务耗时: {new_avg:.0f}ms")
|
||||
print(f" 单任务性能提升: {old_avg/new_avg:.2f}x")
|
||||
print()
|
||||
|
||||
# 识别结果对比
|
||||
print("识别结果对比:")
|
||||
for old_result in result_old['results']:
|
||||
region_name = old_result['region']
|
||||
new_result = next((r for r in result_new['results'] if r['region'] == region_name), None)
|
||||
|
||||
if old_result['success'] and new_result and new_result['success']:
|
||||
old_text = old_result['text'][:20] if old_result['text'] else '(空)'
|
||||
new_text = new_result['text'][:20] if new_result['text'] else '(空)'
|
||||
match = "✓" if old_text == new_text else "✗"
|
||||
print(f" {match} {region_name}: 结果一致")
|
||||
|
||||
# 清理临时文件
|
||||
print()
|
||||
print("清理临时文件...")
|
||||
for info in region_images.values():
|
||||
try:
|
||||
if os.path.exists(info['path']):
|
||||
os.remove(info['path'])
|
||||
except (OSError, PermissionError):
|
||||
pass
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in new issue
Block a user