Huawei cloud GPU server deploys PaddleOCR Chinese and English Identification Service

preface

Recently, the OCR service was used in the company's project. At the beginning, the general character recognition interface on Baidu cloud was used. Later, I learned that the PaddleOCR module of Baidu's open-source flying plasma platform can be used directly. So I built a CPU version OCR recognition service on the company's server. Due to the performance of the company's server and the CPU version, the performance failed to meet the requirements, Therefore, we purchased the GPU server of Huawei cloud to deploy the OCR service of the GPU version.

Please refer to the following article for how to build a GPU version of paddepaddle environment on the Huawei cloud server: https://blog.csdn.net/loutengyuan/article/details/126527326

Install PaddleOCR

Paddlepad OCR is Baidu's open-source OCR character recognition service based on paddlepad. Github address: https://github.com/PaddlePaddle/PaddleOCR/
After installation, enter the following command. After installation, you can experience the OCR recognition service on the command line

pip install "paddleocr>=2.0." -i https://mirror.baidu.com/pypi/simple

Download a test picture experience package:

wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/ppocr_img.zip # Download
unzip ppocr_img.zip #decompression

Enter the directory just unzipped and start to experience OCR recognition

cd ppocr_img #Enter the picture directory you just downloaded and unzipped

Perform OCR recognition

paddleocr --image_dir ./imgs/11.jpg --use_angle_cls true --use_gpu true

There may be an error during execution. Here, you need to install two libx11 and libXext

yum install -y libX11 
yum install -y libXext

In this way, it is recognized. Use the GPU server to identify the ultra fast

Deploy OCR identification API service

In the case of deployment, paddle provides two modes: PaddleHub and paddleserving. It is most convenient to use PaddleHub. It can be executed directly from the command line. Paddle Serving deployment is more stable and supports c + + compilation deployment.
Here, we will talk about the deployment of PaddleHub and Paddle Serving python (the recommended deployment is Paddle Serving)

PaddleHub deploys OCR recognition API

Install PaddleHub

pip install --upgrade paddlehub -i https://mirror.baidu.com/pypi/simple

Start API service

# Foreground start
hub serving start -m ch_pp-ocrv3 --use_gpu   #The OCR model will be automatically downloaded
# Background start
nohup hub serving start -m ch_pp-ocrv3 --use_gpu > log.log 2>&1 &

Thus, an OCR recognition service is started

View progress

ps -ef|grep python

Close process

kill -9 19913

view log

tail -f 1000 log.log

How to view port occupancy

$: netstat -anp | grep 8888
tcp        0      0 127.0.0.1:8888          0.0.0.0:*               LISTEN      13404/python3       
tcp        0      1 172.17.0.10:34036       115.42.35.84:8888       SYN_SENT    14586/python3 

Forced kill process: through pid

$: kill -9 13404
$: kill -9 14586
$: netstat -anp | grep 8888
$:
  • Postman test request

When identifying, the image needs to be converted to base64, and then json requests to submit parameters

