一、运行环境安装
https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.5/doc/doc_ch/quickstart.md
paddle安装
PaddlePaddle 2.2.2
Anaconda下python3.8
CUDA-10.1
cudnn-10.2-v7.6.5
GPU版本安装
python -m pip install paddlepaddle-gpu==2.2.2 -i https://mirror.baidu.com/pypi/simple
测试
import paddle
paddle.utils.run_check()
paddleocr安装
pip install “paddleocr>=2.0.1” # 推荐使用2.0.1+版本
亲测可以运行的配置
二、ocr使用
import time
import os, sys
import matplotlib.pyplot as plt
from PIL import Image
from paddleocr import PaddleOCR, draw_ocr
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
folder_dir = os.path.dirname(os.path.realpath(__file__))
start = time.time()
print("-------------- 识别开始 ---------------")
paddleOCR_dir = os.path.join(os.getcwd(), "PaddleOCR")
os.chdir(paddleOCR_dir)
cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/"
det_model_dir = "./inference/ch_ppocr_mobile_v2.0_det_infer/"
rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/"
use_angle_cls = False
use_space_char = True
use_gpu = True
cls = False
ocr = PaddleOCR(cls_model_dir=cls_model_dir, det_model_dir=det_model_dir, rec_model_dir=rec_model_dir,
use_angle_cls=use_angle_cls, use_gpu=use_gpu )
local_path = os.path.join(folder_dir, 'tmp_in')
img_path = local_path + '\card.png'
result = ocr.ocr(img_path, cls=False)
txts = [result[i][1][0] for i in range(len(result))]
scores = [result[i][1][1] for i in range(len(result))]
print(txts)
print(scores)
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
save_path = os.path.join(folder_dir, 'tmp_out',img_path.split("\\")[-1])
print(save_path)
im_show.save(save_path)
end = time.time()
cost_time = round(end - start, 2)
print("-------------- 识别结束,耗时 "+ str(cost_time) +"秒 ---------------")
参考文章
- https://blog.csdn.net/weixin_43271235/article/details/120201521
- https://zhuanlan.zhihu.com/p/479547189
- https://blog.csdn.net/weixin_43220532/article/details/112472706
|