"""
1.新建一个env
需要 python 3.8(版本太高可能paddle会有问题,之前使用python3.10paddle引用其他库出现问题)
conda create -n paddle python=3.8
conda activate paddle
2.安装paddle(2.1和2.2选一)
2.1安装paddle[gpu]版本
python -m pip install paddlepaddle-gpu==3.0.0rc1 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
2.2没有gpu的话安装cpu版本
python -m pip install paddlepaddle==3.0.0b2 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/
3.安装paddleocr
pip install paddleocr
"""
from paddleocr import PaddleOCR
import os
import time
def timeit(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"函数 {func.__name__} 执行时间为 {end_time - start_time} 秒")
return result
return wrapper
@timeit
def ocr_result(img_path):
temp = []
result = ocr.ocr(img_path, det=False, cls=False)
for idx in range(len(result)):
res = result[idx]
for line in res:
_ =list(line)
if len(_) == 2:
temp.append({"result":_[0],"置信度":_[1]})
return temp
print("加载模型")
ocr = PaddleOCR(lang='ch')
print("模型加载完成")
_dir_path = "./source"
for item in os.listdir(_dir_path):
t = os.path.join(_dir_path, item)
res = ocr_result(t)
print({"file":t,"result":res})