Request address: http://127.0.0.1:8866/predict/ch_pp-ocrv3
Request method: json
Request parameters: {"images": ["the base64 content of the image does not need the previous Base64 ID]}

  • Python test script
import requests
import json
import cv2
import base64

def cv2_to_base64(image):
    data = cv2.imencode('.jpg', image)[1]
    return base64.b64encode(data.tostring()).decode('utf8')

# Send HTTP request
data = {'images':[cv2_to_base64(cv2.imread("Your picture address"))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/ch_pp-ocrv3"
r = requests.post(url=url, headers=headers, data=json.dumps(data))

# Print forecast results
print(r.json())

  • curl test request
curl --location --request POST 'http://127.0.0.1:8866/predict/ch_pp-ocrv3' \
--header 'Content-Type: application/json' \
--data-raw '{"images":["Pictorial base64 Content, no previous base64 identification"]}'

Result return:

{"msg":[{"data":[{"confidence":0.9314630627632141,"text":"(paddle env][root@M-1-2-centos","text_box_position":[[6,10],[231,10],[231,23],[6,23]]},{"confidence":0.9092367887496948,"text":"ppocr imgl# hub serving start -m chpp-ocrv3 --use_gpu","text_box_position":[[227,10],[612,12],[612,25],[227,23]]},{"confidence":0.939938485622406,"text":"[2022-05-30 19:49:34 +0800]","text_box_position":[[5,25],[191,25],[191,38],[5,38]]},{"confidence":0.8236835598945618,"text":"[28320]","text_box_position":[[200,26],[246,26],[246,37],[200,37]]},{"confidence":0.6653339862823486,"text":"LINFO]","text_box_position":[[256,26],[295,26],[295,37],[256,37]]},{"confidence":0.842379093170166,"text":"starting gunicorn 2o.1.0","text_box_position":[[301,26],[474,26],[474,38],[301,38]]},{"confidence":0.939938485622406,"text":"[2022-05-30 19:49:34 +0800]","text_box_position":[[5,40],[191,40],[191,53],[5,53]]},{"confidence":0.8367705345153809,"text":"[28320]","text_box_position":[[200,41],[247,41],[247,52],[200,52]]},{"confidence":0.86468505859375,"text":"[INFO]","text_box_position":[[257,41],[297,41],[297,52],[257,52]]},{"confidence":0.9211856722831726,"text":"Listening at: http://0.0.0.0:8866 (28320)","text_box_position":[[302,40],[589,40],[589,53],[302,53]]},{"confidence":0.9346868395805359,"text":"[2022-05-3019:49:34+0800]","text_box_position":[[4,55],[191,54],[191,67],[4,68]]},{"confidence":0.9421297311782837,"text":"[28320]","text_box_position":[[199,55],[247,55],[247,68],[199,68]]},{"confidence":0.9394086003303528,"text":"[INFO]","text_box_position":[[256,55],[298,55],[298,68],[256,68]]},{"confidence":0.9321832656860352,"text":"Using worker: sync","text_box_position":[[302,56],[430,56],[430,68],[302,68]]},{"confidence":0.9334865808486938,"text":"[2022-05-30 19:49:34 +0800]","text_box_position":[[4,70],[191,70],[191,83],[4,83]]},{"confidence":0.8994974493980408,"text":"[INFO] ","text_box_position":[[256,70],[305,70],[305,84],[256,84]]},{"confidence":0.8855429887771606,"text":"[28324]","text_box_position":[[200,71],[246,71],[246,82],[200,82]]},{"confidence":0.9438435435295105,"text":"Booting worker with pid: 28324","text_box_position":[[300,70],[515,69],[515,83],[300,84]]}],"save_path":""}],"results":"","status":"000"}

In this way, it can be used normally. The recognition speed is about 100-300ms, which is very fast.
Official deployment tutorial: https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.5/deploy/hubserving/readme.md

Deploy OCR identification service through pad serving

Compared with hubserving deployment, PaddleServing has the following advantages:

  • Support high concurrency and efficient communication between client and server
  • Support industrial level service capabilities, such as model management, online loading, online A/B testing, etc
  • Support multiple programming languages to develop clients, such as C++, Python and Java

First prepare the PaddleOCR environment. It will be very slow to pull the github code here. You can download it scientifically and then upload it to the server

git clone https://github.com/PaddlePaddle/PaddleOCR --depth=1

Go to working directory

cd PaddleOCR/deploy/pdserving/

Install the running environment of PaddleServing. The steps are as follows:

# Install serving to start the service
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_server_gpu-0.8.3.post102-py3-none-any.whl
pip3 install paddle_serving_server_gpu-0.8.3.post102-py3-none-any.whl

# Install the client, which is used to send requests to the service
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_client-0.8.3-cp38-none-any.whl
pip3 install paddle_serving_client-0.8.3-cp38-none-any.whl

# Install serving app
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_app-0.8.3-py3-none-any.whl
pip3 install paddle_serving_app-0.8.3-py3-none-any.whl
  • Model conversion

When using paddle serving for service-oriented deployment, you need to convert the saved reference model into a model that is easy to deploy.

First, download the reference model of PP-OCR

# Download and unzip OCR text detection model
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar -O ch_PP-OCRv3_det_infer.tar && tar -xf ch_PP-OCRv3_det_infer.tar
# Download and unzip OCR text recognition model
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar -O ch_PP-OCRv3_rec_infer.tar &&  tar -xf ch_PP-OCRv3_rec_infer.tar

Next, use the installed pad_ serving_ The client converts the downloaded reference model into a model format that is easy for server deployment.

# Transform detection model
python3 -m paddle_serving_client.convert --dirname ./ch_PP-OCRv3_det_infer/ \
                                         --model_filename inference.pdmodel          \
                                         --params_filename inference.pdiparams       \
                                         --serving_server ./ppocr_det_v3_serving/ \
                                         --serving_client ./ppocr_det_v3_client/

# Transform recognition model
python3 -m paddle_serving_client.convert --dirname ./ch_PP-OCRv3_rec_infer/ \
                                         --model_filename inference.pdmodel          \
                                         --params_filename inference.pdiparams       \
                                         --serving_server ./ppocr_rec_v3_serving/  \
                                         --serving_client ./ppocr_rec_v3_client/

After the detection model conversion is completed, ppocr will be added to the current folder_ det_ v3_ Serving and ppocr_ det_ v3_ The client folder has the following format:

|- ppocr_det_v3_serving/
  |- __model__  
  |- __params__
  |- serving_server_conf.prototxt  
  |- serving_server_conf.stream.prototxt

|- ppocr_det_v3_client
  |- serving_client_conf.prototxt  
  |- serving_client_conf.stream.prototxt
  • Pad serving pipeline deployment
# Start the service and save the operation log in log.txt
nohup python3 -u web_service.py > log.log 2>&1 &


If there is no error in the operation log, the startup is successful

Then you can use the client code provided by him to request a test

View progress

ps -ef|grep python

Close process

kill -9 19913

view log

tail -f 1000 log.log

How to view port occupancy

$: netstat -anp | grep 8888
tcp        0      0 127.0.0.1:8888          0.0.0.0:*               LISTEN      13404/python3       
tcp        0      1 172.17.0.10:34036       115.42.35.84:8888       SYN_SENT    14586/python3 

Forced kill process: through pid

$: kill -9 13404
$: kill -9 14586
$: netstat -anp | grep 8888
$:
  • HTTP request test

Request address: http://127.0.0.1:9998/ocr/prediction
Request method: json
Request parameters: {"key": "image", "value": "base64 of image"}

  • curl request test
curl --location --request POST 'http://127.0.0.1:9998/ocr/prediction' \

--header 'Content-Type: application/json' \

--data-raw '{"key":"image","value":"Pictorial base64"}'

Returned data format:

{
"err_no": 0,
"err_msg": "",
"key": [
    "result"
],
"value": [
    "[[('(padde env)[root@M-1-2-centoppocrimghub servingstart -m chpp-ocrv3--usegpu', 0.84638405), [[5.0, 10.0], [611.0, 12.0], [611.0, 24.0], [5.0, 22.0]]], [('[2022-05-3019:49:34+0800][28320][INF0]Startingqunicorm20.1.', 0.81580645), [[5.0, 25.0], [472.0, 25.0], [472.0, 38.0], [5.0, 37.0]]], [('[2022-05-3019:49:34+0800][28320][INF0]Listeningat: http://0.0.0.0:8866(28320)', 0.84695405), [[5.0, 40.0], [589.0, 40.0], [589.0, 54.0], [5.0, 54.0]]], [('[2022-05-319:49:34+0800][28320][INF0]Usingworker:sync', 0.7949861), [[5.0, 54.0], [430.0, 56.0], [430.0, 68.0], [5.0, 66.0]]], [('[2022-05-319:49:34+080][28324][INFO]Bootingworkerwith pid:28324', 0.85473406), [[4.0, 70.0], [515.0, 70.0], [515.0, 84.0], [4.0, 84.0]]]]"
],
"tensors": []
}

This is done. It can be used normally. If you provide calls in other languages, you can view their official documents
Official deployment tutorial: https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.5/deploy/pdserving/README_CN.md

epilogue

The deployment is relatively simple, and the official documents of Paddle are quite complete. However, for some special character recognition, you need to customize the training yourself.

Tags: server Huawei Cloud

Posted by dpearcepng on Fri, 26 Aug 2022 01:04:21 +0530