mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-07 09:31:35 +08:00
[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:
24
examples/application/java/yolov5/cpp/CMakeLists.txt
Normal file
24
examples/application/java/yolov5/cpp/CMakeLists.txt
Normal 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}
|
||||
)
|
53
examples/application/java/yolov5/cpp/infer_demo.cc
Normal file
53
examples/application/java/yolov5/cpp/infer_demo.cc
Normal 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;
|
||||
}
|
21
examples/application/java/yolov5/cpp/infer_demo.h
Normal file
21
examples/application/java/yolov5/cpp/infer_demo.h
Normal 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_
|
Reference in New Issue
Block a user