百度paddleOcr安装与使用

百度paddleOcr安装与使用,第1张

一、运行环境安装
https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.5/doc/doc_ch/quickstart.md#212
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

# 系统路径加入PaddleOCR文件夹, 解决找不到模块问题
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))   # 根据所在文件相对PaddleOCR文件夹层次添加
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) # 切换路径,保证设置参数文件的相对路径正确
# print(os.getcwd())

# --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/"     # 分类模型所在文件夹
cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" 
# --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/"     # 检测模型所在文件夹
det_model_dir = "./inference/ch_ppocr_mobile_v2.0_det_infer/"
# --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/"     # 识别模型所在文件夹
rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" 

# --use_angle_cls=True --use_space_char=True --use_gpu=False
use_angle_cls = False    # 是否加载分类模型
use_space_char = True   # 是否识别空格
use_gpu = True  # 是否使用GPU,若想利用gpu,设置为True
cls = False #   前向时是否启动分类

# 实例化PaddleOCR识别类
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 )  # need to run only once to download and load model into memory
# img_path = '/11.jpg'  # 图片直接放在同级目录
local_path = os.path.join(folder_dir, 'tmp_in') # 若没有tmp_in文件夹,自己建个
img_path = local_path + '\card.png' # 图片名

result = ocr.ocr(img_path, cls=False)   # 最终结果,识别方框4个点坐标,识别文本,置信度组成的列表
# print(result)
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)  # font_path不是必须, font_path='./doc/fonts/simfang.ttf'
im_show = Image.fromarray(im_show)

save_path = os.path.join(folder_dir, 'tmp_out',img_path.split("\")[-1])  # 保存, 若没有tmp_out文件夹,自己建个
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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/917144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存