cnocr的安装使用
docker环境
- 由于docker hub无法正常访问,导致cnocr的docker镜像无法拉取,所以只能自己做一个docker镜像
- Dockerfile如下
FROM python:3.8
RUN pip install cnocr[ort-cpu] -i https://pypi.tuna.tsinghua.edu.cn/simple
- 原cnocr中使用的是阿里云镜像,但是实测下载速度超慢,所以切换为https://pypi.tuna.tsinghua.edu.cn/simple 清华镜像源
- 下载完成后,使用docker build -t cnocr:qinghua . 生成docker镜像
- 使用docker run -it -p 8501:8501 cnocr:qinghua bash 启动镜像
- 此时还需要导入cnocr已经训练好的模型数据,可直接从百度云盘下载cnocr模型百度网盘链接,当然也可以使用cnocr在线操作手册去找
- 下载完毕后,需要将模型zip压缩文件导入镜像中的/root/.cnocr/2.3目录下
- docker环境准备完毕
运行操作
- 第一次运行时会缺少很多python模块,所以按命令 cnocr serve -p 8501 运行
1.libGL.so.1错误
ImportError: libGL.so.1: cannot open shared object file: No such file or dir
需要安装opencv-python-headless模块
pip uninstall opencv-python -y
pip install opencv-python-headless -i https://pypi.tuna.tsinghua.edu.cn/simple
2.uvicorn错误
FileNotFoundError: [Errno 2] No such file or directory: 'uvicorn'
需要安装uvicorn模块
pip install uvicorn -i https://pypi.tuna.tsinghua.edu.cn/simple
3.pydantic错误
FileNotFoundError: [Errno 2] No such file or directory: 'uvicorn'
需要安装pydantic模块
pip install pydantic -i https://pypi.tuna.tsinghua.edu.cn/simple
4.fastapi错误
FileNotFoundError: [Errno 2] No such file or directory: 'fastapi'
需要安装fastapi模块
pip install fastapi -i https://pypi.tuna.tsinghua.edu.cn/simple
5.cnocr错误
FileNotFoundError: [Errno 2] No such file or directory: 'cnocr'
需要安装cnocr模块,切记此时不要安装cnocr[serve],否则会出现一堆其他问题,安装了这个之后,建议重新卸载opencv-python模块及cnocr模块,重新安装
pip install cnocr -i https://pypi.tuna.tsinghua.edu.cn/simple
6.其他的问题就只能搜索解决了
-
问题都解决之后,就可以后台运行了
-
nohup cnocr serve -p 8501 > /home/cnocr.log 2>&1 &
-
运行原理就是如下文件
/usr/local/bin/cnocr 调用 /usr/local/lib/python3.8/site-packages/cnocr/cli.py cli方法,然后调用/usr/local/lib/python3.8/site-packages/cnocr/serve.py 启动FastAPI服务 -
serve.py只使用了基础模型,可以手动修改serve.py以方便外界调用切换模型
外界调用
- curl -F image=@1_副.jpg http://0.0.0.0:8501/ocr
- 上面的图片名称1_副.jpg就在当前目录下,@是为了将其转换为需要传输的数据格式类型