优化:统一代码风格,调整字典定义和字符串格式,增强可读性
This commit is contained in:
1 parent
3b2aab67bd
commit
4492b77d67
1 file changed
+57
-28
+57
-28
@@ -12,15 +12,22 @@ import psutil
|
||||
import gc
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_memory_usage():
|
||||
"""获取当前内存使用情况"""
|
||||
process = psutil.Process(os.getpid())
|
||||
return process.memory_info().rss / 1024 / 1024 # MB
|
||||
|
||||
|
||||
def test_easyocr_performance():
|
||||
"""测试EasyOCR性能"""
|
||||
print("测试 EasyOCR 性能...")
|
||||
results = {'type': 'EasyOCR', 'init_time': 0, 'recognition_times': [], 'memory_usage': 0}
|
||||
results = {
|
||||
"type": "EasyOCR",
|
||||
"init_time": 0,
|
||||
"recognition_times": [],
|
||||
"memory_usage": 0,
|
||||
}
|
||||
|
||||
try:
|
||||
# 测试初始化
|
||||
@@ -28,14 +35,15 @@ def test_easyocr_performance():
|
||||
start_time = time.time()
|
||||
|
||||
import easyocr
|
||||
reader = easyocr.Reader(['ch_sim', 'en'])
|
||||
|
||||
reader = easyocr.Reader(["ch_sim", "en"])
|
||||
|
||||
init_time = time.time() - start_time
|
||||
memory_after = get_memory_usage()
|
||||
|
||||
results['init_time'] = init_time
|
||||
results['memory_usage'] = memory_after - memory_before
|
||||
results['version'] = easyocr.__version__
|
||||
results["init_time"] = init_time
|
||||
results["memory_usage"] = memory_after - memory_before
|
||||
results["version"] = easyocr.__version__
|
||||
|
||||
print(f" 初始化时间: {init_time:.2f}秒")
|
||||
print(f" 内存增加: {memory_after - memory_before:.2f}MB")
|
||||
@@ -47,9 +55,10 @@ def test_easyocr_performance():
|
||||
if not test_images:
|
||||
# 创建测试图片
|
||||
from PIL import Image, ImageDraw
|
||||
img = Image.new('RGB', (300, 100), color='white')
|
||||
|
||||
img = Image.new("RGB", (300, 100), color="white")
|
||||
draw = ImageDraw.Draw(img)
|
||||
draw.text((10, 30), "测试文本 123.45元", fill='black')
|
||||
draw.text((10, 30), "测试文本 123.45元", fill="black")
|
||||
test_path = pic_dir / "test_ocr.png"
|
||||
test_path.parent.mkdir(exist_ok=True)
|
||||
img.save(test_path)
|
||||
@@ -62,7 +71,7 @@ def test_easyocr_performance():
|
||||
start_time = time.time()
|
||||
results_ocr = reader.readtext(str(img_path))
|
||||
elapsed = time.time() - start_time
|
||||
results['recognition_times'].append(elapsed)
|
||||
results["recognition_times"].append(elapsed)
|
||||
print(f" 第{i+1}次: {elapsed:.2f}秒")
|
||||
|
||||
# 清理
|
||||
@@ -70,15 +79,21 @@ def test_easyocr_performance():
|
||||
gc.collect()
|
||||
|
||||
except Exception as e:
|
||||
results['error'] = str(e)
|
||||
results["error"] = str(e)
|
||||
print(f" [ERROR] {e}")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def test_paddleocr_performance():
|
||||
"""测试PaddleOCR性能"""
|
||||
print("测试 PaddleOCR 性能...")
|
||||
results = {'type': 'PaddleOCR', 'init_time': 0, 'recognition_times': [], 'memory_usage': 0}
|
||||
results = {
|
||||
"type": "PaddleOCR",
|
||||
"init_time": 0,
|
||||
"recognition_times": [],
|
||||
"memory_usage": 0,
|
||||
}
|
||||
|
||||
try:
|
||||
# 测试初始化
|
||||
@@ -86,14 +101,15 @@ def test_paddleocr_performance():
|
||||
start_time = time.time()
|
||||
|
||||
from paddleocr import PaddleOCR
|
||||
ocr = PaddleOCR(use_angle_cls=True, lang='ch')
|
||||
|
||||
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
|
||||
|
||||
init_time = time.time() - start_time
|
||||
memory_after = get_memory_usage()
|
||||
|
||||
results['init_time'] = init_time
|
||||
results['memory_usage'] = memory_after - memory_before
|
||||
results['version'] = getattr(PaddleOCR, '__version__', '3.3.2')
|
||||
results["init_time"] = init_time
|
||||
results["memory_usage"] = memory_after - memory_before
|
||||
results["version"] = getattr(PaddleOCR, "__version__", "3.3.2")
|
||||
|
||||
print(f" 初始化时间: {init_time:.2f}秒")
|
||||
print(f" 内存增加: {memory_after - memory_before:.2f}MB")
|
||||
@@ -105,9 +121,10 @@ def test_paddleocr_performance():
|
||||
if not test_images:
|
||||
# 创建测试图片
|
||||
from PIL import Image, ImageDraw
|
||||
img = Image.new('RGB', (300, 100), color='white')
|
||||
|
||||
img = Image.new("RGB", (300, 100), color="white")
|
||||
draw = ImageDraw.Draw(img)
|
||||
draw.text((10, 30), "测试文本 123.45元", fill='black')
|
||||
draw.text((10, 30), "测试文本 123.45元", fill="black")
|
||||
test_path = pic_dir / "test_ocr.png"
|
||||
test_path.parent.mkdir(exist_ok=True)
|
||||
img.save(test_path)
|
||||
@@ -120,7 +137,7 @@ def test_paddleocr_performance():
|
||||
start_time = time.time()
|
||||
results_ocr = ocr.ocr(str(img_path))
|
||||
elapsed = time.time() - start_time
|
||||
results['recognition_times'].append(elapsed)
|
||||
results["recognition_times"].append(elapsed)
|
||||
print(f" 第{i+1}次: {elapsed:.2f}秒")
|
||||
|
||||
# 清理
|
||||
@@ -128,11 +145,12 @@ def test_paddleocr_performance():
|
||||
gc.collect()
|
||||
|
||||
except Exception as e:
|
||||
results['error'] = str(e)
|
||||
results["error"] = str(e)
|
||||
print(f" [ERROR] {e}")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("OCR引擎性能对比测试")
|
||||
@@ -156,20 +174,22 @@ def main():
|
||||
print("=" * 60)
|
||||
|
||||
def print_stats(name, results):
|
||||
if 'error' in results:
|
||||
if "error" in results:
|
||||
print(f"{name}: 失败 - {results['error']}")
|
||||
return
|
||||
|
||||
init_time = results['init_time']
|
||||
memory = results['memory_usage']
|
||||
times = results['recognition_times']
|
||||
init_time = results["init_time"]
|
||||
memory = results["memory_usage"]
|
||||
times = results["recognition_times"]
|
||||
if times:
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
print(f"{name} (v{results.get('version', 'unknown')}):")
|
||||
print(f" 初始化: {init_time:.2f}秒, 内存: {memory:.1f}MB")
|
||||
print(f" 识别平均: {avg_time:.2f}秒 (范围: {min_time:.2f}-{max_time:.2f}秒)")
|
||||
print(
|
||||
f" 识别平均: {avg_time:.2f}秒 (范围: {min_time:.2f}-{max_time:.2f}秒)"
|
||||
)
|
||||
else:
|
||||
print(f"{name}: 无识别测试数据")
|
||||
|
||||
@@ -179,11 +199,19 @@ def main():
|
||||
print()
|
||||
|
||||
# 推荐
|
||||
if ('error' not in easyocr_results and 'error' not in paddleocr_results and
|
||||
easyocr_results['recognition_times'] and paddleocr_results['recognition_times']):
|
||||
if (
|
||||
"error" not in easyocr_results
|
||||
and "error" not in paddleocr_results
|
||||
and easyocr_results["recognition_times"]
|
||||
and paddleocr_results["recognition_times"]
|
||||
):
|
||||
|
||||
easyocr_avg = sum(easyocr_results['recognition_times']) / len(easyocr_results['recognition_times'])
|
||||
paddleocr_avg = sum(paddleocr_results['recognition_times']) / len(paddleocr_results['recognition_times'])
|
||||
easyocr_avg = sum(easyocr_results["recognition_times"]) / len(
|
||||
easyocr_results["recognition_times"]
|
||||
)
|
||||
paddleocr_avg = sum(paddleocr_results["recognition_times"]) / len(
|
||||
paddleocr_results["recognition_times"]
|
||||
)
|
||||
|
||||
print("推荐:")
|
||||
if easyocr_avg < paddleocr_avg:
|
||||
@@ -197,5 +225,6 @@ def main():
|
||||
|
||||
print("• 当前项目策略 (EasyOCR优先+PaddleOCR备用) 是合理的")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
Reference in new issue
Block a user