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

* add java examples

* fix detail

* fix pre-config

---------

Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com>
This commit is contained in:
hjyp
2023-04-10 21:23:44 +08:00
committed by GitHub
parent fc15124800
commit cc4bbf2163
18 changed files with 700 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
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
First, switch the path to `cpp` directory and 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.
> Note: you will need to specify the location of the FASTDEPLOY_INSTALL_DIR pre-compile library at compile time, but also the location of your own compiled FastDeploy library.
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
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 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`.

View File

@@ -0,0 +1,57 @@
[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`目录,将`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`目录下执行以下命令,进行编译,生成动态链接库。
> 注意:编译时需要通过`FASTDEPLOY_INSTALL_DIR`选项指明`FastDeploy`预编译库位置, 当然也可以是自己编译的`FastDeploy`库位置。
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
make -j
```
编译成功后,动态链接库会存放在`cpp/build`目录下,`Linux`下以`.so`结尾,`Windows`下以`.dll`结尾。
## 使用JAVA调用动态链接库
切换路径到`java`目录下,将`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`

View File

@@ -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")
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
set(FastDeploy_DIR ${FASTDEPLOY_INSTALL_DIR})
find_package(FastDeploy REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${FastDeploy_INCLUDE_DIRS})
add_library(
inferDemo
SHARED
infer_demo.cc
)
target_link_libraries(
inferDemo
${FASTDEPLOY_LIBS}
)

View File

@@ -0,0 +1,59 @@
#include "infer_demo.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";
// Configuration information for model inference
fastdeploy::RuntimeOption option;
auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file,
infer_cfg_file, option);
assert(model.Initialized()); // Check whether the model is successfully
// initialized
std::string image_path = ConvertTo(env, imagePath);
cv::Mat im = cv::imread(image_path);
fastdeploy::vision::DetectionResult result;
assert(model.Predict(&im,
&result)); // Check whether the prediction is successful
std::cout << result.Str() << std::endl;
cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
// sava the visual results
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result save in vis_result.jpg" << std::endl;
}

View File

@@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class InferDemo */
#ifndef EXAMPLES_APPLICATION_JAVA_PPYOLOE_CPP_INFER_DEMO_H_
#define EXAMPLES_APPLICATION_JAVA_PPYOLOE_CPP_INFER_DEMO_H_
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: InferDemo
* Method: infer
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_InferDemo_infer
(JNIEnv *env, jobject thiz, jstring modelPath, jstring imagePath);
#ifdef __cplusplus
}
#endif
#endif // EXAMPLES_APPLICATION_JAVA_PPYOLOE_CPP_INFER_DEMO_H_

View File

@@ -0,0 +1,24 @@
public class InferDemo {
private native void infer(String modelPath, String imagePath);
private final static String JNI_LIB_NAME = "../cpp/build/libinferDemo.so";
static {
System.load(InferDemo.class.getResource("/").getPath() + 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);
}
}

View File

@@ -0,0 +1,54 @@
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
First, switch the path to `app` directory and 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.
> Note: you will need to specify the location of the FASTDEPLOY_INSTALL_DIR pre-compile library at compile time, but also the location of your own compiled FastDeploy library.
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
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 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`.

View File

@@ -0,0 +1,55 @@
[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`目录,将`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`目录下执行以下命令,进行编译,生成动态链接库。
> 注意:编译时需要通过`FASTDEPLOY_INSTALL_DIR`选项指明`FastDeploy`预编译库位置, 当然也可以是自己编译的`FastDeploy`库位置。
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
make -j
```
编译成功后,动态链接库会存放在`cpp/build`目录下,`Linux`下以`.so`结尾,`Windows`下以`.dll`结尾。
## 使用JAVA调用动态链接库
`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`

View File

@@ -0,0 +1,24 @@
# 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")
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
set(FastDeploy_DIR ${FASTDEPLOY_INSTALL_DIR})
find_package(FastDeploy REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${FastDeploy_INCLUDE_DIRS})
add_library(
inferDemo
SHARED
infer_demo.cc
)
target_link_libraries(
inferDemo
${FASTDEPLOY_LIBS}
)

View File

@@ -0,0 +1,53 @@
#include "infer_demo.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);
// Configuration information for model inference
fastdeploy::RuntimeOption option;
auto model = fastdeploy::vision::detection::YOLOv5(
model_path, "", option, fastdeploy::ModelFormat::ONNX);
assert(model.Initialized()); // Check whether the model is successfully
// initialized
std::string image_path = ConvertTo(env, imagePath);
cv::Mat im = cv::imread(image_path);
fastdeploy::vision::DetectionResult result;
assert(model.Predict(&im,
&result)); // Check whether the prediction is successful
std::cout << result.Str() << std::endl;
cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
// sava the visual results
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result save in vis_result.jpg" << std::endl;
}

View File

