背景
rapidocr-onnxruntime是一个跨平台的ocr库,基于onnxruntime推理框架。
目前已知运行速度最快、支持最广,完全开源免费并支持离线快速部署的多平台多语言ocr。
缘起:百度paddle工程化不是太好,为了方便大家在各种端上进行ocr推理,
我们将它转换为onnx格式,使用python/c++/java/swift/c# 将它移植到各个平台。
名称来源: 轻快好省并智能。基于深度学习技术的ocr技术,主打人工智能优势及小模型,以速度为使命,效果为主导。
pip install rapidocr-onnxruntime
注:onnxruntime是微软推出的一款推理框架,用户可以非常便利的用其运行一个onnx模型。onnxruntime支持多种运行后端包括cpu,gpu,tensorrt,dml等。可以说onnxruntime是对onnx模型最原生的支持。
1、单独离线部署rapidocr,可以参考这个博客文档:
rapidocr-onnxruntime实现离线文字识别(依赖库) https://www.52pojie.cn/thread-1834278-1-1.html
2、本文重点是阐述在本地大语言模型提供图形化界面的开源项目库open webui 中pdf文件(使用ocr做图像处理)的应用。
open-webui上传pdf 图像处理 (使用 ocr)
在open webui安装运行时,默认pdf文件是只支持转文本部分,不使用ocr做图像处理。 即下图中的该项功能是关闭状态。
在我们的实验中(linux环境,centos系统),我们尝试打开该项功能,即对于pdf中图像包含的文字,能够通过ocr识别软件做处理。
但是,当我们把上图开关启动(使用ocr)后, 在open-webui对话框上传pdf文件,会报如下错误:something went wrong :/ `rapidocr-onnxruntime` package not found, please install it with `pip install rapidocr-onnxruntime`。
查看了相关资料,解决过程如下:
1、直接运行安装命令:pip install rapidocr-onnxruntime
未能按照,给出如下提示,pip在‘root’权限下,不允许安装,建议虚拟环境安装。
warning: running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. it is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
于是,参考pip虚拟环境设置: https://docs.python.org/3/tutorial/venv.html
成功运行,命令如下:
(open-webui) [root@master ~]# python -m venv tutorial-env
(open-webui) [root@master ~]# source tutorial-env/bin/activate
(tutorial-env) (open-webui) [root@master ~]# pip install rapidocr-onnxruntime
installing collected packages: pyclipper, mpmath, flatbuffers, sympy, six, pyyaml, protobuf, pillow, packaging, numpy, humanfriendly, shapely, opencv-python, coloredlogs, onnxruntime, rapidocr-onnxruntime
successfully installed pillow-10.3.0 pyyaml-6.0.1 shapely-2.0.4 coloredlogs-15.0.1 flatbuffers-24.3.25 humanfriendly-10.0 mpmath-1.3.0 numpy-2.0.0 onnxruntime-1.18.0 opencv-python-4.10.0.84 packaging-24.1 protobuf-5.27.1 pyclipper-1.3.0.post5 rapidocr-onnxruntime-1.3.22 six-1.16.0 sympy-1.12.1
虽然rapidocr安装成功了,重启open-webui服务,发现仍然无法上传pdf文件。
查看资料并且单独在python环境测试导入rapidocr库,发现会报错:
importerror: libgl.so.1: cannot open shared object file: no such file or directory
(open-webui) [root@master backend]# python
python 3.10.14 (main, may 6 2024, 19:42:50) [gcc 11.2.0] on linux
type "help", "copyright", "credits" or "license" for more information.
>>> from rapidocr_onnxruntime import rapidocr
traceback (most recent call last):
file "<stdin>", line 1, in <module>
file "/root/miniconda3/envs/open-webui/lib/python3.10/site-packages/rapidocr_onnxruntime/__init__.py", line 4, in <module>
from .main import rapidocr
file "/root/miniconda3/envs/open-webui/lib/python3.10/site-packages/rapidocr_onnxruntime/main.py", line 8, in <module>
import cv2
file "/root/miniconda3/envs/open-webui/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
file "/root/miniconda3/envs/open-webui/lib/python3.10/site-packages/cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
file "/root/miniconda3/envs/open-webui/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
importerror: libgl.so.1: cannot open shared object file: no such file or directory
解决方案:sudo yum install mesa-libgl
(open-webui) [root@master backend]# sudo yum install mesa-libgl
成功安装后。
再次启动运行webui,可以正确上传解析pdf文件。
大概测试了一下, rapidocr库,对于pdf文本格式(印刷体)转换成文本的准确率较高(95%+),但是对于手写文字(来自几位医生),识别准确率稍差(约60%)。
参考资料:
bug: [rag] v0.1.109 breaks pdf upload #1069
issue: pypdf extract_image=true is not working on docker(production) #15576
发表评论