Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PaddlePaddle Hackathon4 No.185] Add PaddleDetection Models Deployment Java Examples #1782

Merged
merged 6 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/application/java/ppyoloe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
English | [简体中文](README_CN.md)
# PPYOLOE Java Deployment Example

This directory provides examples that `java/InferDemo.java` uses `Java` to call FastDeploy `C++` API and finish the deployment of `PPYOLOE` model。


Before deployment, two steps require confirmation

- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md)
- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md)


Using `Java` to call `C++` API can be divided into two steps:
* Generate dynamic link library in `C++` side.
* Call the dynamic link library in `Java` side.

## Generates dynamic link library
You first need to specify the location of the downloaded FastDeploy precompiled library in the `cpp/CMakeLists.txt` file, or the location of the FastDeploy library you compiled.

Specifically, replace your path of FastDeploy library in `line 15` of the `cpp/CMakeLists.txt file`, for example `set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")`.

Then, copy `jni.h` and `jni_md.h` which in `jdk` directory to current directory `cpp`.
```shell
cp /PathJdk/jdk-17.0.6/include/jni.h ./
cp /Pathjdk/jdk-17.0.6/include/linux/jni_md.h ./
```

Then, execute the following command in the `cpp` directory to compile and generate the dynamic link library.
```shell
mkdir build && cd build
cmake ..
make -j
```
After successful compilation, the dynamic link library will be stored in the `cpp/build` directory, ending in `.so` under `Linux` and `.dll` under `Windows`.

## Invoke dynamic link libraries using JAVA
Switch the path to the `java` directory and change the `JNI_LIB_NAME` variable in `InferDemo.java` to the absolute path of the dynamic link library just generated, for example `/home/hjyp/code/DeepLearning/FastDeploy/examples/application/java/ppyoloe/cpp/build/libinferDemo.so`。

Use the following command to add Fastdeploy library path to the environment variable. Note the path of the `FastDeploy` library replaced with your own.

```bash
source /Path/to/fastdeploy-linux-x64-1.0.4/fastdeploy_init.sh
```
Download the `PPYOLOE` model file and test images.
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz
```

Compiling Java files.
```shell
javac InferDemo.java
```

After compiling, run the following command to get the predicted result, where the first parameter indicates the path of the downloaded model and the second parameter indicates the path of the test image.
```shell
java InferDemo ./ppyoloe_crn_l_300e_coco ./000000014439.jpg
```
Then visualized inspection result is saved in the local image `vis_result.jpg`.
60 changes: 60 additions & 0 deletions examples/application/java/ppyoloe/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[English](README.md) | 简体中文
# PPYOLOE Java 部署示例

本目录下提供`java/InferDemo.java`, 使用`Java`调用`C++`API快速完成`PaddleDetection`模型`PPYOLOE`在Linux上部署的示例。

在部署前,需确认以下两个步骤

- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)



使用`Java`调用`C++` API 可以分为两步:

* 在`C++`端生成动态链接库。
* 在`Java`端调用动态链接库。

## C++端生成动态链接库
首先需要在`cpp/CMakeLists.txt`文件中指定下载好的`FastDeploy`预编译库位置,当然也可以是自己编译的`FastDeploy`库位置。

具体是将`cpp/CMakeLists.txt`文件第15行 `set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")`中的`FastDeploy_DIR`后面的路径替换为自己下载的预编译库路径。

然后,将`jdk`目录下的`jni.h`和`jni_md.h`拷贝到当前`cpp`目录下。
```shell
cp /PathJdk/jdk-17.0.6/include/jni.h ./
cp /Pathjdk/jdk-17.0.6/include/linux/jni_md.h ./
```
接着,在`cpp`目录下执行以下命令,进行编译,生成动态链接库。
```shell
mkdir build && cd build
cmake ..
make -j
```
编译成功后,动态链接库会存放在`cpp/build`目录下,`Linux`下以`.so`结尾,`Windows`下以`.dll`结尾。

## 使用JAVA调用动态链接库
切换路径到`java`目录下,将`InferDemo.java`中的`JNI_LIB_NAME`变量修改为刚刚生成的动态链接库的绝对路径,例如`/home/hjyp/code/DeepLearning/FastDeploy/examples/application/java/yolov5/cpp/build/libinferDemo.so`。

