PaddleOCR本地部署
构建TestPaddle目录:
TestPaddle/
└── PaddleOCR
├── ocr_server.py
├── ch_PP-OCRv4_det_infer.tar
├── ch_PP-OCRv4_rec_infer.tar
└── 001.jpg
1、安装PaddleOCR
安装 PaddleOCR
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt
2、下载模型文件至本地:
wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar 8m 30s Py py12 huawei@huawei-G5500-V7 07:37:24 下午
wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar
tar -xvf ch_PP-OCRv4_det_infer.tar
tar -xvf ch_PP-OCRv4_rec_infer.tar
3、使用 Flask 构建 REST API 服务:
安装 Flask
pip3 install flask flask-cors
创建 flask_pdOCR_app.py并运行:
from flask import Flask, request, jsonify
from paddleocr import PaddleOCR
import cv2
import numpy as np
import base64
import osapp = Flask(__name__)# 设置本地模型路径
ocr_engine = PaddleOCR(use_angle_cls=True,lang="ch",det_model_dir='./ch_PP-OCRv4_det_infer',rec_model_dir='./ch_PP-OCRv4_rec_infer',cls_model_dir='./cls',use_gpu=True # 如果无 GPU 支持,设为 False
)@app.route('/ocr', methods=['POST'])
def ocr():try:# 获取图像路径data = request.jsonimage_path = data.get('image_path')if not image_path or not os.path.exists(image_path):return jsonify({'code': 400,'message': f'Image path does not exist: {image_path}','data': None})# 读取图像img = cv2.imread(image_path)if img is None:return jsonify({'code': 400,'message': 'Failed to load image.','data': None})# 执行 OCRresult = ocr_engine.ocr(img, cls=True)# 格式化结果formatted_result = []for line in result:if line is not None:for word_info in line:formatted_result.append({'text': word_info[1][0],'confidence': float(word_info[1][1]),'position': word_info[0]})return jsonify({'code': 200,'message': 'success','data': formatted_result})except Exception as e:return jsonify({'code': 500,'message': str(e),'data': None})if __name__ == '__main__':app.run(host='0.0.0.0', port=5001, threaded=True)
打印:
[2025/05/10 17:22:52] ppocr WARNING: The first GPU is used for inference by default, GPU ID: 0
[2025/05/10 17:22:52] ppocr WARNING: The first GPU is used for inference by default, GPU ID: 0
[2025/05/10 17:22:53] ppocr WARNING: The first GPU is used for inference by default, GPU ID: 0* Serving Flask app 'flask_pdOCR_app'* Debug mode: off
[2025-05-10 17:22:54,251] [ INFO] _internal.py:97 - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5001* Running on http://10.42.1.10:5001
[2025-05-10 17:22:54,251] [ INFO] _internal.py:97 - Press CTRL+C to quit
4、测试效果
输入
curl -X POST http://127.0.0.1:5001/ocr -H "Content-Type: application/json" -d '{"image_path": "001.jpg"}'
输出:
{"code":200,"data":[{"confidence":0.9966216087341309,"position":[[6.0,5.0],[211.0,5.0],[211.0,25.0],[6.0,25.0]],"text":"+54-123-456789"}],"message":"success"}
{"code":200,"data":[{"confidence":0.9975392818450928,"position":[[20.0,72.0],[427.0,72.0],[427.0,235.0],[20.0,235.0]],"text":"123"},{"confidence":0.9969568252563477,"position":[[10.0,280.0],[428.0,280.0],[428.0,448.0],[10.0,448.0]],"text":"678"}],"message":"success"}