- python 3.7 cuda10.2 cudnn 7.6
- 创建虚拟环境 ocr python3.7
- 安装paddlepaddle
conda install paddlepaddle-gpu==2.4.2 cudatoolkit=10.2 --channel [https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/](https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/)
- 先安装 PyWavelets
- 安装paddleocr
pip install paddleocr -i https://mirror.baidu.com/pypi/simple
- 安装shapely
pip install -i [https://pypi.tuna.tsinghua.edu.cn/simple](https://pypi.tuna.tsinghua.edu.cn/simple) --trusted-host pypi.tuna.tsinghua.edu.cn --user pyzmq==19.0.2
import paddle
import paddleocr
import os
from paddleocr import PaddleOCR
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
from PIL import Image
import glob
import random
import re
import json
print(paddle.__version__)
#2.4.1
print(paddleocr.__version__)
#2.6.1.3
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
SUPPORT_DET_MODEL = ['DB']
VERSION = '2.6.1.0'
SUPPORT_REC_MODEL = ['CRNN', 'SVTR_LCNet']
BASE_DIR = os.path.expanduser("~/.paddleocr/")
DEFAULT_OCR_MODEL_VERSION = 'PP-OCRv3'
ocr = PaddleOCR(use_angles_cls=True, use_gpu=False)
def draw_img(img_path,boxes):
save_root = 'data/resocr/'
img_name = img_path.split('\\')[1]
img = cv2.imread(img_path)
for box in boxes:
box = np.reshape(np.array(box),[-1,1,2]).astype(np.int64)
img = cv2.polylines(np.array(img), [box], True, (255,0,0),2)
plt.figure(figsize=(10,10))
save_file = save_root+img_name
plt.imshow(img)
plt.savefig(save_file)
imgp = 'data\\test.jpg'
print(ocr.args)
res = ocr.ocr(imgp)
print(res)
boxes = []
texts = []
for j in range(len(res[0])):
boxes.append(res[0][j][0])
texts.append(res[0][j][1][0])
draw_img(imgp,boxes)
import cv2
from paddleocr import PaddleOCR, draw_ocr
# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
img_path = './data/test.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
print(line)
# 显示结果
from PIL import Image
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='./fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
成功输出!
遇到的问题
下载whl文件
pillow版本过高 更换版本为8.3.2
删除对应虚拟环境下G:\Anaconda\envs\ocr\Library\bin libiomp5md.dll文件
- if scores is not None and (scores[i] < drop_score or TypeError: ‘<’ not supported between instances of ‘tuple’ and ‘float’
更换paddleocr的版本,最新版本是2.6,V2.6版本调用的ocr.ocr返回的部署一个数组,是一个字符串,需要进行转换 pip install paddleocr==2.5.0.3 -i [https://mirror.baidu.com/pypi/simple](https://mirror.baidu.com/pypi/simple)
参考链接
测试代码都是直接copy的两位大佬的,我纯新手小白!下面附大佬原文链接
https://zhuanlan.zhihu.com/p/523972865
https://www.rstk.cn/news/32313.html?action=onClick