将`FastDeploy`的库路径添加到环境变量,注意替换为自己的`FastDeploy`库所在路径。
```bash
source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh
```
下载PPYOLOE模型文件和测试图片
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz
```

编译Java文件
```shell
javac InferDemo.java
```

编译完成后,执行如下命令可得到预测结果,其中第一个参数指明下载的模型路径,第二个参数指明测试图片路径。
```shell
java InferDemo ./ppyoloe_crn_l_300e_coco ./000000014439.jpg
```
可视化的检测结果图片保存在本地`vis_result.jpg`。

23 changes: 23 additions & 0 deletions examples/application/java/ppyoloe/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.
project("inferDemo")

set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")
find_package(FastDeploy REQUIRED)

# include_directories(.)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${FastDeploy_INCLUDE_DIRS})

add_library(
inferDemo
SHARED
InferDemo.cc
)

target_link_libraries(
inferDemo
${FASTDEPLOY_LIBS}
)
57 changes: 57 additions & 0 deletions examples/application/java/ppyoloe/cpp/InferDemo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "InferDemo.h"

#include "fastdeploy/vision.h"

std::string ConvertTo(JNIEnv *env, jstring jstr) {
if (!jstr) {
return "";
}
const jclass jstring_clazz = env->GetObjectClass(jstr);
const jmethodID getBytesID =
env->GetMethodID(jstring_clazz, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray jstring_bytes = reinterpret_cast<jbyteArray>(
env->CallObjectMethod(jstr, getBytesID, env->NewStringUTF("UTF-8")));

size_t length = static_cast<size_t>(env->GetArrayLength(jstring_bytes));
jbyte *jstring_bytes_ptr = env->GetByteArrayElements(jstring_bytes, NULL);

std::string res =
std::string(reinterpret_cast<char *>(jstring_bytes_ptr), length);
env->ReleaseByteArrayElements(jstring_bytes, jstring_bytes_ptr, JNI_ABORT);

env->DeleteLocalRef(jstring_bytes);
env->DeleteLocalRef(jstring_clazz);
return res;
}

JNIEXPORT void JNICALL Java_InferDemo_infer(JNIEnv *env, jobject thiz,
jstring modelPath,
jstring imagePath) {
std::string model_path = ConvertTo(env, modelPath);
if (model_path[model_path.length() - 1] != '/') {
model_path += "/";
}
std::string model_file = model_path + "model.pdmodel";
std::string params_file = model_path + "model.pdiparams";
std::string infer_cfg_file = model_path + "infer_cfg.yml";

// 模型推理的配置信息
fastdeploy::RuntimeOption option;
auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file,
infer_cfg_file, option);

assert(model.Initialized()); // 判断模型是否初始化成功

std::string image_path = ConvertTo(env, imagePath);
cv::Mat im = cv::imread(image_path);
fastdeploy::vision::DetectionResult result;

assert(model.Predict(&im, &result)); // 判断是否预测成功

std::cout << result.Str() << std::endl;

cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
// 可视化结果保存到本地
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result save in vis_result.jpg" << std::endl;
}
21 changes: 21 additions & 0 deletions examples/application/java/ppyoloe/cpp/InferDemo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions examples/application/java/ppyoloe/java/InferDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class InferDemo {

private native void infer(String modelPath, String imagePath);

private final static String JNI_LIB_NAME = "/home/hjyp/code/DeepLearning/FastDeploy/examples/application/java/ppyoloe/cpp/build/libinferDemo.so";

static {
System.load(JNI_LIB_NAME);
}

public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Please input enough params. e.g. java test.java param-dir image-path");
return;
}
String modelPath = args[0];
String imagePath = args[1];

InferDemo inferDemo = new InferDemo();

// 执行推理逻辑
inferDemo.infer(modelPath, imagePath);
}
}

59 changes: 59 additions & 0 deletions examples/application/java/yolov5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
English | [简体中文](README_CN.md)
# YOLOv5 Java Deployment Example

This directory provides examples that `java/InferDemo.java` uses `Java` to call FastDeploy `C++` API and finish the deployment of `YOLOv5` model。


Before deployment, two steps require confirmation

- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md)
- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md)


Using `Java` to call `C++` API can be divided into two steps:
* Generate dynamic link library in `C++` side.
* Call the dynamic link library in `Java` side.

## Generates dynamic link library
You first need to specify the location of the downloaded FastDeploy precompiled library in the `cpp/CMakeLists.txt` file, or the location of the FastDeploy library you compiled.

Specifically, replace your path of FastDeploy library in `line 15` of the `cpp/CMakeLists.txt file`, for example `set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")`.

