项目集成paddleocr功能
示例代码下载
参考网站
- https://www.paddlepaddle.org.cn/
- https://github.com/PaddlePaddle/PaddleOCR
- https://fastapi.tiangolo.com/
安装paddle和paddle ocr环境
# 安装paddlepaddle
# https://www.paddlepaddle.org.cn/
pip install paddlepaddle==2.6.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装paddleocr
# 提示,截止至2024年5月2日,需要的python版本为3.11.更高版本会出现安装不上的情况
# 提示,截止至2024年5月2日,paddleocr==2.7.5有bug
# 需要修改的地方
# C:\Python311\Lib\site-packages\paddleocr\paddleocr.py
# 第54行之后
# from ppstructure import predict_system
pip install paddleocr==2.7.5 -i https://mirror.baidu.com/pypi/simple
# 可能出现其他安装失败情况,请使用清华源安装,更新pip工具
pip install --upgrade pip wheel setuptools
安装fast api服务框架
# 安装fastAPI
pip install fastapi -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装web容器
pip install "uvicorn[standard]" -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装from参数表单
pip install python-multipart -i https://pypi.tuna.tsinghua.edu.cn/simple
编写后台代码
import paddle as paddle
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import time
import uvicorn
# 打印paddle版本
print(paddle.__version__)
app = FastAPI()
def get_image_ocr(img_path: str):
# 创建一个OCR实例
ocr = PaddleOCR(
use_angle_cls=True, lang="ch"
) # need to run only once to download and load model into memory
# img_path = "./images/test.png"
# 使用PaddleOCR进行文字检测和识别
ocr_result = ocr.ocr(img_path, cls=True)
# 显示结果
for line in ocr_result:
print(line)
# 对现实结果在原图进行标注
result = ocr_result[0]
image = Image.open(img_path).convert("RGB")
boxes = [elements[0] for elements in result]
# pairs = [elements[1] for elements in ocr_result]
# txts = [pair[0] for pair in pairs]
# scores = [pair[1] for pair in pairs]
im_show = draw_ocr(image, boxes)
im_show = Image.fromarray(im_show)
im_show.save(f"{img_path}")
# im_show.save("./images/result.jpg")
return {"image": img_path, "result": result}
app.mount("/static", StaticFiles(directory="./static"), name="static")
@app.get("/")
def read_root():
html_content = """
<html>
<head>
<title>OCR</title>
</head>
<body>
<h1>OCR</h1>
<p>Upload an image to extract text from it.</p>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
sequence_counter = 1
@app.post("/ocr/")
async def ocr_image(file: UploadFile | None = File(None)):
# 判断文件是否存在
if file is None:
return {"code": 400, "message": "No file found", data: None}
# 生成时间戳保存上传图片以免被覆盖
current_time = int(time.time() * 10000)
unique_sequnce = current_time + sequence_counter
new_fileName = f"./static/{unique_sequnce}--{file.filename}.jpg"
# 将文件写入磁盘
with open(f"{new_fileName}", "wb+") as fsio:
fsio.write(await file.read())
pass
# 调用OCR方法
ocr_result = get_image_ocr(new_fileName)
return {"code": 200, "message": "ok", "data": ocr_result}
# 启动服务
uvicorn.run(app, host="0.0.0.0", port=8000)