This tutorial will introduce how to use [paddle-lite](https://github.com/PaddlePaddle/Paddle-Lite) to deploy paddleOCR ultra-lightweight Chinese and English detection models on mobile phones.
This tutorial will introduce how to use [Paddle Lite](https://github.com/PaddlePaddle/Paddle-Lite) to deploy paddleOCR ultra-lightweight Chinese and English detection models on mobile phones.
paddle-lite is a lightweight inference engine for PaddlePaddle.
paddle-lite is a lightweight inference engine for PaddlePaddle. It provides efficient inference capabilities for mobile phones and IoTs, and extensively integrates cross-platform hardware to provide lightweight deployment solutions for end-side deployment issues.
It provides efficient inference capabilities for mobile phones and IoTs,
and extensively integrates cross-platform hardware to provide lightweight
deployment solutions for end-side deployment issues.
## 1. Preparation
## 1. Preparation
### 运行准备
- Computer (for Compiling Paddle Lite)
- Computer (for Compiling Paddle Lite)
- Mobile phone (arm7 or arm8)
- Mobile phone (arm7 or arm8)
***Note: PaddleOCR lite deployment currently does not support dynamic graph models, only models saved with static graph. The static branch of PaddleOCR is `develop`.***
### 1.1 Prepare the cross-compilation environment
The cross-compilation environment is used to compile C++ demos of Paddle Lite and PaddleOCR.
Supports multiple development environments.
For the compilation process of different development environments, please refer to the corresponding documents.
note: The above pre-build inference library is compiled from the PaddleLite `release/v2.8` branch. For more information about PaddleLite 2.8, please refer to [link](https://github.com/PaddlePaddle/Paddle-Lite/releases/tag/v2.8).
Note: 1. The above Paddle-Lite library is compiled from the Paddle-Lite 2.8 branch. For more information about Paddle-Lite 2.8, please refer to [link](https://github.com/PaddlePaddle/Paddle-Lite/releases/tag/v2.8).
### 3.2 Compile prebuild library (Recommended)
- 2. [Recommended] Compile Paddle-Lite to get the prediction library. The compilation method of Paddle-Lite is as follows:
The structure of the prediction library is as follows:
Note: When compiling Paddle-Lite to obtain the Paddle-Lite library, you need to turn on the two options `--with_cv=ON --with_extra=ON`, `--arch` means the `arm` version, here is designated as armv8,
More compilation commands refer to the introduction [link](https://paddle-lite.readthedocs.io/zh/latest/user_guides/Compile/Android.html#id2) 。
After directly downloading the Paddle-Lite library and decompressing it, you can get the `inference_lite_lib.android.armv8/` folder, and the Paddle-Lite library obtained by compiling Paddle-Lite is located
| |-- libpaddle_api_light_bundled.a C++ static library
| |-- libpaddle_api_light_bundled.a C++ static library
| `-- libpaddle_light_api_shared.so C++ dynamic library
| `-- libpaddle_light_api_shared.so C++ dynamic library
|-- java Java predict library
|-- java Java library
| |-- jar
| |-- jar
| | `-- PaddlePredictor.jar
| | `-- PaddlePredictor.jar
| |-- so
| |-- so
| | `-- libpaddle_lite_jni.so
| | `-- libpaddle_lite_jni.so
| `-- src
| `-- src
|-- demo C++ and java demo
|-- demo C++ and Java demo
| |-- cxx
| |-- cxx C++ demo
| `-- java
| `-- java Java demo
```
```
## 2 Run
## 4. Inference Model Optimization
### 2.1 Inference Model Optimization
Paddle Lite provides a variety of strategies to automatically optimize the original training model, including quantization, sub-graph fusion, hybrid scheduling, Kernel optimization and so on. In order to make the optimization process more convenient and easy to use, Paddle Lite provide opt tools to automatically complete the optimization steps and output a lightweight, optimal executable model.
Paddle Lite provides a variety of strategies to automatically optimize the original training model, including quantization, sub-graph fusion, hybrid scheduling, Kernel optimization and so on. In order to make the optimization process more convenient and easy to use, Paddle Lite provide opt tools to automatically complete the optimization steps and output a lightweight, optimal executable model.
If you have prepared the model file ending in `.nb`, you can skip this step.
If you have prepared the model file ending in .nb, you can skip this step.
The following table also provides a series of models that can be deployed on mobile phones to recognize Chinese. You can directly download the optimized model.
The following table also provides a series of models that can be deployed on mobile phones to recognize Chinese.
|Version|Introduction|Model size|Detection model|Text Direction model|Recognition model|Paddle-Lite branch|
You can directly download the optimized model.
|---|---|---|---|---|---|---|
|V2.0|extra-lightweight chinese OCR optimized model|7.8M|[download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ch_ppocr_mobile_v2.0_det_infer_nb.nb)|[download lin](https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ch_ppocr_mobile_v2.0_cls_infer_nb.nb)|[download lin](https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ch_ppocr_mobile_v2.0_rec_infer_nb.nb)|v2.8|
| Version | Introduction | Model size | Detection model | Text Direction model | Recognition model | Paddle Lite branch |
If you directly use the model in the above table for deployment, you can skip the following steps and directly read [Section 2.2](#2.2 Run optimized model on Phone).
| --- | --- | --- | --- | --- | --- | --- |
| V1.1 | extra-lightweight chinese OCR optimized model | 8.1M | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_det_opt.nb) | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_cls_opt.nb) | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_rec_opt.nb) | develop |
| [slim] V1.1 | extra-lightweight chinese OCR optimized model | 3.5M | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_det_prune_opt.nb) | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_cls_quant_opt.nb) | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.1_rec_quant_opt.nb) | develop |
| V1.0 | lightweight Chinese OCR optimized model | 8.6M | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.0_det_opt.nb) | - | [Download](https://paddleocr.bj.bcebos.com/20-09-22/mobile/lite/ch_ppocr_mobile_v1.0_rec_opt.nb) | develop |
If the model to be deployed is not in the above table, you need to follow the steps below to obtain the optimized model.
If the model to be deployed is not in the above table, you need to follow the steps below to obtain the optimized model.
The `opt` tool can be obtained by compiling Paddle Lite.
The `opt` tool can be obtained by compiling Paddle Lite.
After the compilation is complete, the opt file is located under build.opt/lite/api/, You can view the operating options and usage of opt in the following ways:
After the compilation is complete, the opt file is located under `build.opt/lite/api/`.
The `opt` can optimize the inference model saved by paddle.io.save_inference_model to get the model that the paddlelite API can use.
The usage of opt is as follows:
```
```
# 【Recommend】V1.1 is better than V1.0. steps for convert V1.1 model to nb file are as follows
cd build.opt/lite/api/
wget https://paddleocr.bj.bcebos.com/20-09-22/mobile-slim/det/ch_ppocr_mobile_v1.1_det_prune_infer.tar && tar xf ch_ppocr_mobile_v1.1_det_prune_infer.tar
./opt
wget https://paddleocr.bj.bcebos.com/20-09-22/mobile-slim/rec/ch_ppocr_mobile_v1.1_rec_quant_infer.tar && tar xf ch_ppocr_mobile_v1.1_rec_quant_infer.tar
|--model_dir|The path of the PaddlePaddle model to be optimized (non-combined form)|
|--model_file|The network structure file path of the PaddlePaddle model (combined form) to be optimized|
|--param_file|The weight file path of the PaddlePaddle model (combined form) to be optimized|
|--optimize_out_type|Output model type, currently supports two types: protobuf and naive_buffer, among which naive_buffer is a more lightweight serialization/deserialization implementation. If you need to perform model prediction on the mobile side, please set this option to naive_buffer. The default is protobuf|
|--optimize_out|The output path of the optimized model|
|--valid_targets|The executable backend of the model, the default is arm. Currently it supports x86, arm, opencl, npu, xpu, multiple backends can be specified at the same time (separated by spaces), and Model Optimize Tool will automatically select the best method. If you need to support Huawei NPU (DaVinci architecture NPU equipped with Kirin 810/990 Soc), it should be set to npu, arm|
|--record_tailoring_info|When using the function of cutting library files according to the model, set this option to true to record the kernel and OP information contained in the optimized model. The default is false|
# or use V1.0 model
`--model_dir` is suitable for the non-combined mode of the model to be optimized, and the inference model of PaddleOCR is the combined mode, that is, the model structure and model parameters are stored in a single file.
wget https://paddleocr.bj.bcebos.com/ch_models/ch_det_mv3_db_infer.tar && tar xf ch_det_mv3_db_infer.tar
wget https://paddleocr.bj.bcebos.com/ch_models/ch_rec_mv3_crnn_infer.tar && tar xf ch_rec_mv3_crnn_infer.tar
The following takes the ultra-lightweight Chinese model of PaddleOCR as an example to introduce the use of the compiled opt file to complete the conversion of the inference model to the Paddle-Lite optimized model
When the above code command is completed, there will be two more files `.nb` in the current directory, which is the converted model file.
```
## 5. Run optimized model on Phone
After the conversion is successful, there will be more files ending with `.nb` in the current directory, which is the successfully converted model file.
1. Prepare an Android phone with arm8. If the compiled prediction library and opt file are armv7, you need an arm7 phone and modify ARM_ABI = arm7 in the Makefile.
<aname="2.2 Run optimized model on Phone"></a>
### 2.2 Run optimized model on Phone
2. Make sure the phone is connected to the computer, open the USB debugging option of the phone, and select the file transfer mode.
Some preparatory work is required first.
1. Prepare an Android phone with arm8. If the compiled prediction library and opt file are armv7, you need an arm7 phone and modify ARM_ABI = arm7 in the Makefile.
2. Make sure the phone is connected to the computer, open the USB debugging option of the phone, and select the file transfer mode.
To install on win, you need to go to Google's Android platform to download the adb package for installation:[link](https://developer.android.com/studio)
Verify whether adb is installed successfully
Verify whether adb is installed successfully
```
adb devices
```
```
$ adb devices
If there is device output, it means the installation is successful。
```
List of devices attached
List of devices attached
744be294 device
744be294 device
```
```
If there is `device` output, it means the installation was successful.
4. Prepare optimized models, prediction library files, test images and dictionary files used.
```
4. Prepare optimized models, prediction library files, test images and dictionary files used.
Prepare the test image, taking PaddleOCR/doc/imgs/11.jpg as an example, copy the image file to the demo/cxx/ocr/debug/ folder. Prepare the model files optimized by the lite opt tool, ch_det_mv3_db_opt.nb, ch_rec_mv3_crnn_opt.nb, and place them under the demo/cxx/ocr/debug/ folder.
Prepare the test image, taking `PaddleOCR/doc/imgs/11.jpg` as an example, copy the image file to the `demo/cxx/ocr/debug/` folder.
Prepare the model files optimized by the lite opt tool, `ch_det_mv3_db_opt.nb, ch_rec_mv3_crnn_opt.nb`,
and place them under the `demo/cxx/ocr/debug/` folder.
The structure of the OCR demo is as follows after the above command is executed:
The structure of the OCR demo is as follows after the above command is executed:
```
```
demo/cxx/ocr/
demo/cxx/ocr/
|-- debug/
|-- debug/
| |--ch_ppocr_mobile_v1.1_det_prune_opt.nb Detection model
| |--ch_ppocr_mobile_v2.0_det_slim_opt.nb Detection model
| |--ch_ppocr_mobile_v1.1_rec_quant_opt.nb Recognition model
| |--ch_ppocr_mobile_v2.0_rec_slim_opt.nb Recognition model
| |--ch_ppocr_mobile_cls_quant_opt.nb Text direction classification model
| |--ch_ppocr_mobile_v2.0_cls_slim_opt.nb Text direction classification model
| |--11.jpg Image for OCR
| |--11.jpg Image for OCR
| |--ppocr_keys_v1.txt Dictionary file
| |--ppocr_keys_v1.txt Dictionary file
| |--libpaddle_light_api_shared.so C++ .so file
| |--libpaddle_light_api_shared.so C++ .so file
| |--config.txt Config file
| |--config.txt Config file
|-- config.txt
|-- config.txt Config file
|-- crnn_process.cc
|-- cls_process.cc Pre-processing and post-processing files for the angle classifier
|-- cls_process.h
|-- crnn_process.cc Pre-processing and post-processing files for the CRNN model
|-- crnn_process.h
|-- crnn_process.h
|-- db_post_process.cc
|-- db_post_process.cc Pre-processing and post-processing files for the DB model
|-- db_post_process.h
|-- db_post_process.h
|-- Makefile
|-- Makefile
|-- ocr_db_crnn.cc
|-- ocr_db_crnn.cc C++ main code
```
```
#### Note:
#### 注意:
1. ppocr_keys_v1.txt is a Chinese dictionary file.
1.`ppocr_keys_v1.txt` is a Chinese dictionary file. If the nb model is used for English recognition or other language recognition, dictionary file should be replaced with a dictionary of the corresponding language. PaddleOCR provides a variety of dictionaries under ppocr/utils/, including:
If the nb model is used for English recognition or other language recognition, dictionary file should be replaced with a dictionary of the corresponding language.
PaddleOCR provides a variety of dictionaries under ppocr/utils/, including:
```
```
dict/french_dict.txt # french
dict/french_dict.txt # french
dict/german_dict.txt # german
dict/german_dict.txt # german
...
@@ -204,7 +223,7 @@ dict/korean_dict.txt # korean
...
@@ -204,7 +223,7 @@ dict/korean_dict.txt # korean
ppocr_keys_v1.txt # chinese
ppocr_keys_v1.txt # chinese
```
```
2.`config.txt` of the detector and classifier, as shown below:
2.`config.txt` of the detector and classifier, as shown below:
```
```
max_side_len 960 # Limit the maximum image height and width to 960
max_side_len 960 # Limit the maximum image height and width to 960
det_db_thresh 0.3 # Used to filter the binarized image of DB prediction, setting 0.-0.3 has no obvious effect on the result
det_db_thresh 0.3 # Used to filter the binarized image of DB prediction, setting 0.-0.3 has no obvious effect on the result
...
@@ -213,19 +232,26 @@ det_db_unclip_ratio 1.6 # Indicates the compactness of the text box, the small
...
@@ -213,19 +232,26 @@ det_db_unclip_ratio 1.6 # Indicates the compactness of the text box, the small
use_direction_classify 0 # Whether to use the direction classifier, 0 means not to use, 1 means to use
use_direction_classify 0 # Whether to use the direction classifier, 0 means not to use, 1 means to use
```
```
5. Run Model on phone
5. Run Model on phone
```
After the above steps are completed, you can use adb to push the file to the phone to run, the steps are as follows:
cd inference_lite_lib.android.armv8/demo/cxx/ocr/
make -j
```
mv ocr_db_crnn ./debug/
# Execute the compilation and get the executable file ocr_db_crnn
adb push debug /data/local/tmp/
# The use of ocr_db_crnn is:
adb shell
# ./ocr_db_crnn Detection model file Orientation classifier model file Recognition model file Test image path Dictionary file path
Q1: What if I want to change the model, do I need to run it again according to the process?
Q1: What if I want to change the model, do I need to run it again according to the process?
A1: If you have performed the above steps, you only need to replace the .nb model file to complete the model replacement.
A1: If you have performed the above steps, you only need to replace the .nb model file to complete the model replacement.
Q2: How to test with another picture?
Q2: How to test with another picture?
A2: Replace the .jpg test image under `./debug` with the image you want to test, and run `adb push` to push new image to the phone.
A2: Replace the .jpg test image under ./debug with the image you want to test, and run adb push to push new image to the phone.
Q3: How to package it into the mobile APP?
Q3: How to package it into the mobile APP?
A3: This demo aims to provide the core algorithm part that can run OCR on mobile phones. Further,
PaddleOCR/deploy/android_demo is an example of encapsulating this demo into a mobile app for reference.
A3: This demo aims to provide the core algorithm part that can run OCR on mobile phones. Further, PaddleOCR/deploy/android_demo is an example of encapsulating this demo into a mobile app for reference.