paddleOCR迎来大更新,搞一把新的api接口,直接用起来。
搞容器:
FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
ENV DEBIAN_FRONTEND=noninteractive
# 安装基本软件包
RUN apt-get update && \
apt-get upgrade -y && \
apt-get -y --no-install-recommends install vim wget curl git build-essential python3.10 python3-pip python3.10-venv sudo && \
update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1 && \
apt-get install -y libgl1 libglib2.0-0
# 安装 tzdata 包并设置时区为上海(无交互)
RUN apt-get update && \
apt-get install -y tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
docker run -it --gpus all -p 7860:7860 7df2e9c725a9d865ef29b9f8611ee6a2c640a5eb25b6ff5ad66009f4ed9a0947 bash
搞数据:
apt install unzip -y
wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/ppocr_img.zip
unzip ppocr_img.zip
cd ppocr_img
搞基础环境:
wget http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.20_amd64.deb
sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2.20_amd64.deb
搞conda:
wget http://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
~/miniconda3/bin/conda init
. ~/.bashrc
搞python:
conda create -n py38 python=3.8 -y
conda activate py38
python -m pip install paddlepaddle-gpu==2.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install "paddleocr>=2.0.1" --upgrade PyMuPDF==1.21.1
conda install numpy=1.20 -y
测试:
paddleocr --image_dir ./imgs/11.jpg --use_angle_cls true --use_gpu ture
测试效果:
from paddleocr import PaddleOCR, draw_ocr
# 定义参数
params = {
#'lang': 'ch, en', # 使用中文和英文模型
#'det_model_dir': '/path/to/your/server/det/model/dir', # 指向服务器上的大模型目录
#'rec_model_dir': '/path/to/your/server/rec/model/dir', # 指向服务器上的大模型目录
#cls_model_dir': '/path/to/your/server/cls/model/dir', # 指向服务器上的大模型目录
'use_gpu': True, # 使用 GPU 执行
'use_angle_cls': True # 进行方向判别
}
# 初始化 OCR 实例
ocr = PaddleOCR(**params)
# 读取图片并进行 OCR
img_path = './imgs/11.jpg'
result = ocr.ocr(img_path, cls=True)
print(result )
# python 3.8
pip install fastapi uvicorn python-multipart
# main.py
import os
import cv2
import numpy as np
from fastapi import FastAPI, File, UploadFile
from paddleocr import PaddleOCR, draw_ocr
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# 初始化 FastAPI 应用
app = FastAPI(
title='PadlleOCR API',
description='基于 PaddleOCR 的 OCR 服务 API 接口',
version='1.0.1',
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 定义 OCR 参数
params = {
'use_gpu': True,
'use_angle_cls': True
}
# 初始化 OCR 实例
ocr = PaddleOCR(**params)
@app.post("/image_ocr")
async def upload_image(image: UploadFile = File(...)):
"""
接收上传的图片文件并进行 OCR 处理
"""
try:
img = cv2.imdecode(np.fromstring(image.file.read(), np.uint8), cv2.IMREAD_COLOR)
# 读取保存的图片并进行 OCR
result = ocr.ocr(img, cls=True)
return result
except:
print("Error")
if __name__ == '__main__':
uvicorn.run(f'{os.path.basename(__file__).split(".")[0]}:app',
host='0.0.0.0',
port=7860,
reload=False,
workers=1)
docker commit 77fcb48b6357 kevinchina/deeplearning:paddleocrv4
Dockerfile
FROM kevinchina/deeplearning:paddleocrv4
EXPOSE 7860
ENTRYPOINT /root/miniconda3/envs/py38/bin/python /ppocr_img/main.py
推到云端:
docker push kevinchina/deeplearning:paddleocrv4_api
启动:
docker run -d --gpus all -p 7860:7860 kevinchina/deeplearning:paddleocrv4_api
访问:
127.0.0.1:7860/docs