[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,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_