add deploy lite demo

release/1.1
LDOUBLEV 5 years ago
parent 6f456775b0
commit 85706e16ac

1
.gitignore vendored

@ -18,3 +18,4 @@ output/
*.idea
*.log
.clang-format

@ -0,0 +1,61 @@
ARM_ABI = arm8
export ARM_ABI
include ../Makefile.def
LITE_ROOT=../../../
THIRD_PARTY_DIR=${LITE_ROOT}/third_party
OPENCV_VERSION=opencv4.1.0
OPENCV_LIBS = ../../../third_party/${OPENCV_VERSION}/arm64-v8a/libs/libopencv_imgcodecs.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/libs/libopencv_imgproc.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/libs/libopencv_core.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/libtegra_hal.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/liblibjpeg-turbo.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/liblibwebp.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/liblibpng.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/liblibjasper.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/liblibtiff.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/libIlmImf.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/libtbb.a \
../../../third_party/${OPENCV_VERSION}/arm64-v8a/3rdparty/libs/libcpufeatures.a
OPENCV_INCLUDE = -I../../../third_party/${OPENCV_VERSION}/arm64-v8a/include
CXX_INCLUDES = $(INCLUDES) ${OPENCV_INCLUDE} -I$(LITE_ROOT)/cxx/include
CXX_LIBS = ${OPENCV_LIBS} -L$(LITE_ROOT)/cxx/lib/ -lpaddle_light_api_shared $(SYSTEM_LIBS)
###############################################################
# How to use one of static libaray: #
# `libpaddle_api_full_bundled.a` #
# `libpaddle_api_light_bundled.a` #
###############################################################
# Note: default use lite's shared library. #
###############################################################
# 1. Comment above line using `libpaddle_light_api_shared.so`
# 2. Undo comment below line using `libpaddle_api_light_bundled.a`
#CXX_LIBS = $(LITE_ROOT)/cxx/lib/libpaddle_api_light_bundled.a $(SYSTEM_LIBS)
ocr_db_crnn: fetch_opencv ocr_db_crnn.o
$(CC) $(SYSROOT_LINK) $(CXXFLAGS_LINK) ocr_db_crnn.o -o ocr_db_crnn $(CXX_LIBS) $(LDFLAGS)
ocr_db_crnn.o: ocr_db_crnn.cc
$(CC) $(SYSROOT_COMPLILE) $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -o ocr_db_crnn.o -c ocr_db_crnn.cc
fetch_opencv:
@ test -d ${THIRD_PARTY_DIR} || mkdir ${THIRD_PARTY_DIR}
@ test -e ${THIRD_PARTY_DIR}/${OPENCV_VERSION}.tar.gz || \
(echo "fetch opencv libs" && \
wget -P ${THIRD_PARTY_DIR} https://paddle-inference-dist.bj.bcebos.com/${OPENCV_VERSION}.tar.gz)
@ test -d ${THIRD_PARTY_DIR}/${OPENCV_VERSION} || \
tar -zxvf ${THIRD_PARTY_DIR}/${OPENCV_VERSION}.tar.gz -C ${THIRD_PARTY_DIR}
.PHONY: clean
clean:
rm -f ocr_db_crnn.o
rm -f ocr_db_crnnn

File diff suppressed because it is too large Load Diff

@ -0,0 +1,162 @@
# PaddleOCR 移动端部署
本教程介绍如何在移动端部署PaddleOCR超轻量中文检测、识别模型。
## 运行准备
- 电脑编译Paddle-Lite
- 安卓手机armv7或armv8
## 1. 准备环境
### 1.1 准备交叉编译环境
交叉编译环境用于编译Paddle-Lite和PaddleOCR的C++ demo。
支持多种开发环境,不同开发环境的编译流程请参考对应文档。:
1. [Docker](https://paddle-lite.readthedocs.io/zh/latest/user_guides/source_compile.html#docker)
2. [Linux](https://paddle-lite.readthedocs.io/zh/latest/user_guides/source_compile.html#android)
3. [MAC OS](https://paddle-lite.readthedocs.io/zh/latest/user_guides/source_compile.html#id13)
4. [Windows](https://paddle-lite.readthedocs.io/zh/latest/demo_guides/x86.html#windows)
### 1.2 准备预编译库
预编译库有两种获取方式:
- 1. 直接下载,下载[链接](https://paddle-lite.readthedocs.io/zh/latest/user_guides/release_lib.html#android-toolchain-gcc).
注意选择with_extra=ONwith_cv=ON的下载链接。
- 2. 编译Paddle-Lite得到Paddle-Lite的编译方式如下
```
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite
./lite/tools/build_android.sh --arch=armv8 --with_cv=ON --with_extra=ON
```
注意编译Paddle-Lite获得预编译库时需要打开--with_cv=ON --with_extra=ON两个选项--arch表示arm版本这里指定为armv8
更多编译命令
介绍请参考[链接](https://paddle-lite.readthedocs.io/zh/latest/user_guides/Compile/Android.html#id2)。
直接下载预编译库并解压后,可以得到'inference_lite_lib.android.armv8/'文件夹通过编译Paddle-Lite得到的预编译库位于
'Paddle-Lite/build.lite.android.armv8.gcc/inference_lite_lib.android.armv8/'文件夹下。
预编译库的文件目录如下:
```
inference_lite_lib.android.armv8/
|-- cxx C++ 预测库和头文件
| |-- include C++ 头文件
| | |-- paddle_api.h
| | |-- paddle_image_preprocess.h
| | |-- paddle_lite_factory_helper.h
| | |-- paddle_place.h
| | |-- paddle_use_kernels.h
| | |-- paddle_use_ops.h
| | `-- paddle_use_passes.h
| `-- lib C++预测库
| |-- libpaddle_api_light_bundled.a C++静态库
| `-- libpaddle_light_api_shared.so C++动态库
|-- java Java预测库
| |-- jar
| | `-- PaddlePredictor.jar
| |-- so
| | `-- libpaddle_lite_jni.so
| `-- src
|-- demo C++和Java示例代码
| |-- cxx C++ 预测库demo
| `-- java Java 预测库demo
```
## 2 开始运行
### 2.1 模型优化
Paddle-Lite 提供了多种策略来自动优化原始的模型其中包括量化、子图融合、混合调度、Kernel优选等方法使用Paddle_lite的opt工具可以自动
对模inference型进行优化优化后的模型更轻量模型运行速度更快。
模型优化需要使用Paddle-Lite的opt可执行文件可以通过编译Paddle-Lite源码获得编译步骤如下
```
# 如果准备环境中已经clone了Paddle-Lite则不用重新clone Paddle-Lite
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite
# 启动编译
./lite/tools/build.sh build_optimize_tool
```
编译完成后opt文件位于'build.opt/lite/api/'下可通过如下方式查看opt的运行选项和使用方式
```
cd build.opt/lite/api/
./opt
```
|选项|说明|
|:-:|:-:|
|--model_dir|待优化的PaddlePaddle模型非combined形式的路径|
|--model_file|待优化的PaddlePaddle模型combined形式的网络结构文件路径|
|--param_file|待优化的PaddlePaddle模型combined形式的权重文件路径|
|--optimize_out_type|输出模型类型目前支持两种类型protobuf和naive_buffer其中naive_buffer是一种更轻量级的序列化/反序列化实现。若您需要在mobile端执行模型预测请将此选项设置为naive_buffer。默认为protobuf|
|--optimize_out|优化模型的输出路径|
|--valid_targets|指定模型可执行的backend默认为arm。目前可支持x86、arm、opencl、npu、xpu可以同时指定多个backend(以空格分隔)Model Optimize Tool将会自动选择最佳方式。如果需要支持华为NPUKirin 810/990 Soc搭载的达芬奇架构NPU应当设置为npu, arm|
|--record_tailoring_info|当使用 根据模型裁剪库文件 功能时则设置该选项为true以记录优化后模型含有的kernel和OP信息默认为false|
--model_dir适用于待优化的模型是非combined方式PaddleOCR的inference模型是combined方式即模型结构和模型参数使用单独一个文件存储。
下面以PaddleOCR的超轻量中文模型为例介绍使用编译好的opt文件完成inference模型到Paddle-Lite优化模型的转换。
```
# 下载PaddleOCR的超轻量文inference模型并解压
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
# 转换检测模型
./opt --model_file=./ch_det_mv3_db/model --param_file=./ch_det_mv3_db/params --optimize_out_type=naive_buffer --optimize_out=./ch_det_mv3_db_opt --valid_targets=arm
# 转换识别模型
./opt --model_file=./ch_rec_mv3_crnn/model --param_file=./ch_rec_mv3_crnn/params --optimize_out_type=naive_buffer --optimize_out=./ch_rec_mv3_crnn_opt --valid_targets=arm
```
转换成功后当前目录下会多出ch_det_mv3_db_opt.nb, ch_rec_mv3_crnn_opt.nb结尾的文件即是转换成功的模型文件。
### 2.2 与手机联调
首先需要进行一些准备工作。
1. 准备一台arm8的安卓手机如果编译的预测库和opt文件是armv7则需要arm7的手机。
2. 打开手机的USB调试选项选择文件传输模式连接电脑
3. 电脑上安装adb工具用于调试。在电脑终端中输入'adb devices',如果有类似以下输出,则表示安装成功。
```
List of devices attached
744be294 device
```
4. 准备预测库、模型和预测文件在预测库inference_lite_lib.android.armv8/demo/cxx/下新建一个ocr/文件夹并将转换后的nb模型、
PaddleOCR repo中PaddleOCR/deploy/lite/ 下的所有文件放在新建的ocr文件夹下。执行完成后ocr文件夹下将有如下文件格式
```
demo/cxx/ocr/
|-- debug/ 新建debug文件夹存放模型文件
| |--ch_det_mv3_db_opt.nb 优化后的检测模型文件
| |--ch_rec_mv3_crnn_opt.nb 优化后的识别模型文件
|-- utils/
| |-- clipper.cpp Clipper库的cpp文件
| |-- clipper.hpp Clipper库的hpp文件
| |-- crnn_process.cpp 识别模型CRNN的预处理和后处理cpp文件
| |-- db_post_process.cpp 检测模型DB的后处理cpp文件
|-- Makefile 编译文件
|-- ocr_db_crnn.cc C++预测文件
```
5. 编译C++预测文件,准备测试图像,准备字典文件
```
cd demo/cxx/ocr/
# 执行编译
make
# 将编译的可执行文件移动到debug文件夹中
mv ocr_db_crnn ./debug/
```
准备测试图像以PaddleOCR/doc/imgs/12.jpg为例将测试的图像复制到demo/cxx/ocr/debug/文件夹下。
准备字典文件将PaddleOCR/ppocr/utils/ppocr_keys_v1.txt复制到demo/cxx/ocr/debug/文件夹下。
上述步骤完成后就可以使用adb将文件push到手机上运行步骤如下
```
adb push debug /data/local/tmp/
adb shell
cd /data/local/tmp/debug
export LD_LIBRARY_PATH=/data/local/tmp/debug:$LD_LIBRARY_PATH
./ocr_db_crnn ch_det_mv3_db_opt.nb ch_rec_mv3_crnn_opt.nb ./12.jpg
```
如果对代码做了修改则需要重新编译并push到手机上。

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,168 @@
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <vector>
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "math.h"
#include <iostream>
#include <cstring>
#include <fstream>
#define character_type "ch"
#define max_dict_length 6624
const std::vector<int> rec_image_shape {3, 32, 320};
cv::Mat crnn_resize_norm_img(cv::Mat img, float wh_ratio){
int imgC, imgH, imgW;
imgC = rec_image_shape[0];
imgW = rec_image_shape[2];
imgH = rec_image_shape[1];
if (character_type=="ch")
imgW = int(32*wh_ratio);
float ratio = float(img.cols)/float(img.rows);
int resize_w, resize_h;
if (ceilf(imgH*ratio)>imgW)
resize_w = imgW;
else
resize_w = int(ceilf(imgH*ratio));
cv::Mat resize_img;
cv::resize(img, resize_img, cv::Size(resize_w, imgH),0.f, 0.f, cv::INTER_CUBIC);
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
for (int h=0; h< resize_img.rows; h++){
for (int w=0; w< resize_img.cols; w++){
resize_img.at<cv::Vec3f>(h, w)[0] = (resize_img.at<cv::Vec3f>(h, w)[0] - 0.5) *2;
resize_img.at<cv::Vec3f>(h, w)[1] = (resize_img.at<cv::Vec3f>(h, w)[1] - 0.5) *2;
resize_img.at<cv::Vec3f>(h, w)[2] = (resize_img.at<cv::Vec3f>(h, w)[2] - 0.5) *2;
}
}
cv::Mat dist;
cv::copyMakeBorder(resize_img, dist, 0, 0, 0, int(imgW-resize_w), cv::BORDER_CONSTANT, {0, 0, 0});
return dist;
}
cv::Mat crnn_resize_img(cv::Mat img, float wh_ratio){
int imgC, imgH, imgW;
imgC = rec_image_shape[0];
imgW = rec_image_shape[2];
imgH = rec_image_shape[1];
if (character_type=="ch")
imgW = int(32*wh_ratio);
float ratio = float(img.cols)/float(img.rows);
int resize_w, resize_h;
if (ceilf(imgH*ratio)>imgW)
resize_w = imgW;
else
resize_w = int(ceilf(imgH*ratio));
cv::Mat resize_img;
cv::resize(img, resize_img, cv::Size(resize_w, imgH),0.f, 0.f, cv::INTER_LINEAR);
return resize_img;
}
std::basic_string<char, std::char_traits<char>, std::allocator<char>> * read_dict(std::string path){
std::ifstream ifs;
std::string charactors[max_dict_length];
ifs.open(path);
if (!ifs.is_open())
{
std::cout<<"open file "<<path<<" failed"<<std::endl;
}
else
{
std::string con = "";
int count = 0;
while (ifs)
{
getline(ifs, charactors[count]);
count++;
}
}
return charactors;
}
cv::Mat get_rotate_crop_image(cv::Mat srcimage, std::vector<std::vector<int>> box){
cv::Mat image;
srcimage.copyTo(image);
std::vector<std::vector<int>> points = box;
int x_collect[4] = {box[0][0], box[1][0], box[2][0], box[3][0]};
int y_collect[4] = {box[0][1], box[1][1], box[2][1], box[3][1]};
int left = int(*std::min_element(x_collect, x_collect+4));
int right = int(*std::max_element(x_collect, x_collect+4));
int top = int(*std::min_element(y_collect, y_collect+4));
int bottom = int(*std::max_element(y_collect, y_collect+4));
cv::Mat img_crop;
image(cv::Rect(left, top, right-left, bottom-top)).copyTo(img_crop);
for(int i=0; i<points.size(); i++){
points[i][0] -= left;
points[i][1] -= top;
}
int img_crop_width = int(sqrt(pow(points[0][0] - points[1][0], 2) +
pow(points[0][1] - points[1][1], 2)));
int img_crop_height = int(sqrt(pow(points[0][0] - points[3][0], 2) +
pow(points[0][1] - points[3][1], 2)));
cv::Point2f pts_std[4];
pts_std[0] = cv::Point2f(0., 0.);
pts_std[1] = cv::Point2f(img_crop_width, 0.);
pts_std[2] = cv::Point2f(img_crop_width, img_crop_height);
pts_std[3] = cv::Point2f(0.f, img_crop_height);
cv::Point2f pointsf[4];
pointsf[0] = cv::Point2f(points[0][0], points[0][1]);
pointsf[1] = cv::Point2f(points[1][0], points[1][1]);
pointsf[2] = cv::Point2f(points[2][0], points[2][1]);
pointsf[3] = cv::Point2f(points[3][0], points[3][1]);
cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std);
cv::Mat dst_img;
cv::warpPerspective(img_crop, dst_img, M, cv::Size(img_crop_width, img_crop_height), cv::BORDER_REPLICATE);
if (float(dst_img.rows) >= float(dst_img.cols)*1.5){
cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth());
cv::transpose(dst_img, srcCopy);
cv::flip(srcCopy, srcCopy, 0);
return srcCopy;
}
else{
return dst_img;
}
}
template<class ForwardIterator>
inline size_t argmax(ForwardIterator first, ForwardIterator last)
{
return std::distance(first, std::max_element(first, last));
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save