@@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class InferDemo */
#ifndef EXAMPLES_APPLICATION_JAVA_YOLOV5_CPP_INFER_DEMO_H_
#define EXAMPLES_APPLICATION_JAVA_YOLOV5_CPP_INFER_DEMO_H_
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: InferDemo
* Method: infer
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_InferDemo_infer
(JNIEnv *env, jobject thiz, jstring modelPath, jstring imagePath);
#ifdef __cplusplus
}
#endif
#endif // EXAMPLES_APPLICATION_JAVA_YOLOV5_CPP_INFER_DEMO_H_

View File

@@ -0,0 +1,24 @@
public class InferDemo {
private native void infer(String modelPath, String imagePath);
private final static String JNI_LIB_NAME = "../cpp/build/libinferDemo.so";
static {
System.load(InferDemo.class.getResource("/").getPath() + 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);
}
}

View File

@@ -0,0 +1,54 @@
English | [简体中文](README_CN.md)
# YOLOv8 Java Deployment Example
This directory provides examples that `java/InferDemo.java` uses `Java` to call FastDeploy `C++` API and finish the deployment of `YOLOv8` 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
First, switch the path to the `cpp` directory and 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.
> Note: you will need to specify the location of the FASTDEPLOY_INSTALL_DIR pre-compile library at compile time, but also the location of your own compiled FastDeploy library.
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
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 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 `YOLOv8` model file and test images.
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov8s.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 ./yolov8s.onnx ./000000014439.jpg
```
Then visualized inspection result is saved in the local image `vis_result.jpg`.

View File

@@ -0,0 +1,55 @@
[English](README.md) | 简体中文
# YOLOv8 Java 部署示例
本目录下提供`java/InferDemo.java`, 使用`Java`调用`C++`API快速完成`PaddleDetection`模型`YOLOv8`在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`目录,将`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`目录下执行以下命令,进行编译,生成动态链接库。
> 注意:编译时需要通过`FASTDEPLOY_INSTALL_DIR`选项指明`FastDeploy`预编译库位置, 当然也可以是自己编译的`FastDeploy`库位置。
```shell
mkdir build && cd build
cmake .. -FASTDEPLOY_INSTALL_DIR /fast-deploy-path
make -j
```
编译成功后,动态链接库会存放在`cpp/build`目录下,`Linux`下以`.so`结尾,`Windows`下以`.dll`结尾。
## 使用JAVA调用动态链接库
切换路径到`java`目录下,将`FastDeploy`的库路径添加到环境变量,注意替换为自己的`FastDeploy`库所在路径。
```bash
source /Path/to/fastdeploy-linux-x64-0.0.0/fastdeploy_init.sh
```
下载YOLOv8模型文件和测试图片
```bash
wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov8s.onnx
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
```
编译Java文件
```shell
javac InferDemo.java
```
编译完成后,执行如下命令可得到预测结果,其中第一个参数指明下载的模型路径,第二个参数指明测试图片路径。
```shell
java InferDemo ./yolov8s.onnx ./000000014439.jpg
```
可视化的检测结果图片保存在本地`vis_result.jpg`

View File

@@ -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")
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
set(FastDeploy_DIR ${FASTDEPLOY_INSTALL_DIR})
find_package(FastDeploy REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${FastDeploy_INCLUDE_DIRS})
add_library(
inferDemo
SHARED
infer_demo.cc
)
target_link_libraries(
inferDemo
${FASTDEPLOY_LIBS}
)

View File

@@ -0,0 +1,53 @@
#include "infer_demo.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);
// Configuration information for model inference
fastdeploy::RuntimeOption option;
auto model = fastdeploy::vision::detection::YOLOv8(
model_path, "", option, fastdeploy::ModelFormat::ONNX);
assert(model.Initialized()); // Check whether the model is successfully
// initialized
std::string image_path = ConvertTo(env, imagePath);
cv::Mat im = cv::imread(image_path);
fastdeploy::vision::DetectionResult result;
assert(model.Predict(im,
&result)); // Check whether the prediction is successful
std::cout << result.Str() << std::endl;
cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
// sava the visual results
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result save in vis_result.jpg" << std::endl;
}

View File

@@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class InferDemo */
#ifndef EXAMPLES_APPLICATION_JAVA_YOLOV8_CPP_INFER_DEMO_H_
#define EXAMPLES_APPLICATION_JAVA_YOLOV8_CPP_INFER_DEMO_H_
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: InferDemo
* Method: infer
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_InferDemo_infer
(JNIEnv *env, jobject thiz, jstring modelPath, jstring imagePath);
#ifdef __cplusplus
}
#endif
#endif // EXAMPLES_APPLICATION_JAVA_YOLOV8_CPP_INFER_DEMO_H_

View File

@@ -0,0 +1,24 @@
public class InferDemo {
private native void infer(String modelPath, String imagePath);
private final static String JNI_LIB_NAME = "../cpp/build/libinferDemo.so";
static {
System.load(InferDemo.class.getResource("/").getPath() + 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);
}
}