Paddleocr的部署(Linux基于docker容器的方式)
·
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
然后外部进行访问的这个服务器端口号
小白 制作不容易 喜欢就关注一下点个赞收藏吧!!!
更多推荐
所有评论(0)