Then, copy `jni.h` and `jni_md.h` which in `jdk` directory to current directory `cpp`.
```shell
cp /PathJdk/jdk-17.0.6/include/jni.h ./
cp /Pathjdk/jdk-17.0.6/include/linux/jni_md.h ./
```

Then, execute the following command in the `cpp` directory to compile and generate the dynamic link library.
```shell
mkdir build && cd build
cmake ..
make -j
```
After successful compilation, the dynamic link library will be stored in the `cpp/build` directory, ending in `.so` under `Linux` and `.dll` under `Windows`.

## Invoke dynamic link libraries using JAVA
Switch the path to the `java` directory and change the `JNI_LIB_NAME` variable in `InferDemo.java` to the absolute path of the dynamic link library just generated, for example `/home/hjyp/code/DeepLearning/FastDeploy/examples/application/java/yolov5/cpp/build/libinferDemo.so`。

Use the following command to add Fastdeploy library path to the environment variable. Note the path of the `FastDeploy` library replaced with your own.

```bash
source /Path/to/fastdeploy-linux-x64-1.0.4/fastdeploy_init.sh
```
Download the `YOLOv5` model file and test images.
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
```

Compiling Java files.
```shell
javac InferDemo.java
```

After compiling, run the following command to get the predicted result, where the first parameter indicates the path of the downloaded model and the second parameter indicates the path of the test image.
```shell
java InferDemo ./yolov5s.onnx ./000000014439.jpg
```
Then visualized inspection result is saved in the local image `vis_result.jpg`.
59 changes: 59 additions & 0 deletions examples/application/java/yolov5/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[English](README.md) | 简体中文
# YOLOv5 Java 部署示例

本目录下提供`java/InferDemo.java`, 使用`Java`调用`C++`API快速完成`PaddleDetection`模型`YOLOv5`在Linux上部署的示例。

在部署前,需确认以下两个步骤

- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)



使用`Java`调用`C++` API 可以分为两步:

* 在`C++`端生成动态链接库。
* 在`Java`端调用动态链接库。

## C++端生成动态链接库
首先需要在`cpp/CMakeLists.txt`文件中指定下载好的`FastDeploy`预编译库位置,当然也可以是自己编译的`FastDeploy`库位置。

具体是将`cpp/CMakeLists.txt`文件第15行 `set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")`中的`FastDeploy_DIR`后面的路径替换为自己下载的预编译库路径。

然后,将`jdk`目录下的`jni.h`和`jni_md.h`拷贝到当前`cpp`目录下。
```shell
cp /PathJdk/jdk-17.0.6/include/jni.h ./
cp /Pathjdk/jdk-17.0.6/include/linux/jni_md.h ./
```

接着,在`cpp`目录下执行以下命令,进行编译,生成动态链接库。
```shell
mkdir build && cd build
cmake ..
make -j
```
编译成功后,动态链接库会存放在`cpp/build`目录下,`Linux`下以`.so`结尾,`Windows`下以`.dll`结尾。

## 使用JAVA调用动态链接库
切换路径到`java`目录下,将`InferDemo.java`中的`JNI_LIB_NAME`变量修改为刚刚生成的动态链接库的绝对路径,例如`/home/hjyp/code/DeepLearning/FastDeploy/examples/application/java/yolov5/cpp/build/libinferDemo.so`。

将`FastDeploy`的库路径添加到环境变量,注意替换为自己的`FastDeploy`库所在路径。
```bash
source /Path/to/fastdeploy-linux-x64-1.0.4/fastdeploy_init.sh
```
下载YOLOv5模型文件和测试图片
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov5s.onnx
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
```

编译Java文件
```shell
javac InferDemo.java
```

编译完成后,执行如下命令可得到预测结果,其中第一个参数指明下载的模型路径,第二个参数指明测试图片路径。
```shell
java InferDemo ./yolov5s.onnx ./000000014439.jpg
```
可视化的检测结果图片保存在本地`vis_result.jpg`。
23 changes: 23 additions & 0 deletions examples/application/java/yolov5/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.
project("inferDemo")

set(FastDeploy_DIR "/home/hjyp/data/fastdeploy-linux-x64-1.0.4/")
find_package(FastDeploy REQUIRED)

# include_directories(.)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${FastDeploy_INCLUDE_DIRS})

add_library(
inferDemo
SHARED
InferDemo.cc
)

target_link_libraries(
inferDemo
${FASTDEPLOY_LIBS}
)
Loading