微信公众号怎么做网站网页设计模板网站
paddleocr是paddlepaddle专门做ocr的库,我们简单用一下
- 参考 PaddleOCR—图片文字识别提取—快速使用教程_paddleocr使用教程-CSDN博客
目录
1 安装
1.1 前言
1.2 安装paddleocr
1.3 安装paddlepaddle
1.4 安装cuda
1.5 安装cudnn
1.6 配置 zlibwapi.dll
2 基本使用
3 opencv读的图像可以直接扔ocr.ocr()里面
4 针对单行文本的识别
4.1 识别本地图像
4.2 服务
4.3 请求
5 在RDK X3上的适配
6 视觉暂留
7 遇到的问题
7.1 找不到torch的shm.dll
1 安装
1.1 前言
我使用的系统为windows,python版本为python3.7,paddleocr版本为2.7.0.2
我的显卡是GTX970M,估计是硬件问题,后续使用代码的时候如果使用GPU就不能预测出结果,但CPU可以预测出结果。但在更新CUDA后,在任务管理器中可以查看到GPU的使用情况(之前很少)
综上所述下面安装paddlepaddle-gpu版流程是不一定正确的
1.2 安装paddleocr
pip intall paddleocr
1.3 安装paddlepaddle
之后安装paddlepaddle,由于3.7是很早的版本了,所以直接执行 pip install paddlepaddle-gpu 大概率是可以对应上paddleocr的,最终安装paddlepaddle的版本为2.5.2
1.4 安装cuda
paddlepaddle-gpu-2.5.2需要cuda与cudnn,我目前这两个版本都比较落后,所以我们需要安装一下新的
首先找到cuda的安装包 CUDA Toolkit Archive | NVIDIA Developer
这里的Version指的是windows server,一般选最新的就行,感兴趣可以看一下这个 windows server_百度百科
之后你会下载下来这样一个exe
打开
OK
- 这个只是临时存放的路径,如果我们C盘空间不够可以换其他的地方
等
到100%后,等一会儿会出现这个
等
安装完cuda之后,显卡驱动会自动更新到合适的版本
如果按照上面的方式安装的话,默认安装在 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0
安装之后环境变量也自动给你搞好了
确定这里有环境变量之后,我们重新启动一下,重启后执行 nvcc -V 可以显示cuda12.0
1.5 安装cudnn
我们首先进入cudnn的下载地址 Log in | NVIDIA Developer ,首先你需要登录一下
之后需要做个问卷,随便选选就行了,但是你最好每个空都填一填,不然通过不了
同意
更多
进到这个页面,这个页面需要加载一会儿,完全加载完毕后再进行下面的操作
向下滚到这,然后点击
点开之后点这个
之后你会获得这样一个压缩包
解压之后的文件夹内会有这四个文件
把这三个文件夹的东西,手动复制到CUDA的安装目录下
lib会有一个x64的子文件夹,你打开x64,把这些lib文件扔进去
1.6 配置 zlibwapi.dll
- 参考 解决问题:Could not locate zlibwapi.dll. Please make sure it is in your library path!-CSDN博客
我直接用的人家的百度云链接,解压之后会得到这么三个文件
打开dll_x64找到zlibwapi.dll与zlibwapi.lib
将zlibwapi.lib放在CUDA/v12.0/lib/x64中
将zlibwapi.dll放在 CUDA/v12.0/bin 中
2 基本使用
我们准备预测这个图像
在预测前我们需要准备simfang.ttf
simfang.ttf 是仿宋字体,可以在 C:\Windows\Fonts 中找到
之后我们直接使用文档中的代码 doc/doc_ch/quickstart.md · PaddlePaddle/PaddleOCR - Gitee.com
对代码做一些修改
运行后会生成result.jpg
打开后是这样的
3 opencv读的图像可以直接扔ocr.ocr()里面
获得结果后我们做一下数据的处理
from paddleocr import PaddleOCR
import cv2
import numpy as npocr = PaddleOCR(use_angle_cls=True, lang="ch",use_gpu=False)frame = cv2.imread("test.png")
result = ocr.ocr(frame, cls=True)
# print(result)result = result[0]
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]print(boxes)
print(txts)
print(scores)from PIL import ImageFont,ImageDraw,Imageshow_font = ImageFont.truetype("simfang.ttf",15,encoding="utf-8")for i in range(0,len(boxes)):pilimg = Image.fromarray(frame)PIL_draw = ImageDraw.Draw(pilimg)PIL_draw.text((int(boxes[i][0][0]),int(boxes[i][0][1]-20)),txts[i],(0,0,255),font=show_font)frame = cv2.cvtColor(np.array(pilimg),0)points = boxes[i]points = np.array(points,np.int32)points = points.reshape((-1,1,2))frame = cv2.polylines(frame,[points],True,(255,0,0),2)cv2.imshow('frame',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
然后就能得到下面的效果
4 针对单行文本的识别
4.1 识别本地图像
比如验证码,我们先写个demo
from paddleocr import PaddleOCR# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch",use_gpu=False) # need to run only once to download and load model into memory
img_path = './RandCode.png'
result = ocr.ocr(img_path, cls=True)
print()
print(result[0][0][1][0])
我现在想识别这个图像
运行后可以得到结果
4.2 服务
现在每一次预测都实例化一次对象,这个时间比较长,我们简单搞一个服务
4.3 请求
之后我们直接发请求就可以了
经测试车牌也可以检测出来,比如
但必须是单行文本(服务端只做了简单处理,如果处理好一点也没有问题),比如这个就不行
5 在RDK X3上的适配
直接pip。先装paddleocr
再装paddlepaddle
然后直接运行上面识别本地图像的代码,缺的东西会自动下载,可以出结果
- 可能涉及到别人的隐私问题,我把最后一位划掉了
识别的是下面这张图像
6 视觉暂留
视觉暂留就是让上一次预测的结果知道下一次预测结果出来之后再消失,本质上并不能解决帧数低的问题,但是会让低帧数看起来不是一闪一闪的
import queue
import threading
from paddleocr import PaddleOCR
import cv2
import numpy as np
import timeocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=True)frame_queue = queue.Queue(maxsize=1)
boxes_queue = queue.Queue(maxsize=1)
txts_queue = queue.Queue(maxsize=1)cap = cv2.VideoCapture(0)
def video_capture():while cap.isOpened():ret, frame = cap.read()# frame = cv2.resize(frame, (320, 240))if ret:frame_queue.put(frame)cap.release()def predict():while cap.isOpened():try:start_time = time.time()frame = frame_queue.get()result = ocr.ocr(frame, cls=True)result = result[0]boxes = [line[0] for line in result]boxes_queue.put(boxes)txts = [line[1][0] for line in result]txts_queue.put(txts)print(time.time() - start_time)except:passcap.release()from PIL import ImageFont, ImageDraw, Imageshow_font = ImageFont.truetype("simfang.ttf", 30, encoding="utf-8")def draw():txts_list = [['0']]boxes_list = [[[[174.0, 17.0], [252.0, 15.0], [253.0, 38.0], [175.0, 41.0]]]]while cap.isOpened():frame = frame_queue.get()try:boxes = boxes_queue.get(block=False)txts = txts_queue.get(block=False)for i in range(0, len(boxes)):pilimg = Image.fromarray(frame)PIL_draw = ImageDraw.Draw(pilimg)PIL_draw.text((int(boxes[i][0][0]),int(boxes[i][0][1])), txts[i], (0, 0, 255), font=show_font)frame = cv2.cvtColor(np.array(pilimg), 0)points = boxes[i]points = np.array(points,np.int32)points = points.reshape((-1,1,2))frame = cv2.polylines(frame,[points],True,(255,0,0),2)txts_list.append(txts)boxes_list.append(boxes)if len(txts_list)>2:txts_list = txts_list[-2:]boxes_list = boxes_list[-2:]print(txts_list)except:boxes = boxes_list[-1]txts = txts_list[-1]for i in range(0, len(boxes)):pilimg = Image.fromarray(frame)PIL_draw = ImageDraw.Draw(pilimg)PIL_draw.text((int(boxes[i][0][0]),int(boxes[i][0][1])), txts[i], (0, 0, 255), font=show_font)frame = cv2.cvtColor(np.array(pilimg), 0)points = boxes[i]points = np.array(points,np.int32)points = points.reshape((-1,1,2))frame = cv2.polylines(frame,[points],True,(255,0,0),2)cv2.imshow('draw_frame', frame)cv2.waitKey(1)cap.release()threading.Thread(target=video_capture).start()
threading.Thread(target=predict).start()
threading.Thread(target=draw).start()
原理是在 获取图像、预测图像、绘制图像 中的绘制图像线程中,首先初始化初始的预测值
优先取队列中实际预测出来的值,如果画不出来就会报错进入except。如果能画出来那么就添加到上面初始化的列表中,如果列表的元素超过两个就只保留最后的两个
except就是拿最新的画上
7 遇到的问题
7.1 找不到torch的shm.dll
把环境中的torch删了,然后再装不好使。后面给paddlepaddle搞了一个独立的环境就好了
(paddlepaddle) D:\笔记>pip list
Package Version
---------------------- -----------
albucore 0.0.23
albumentations 2.0.5
annotated-types 0.7.0
anyio 4.9.0
astor 0.8.1
beautifulsoup4 4.13.3
certifi 2025.1.31
charset-normalizer 3.4.1
colorama 0.4.6
Cython 3.0.12
decorator 5.2.1
fire 0.7.0
fonttools 4.56.0
h11 0.14.0
httpcore 1.0.7
httpx 0.28.1
idna 3.10
imageio 2.37.0
lazy_loader 0.4
lmdb 1.6.2
lxml 5.3.1
networkx 3.4.2
numpy 2.2.4
opencv-contrib-python 4.11.0.86
opencv-python 4.11.0.86
opencv-python-headless 4.11.0.86
opt-einsum 3.3.0
packaging 24.2
paddleocr 2.10.0
paddlepaddle 3.0.0
pillow 11.1.0
pip 25.0
protobuf 6.30.2
pyclipper 1.3.0.post6
pydantic 2.10.6
pydantic_core 2.27.2
python-docx 1.1.2
PyYAML 6.0.2
RapidFuzz 3.12.2
requests 2.32.3
scikit-image 0.25.2
scipy 1.15.2
setuptools 75.8.0
shapely 2.0.7
simsimd 6.2.1
sniffio 1.3.1
soupsieve 2.6
stringzilla 3.12.3
termcolor 2.5.0
tifffile 2025.3.13
tqdm 4.67.1
typing_extensions 4.13.0
urllib3 2.3.0
wheel 0.45.1
上面是我的环境,我们可以看到实际上是没有用到torch的