1.首先要拉取paddleocr的镜像  根据自己的情况选择用不用gpu版本的(我这里选择用了gpu版本的)

docker pull ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.1.0-gpu-cuda11.8-cudnn8.9

2.挂载端口号来自己设置 -v设置自己挂载的目录

docker run -it --gpus all   --name paddleocr   -p 8000:8000   -v /xxx/xxx/paddle_ocr:/workspace   ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddle:3.1.0-gpu-cuda11.8-cudnn8.9   /bin/bash

3.在挂载目录里 拉取paddleocr的项目并且安装所需环境

cd /workspace
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt

后面就能运行啦我是自己创了个py文件

ocr = PaddleOCR(lang="ch", use_angle_cls=False)

def _handle_Image(file_path: str) -> Dict[str, Any]:
    try:
        img = cv2.imread(file_path)
        results = ocr.predict(img)

        data = results[0]

        texts = data.get('rec_texts', [])
        scores = data.get('rec_scores', [])

        text_parts = []
        for text, score in zip(texts, scores):
            if score > 0.8 and text.strip():
                text_parts.append(text)

        clean_text = ''.join(text_parts).strip()

        return {
            "code": 0,
            "msg": "success",
            "result": clean_text
        }
    except Exception as e:
        return {
            "code": 1,
            "msg": f"OCR failed: {str(e)}",
            "result": ""
        }

@app.post("/ocr")
async def ocr_image(file: UploadFile = File(...)):
    suffix = os.path.splitext(file.filename)[-1]
    tmp_path = f"/tmp/{uuid.uuid4().hex}{suffix}"
    try:
        with open(tmp_path, "wb") as f:
            shutil.copyfileobj(file.file, f)
        result = _handle_Image(tmp_path)
        return result
    finally:
        if os.path.exists(tmp_path):
            os.remove(tmp_path)
            
#main.py paddleocr

然后外部进行访问的这个服务器端口号   

小白 制作不容易 喜欢就关注一下点个赞收藏吧!!!

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