fixed: windows/linux双系统适配
This commit is contained in:
parent
999a4e8c59
commit
aaa71e0598
|
|
@ -42,7 +42,9 @@ public:
|
||||||
std::vector<const char*> output_node_names;
|
std::vector<const char*> output_node_names;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
YoloDetector(const char* model_path, int in_width, int in_height);
|
/**
|
||||||
|
* */
|
||||||
|
YoloDetector(const ORTCHAR_T* model_path, int in_width, int in_height);
|
||||||
|
|
||||||
std::vector<Detection> detect(
|
std::vector<Detection> detect(
|
||||||
unsigned char* image_bytes,
|
unsigned char* image_bytes,
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ std::vector<Detection> postprocess(Ort::Value& output_tensor, float scale, int p
|
||||||
/**
|
/**
|
||||||
* @brief YoloDetector
|
* @brief YoloDetector
|
||||||
*/
|
*/
|
||||||
YoloDetector::YoloDetector(const char* model_path, int in_width, int in_height)
|
YoloDetector::YoloDetector(const ORTCHAR_T* model_path, int in_width, int in_height)
|
||||||
: env(ORT_LOGGING_LEVEL_WARNING, "YOLOv8-ONNX-CPU"),
|
: env(ORT_LOGGING_LEVEL_WARNING, "YOLOv8-ONNX-CPU"),
|
||||||
input_width(in_width),
|
input_width(in_width),
|
||||||
input_height(in_height)
|
input_height(in_height)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,27 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
std::wstring jstringToWString(JNIEnv *env, jstring jStr) {
|
||||||
|
if (!jStr) return L"";
|
||||||
|
|
||||||
|
const jchar *raw = env->GetStringChars(jStr, nullptr);
|
||||||
|
if (!raw) return L"";
|
||||||
|
|
||||||
|
|
||||||
|
jsize len = env->GetStringLength(jStr);
|
||||||
|
|
||||||
|
|
||||||
|
std::wstring wStr(reinterpret_cast<const wchar_t*>(raw), len);
|
||||||
|
|
||||||
|
env->ReleaseStringChars(jStr, raw);
|
||||||
|
|
||||||
|
return wStr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void throwJavaException(JNIEnv *env, const char *message) {
|
void throwJavaException(JNIEnv *env, const char *message) {
|
||||||
env->ThrowNew(env->FindClass("java/lang/RuntimeException"), message);
|
env->ThrowNew(env->FindClass("java/lang/RuntimeException"), message);
|
||||||
}
|
}
|
||||||
|
|
@ -17,6 +38,18 @@ void throwJavaException(JNIEnv *env, const char *message) {
|
||||||
JNIEXPORT jlong JNICALL Java_com_bonus_sdk_YoloSdk_nativeInit
|
JNIEXPORT jlong JNICALL Java_com_bonus_sdk_YoloSdk_nativeInit
|
||||||
(JNIEnv *env, jobject thiz, jstring modelPath, jint inputWidth, jint inputHeight) {
|
(JNIEnv *env, jobject thiz, jstring modelPath, jint inputWidth, jint inputHeight) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
YoloDetector* detector = nullptr;
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
|
std::wstring wpath = jstringToWString(env, modelPath);
|
||||||
|
if (wpath.empty() && env->ExceptionCheck() == JNI_FALSE) {
|
||||||
|
throwJavaException(env, "Failed to convert model path to wstring.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
detector = new YoloDetector(wpath.c_str(), inputWidth, inputHeight);
|
||||||
|
#else
|
||||||
|
|
||||||
const char* c_model_path = env->GetStringUTFChars(modelPath, nullptr);
|
const char* c_model_path = env->GetStringUTFChars(modelPath, nullptr);
|
||||||
if (c_model_path == nullptr) {
|
if (c_model_path == nullptr) {
|
||||||
|
|
@ -24,18 +57,20 @@ JNIEXPORT jlong JNICALL Java_com_bonus_sdk_YoloSdk_nativeInit
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
detector = new YoloDetector(c_model_path, inputWidth, inputHeight);
|
||||||
YoloDetector* detector = new YoloDetector(c_model_path, inputWidth, inputHeight);
|
} catch (const std::exception& e) {
|
||||||
|
|
||||||
|
|
||||||
env->ReleaseStringUTFChars(modelPath, c_model_path);
|
env->ReleaseStringUTFChars(modelPath, c_model_path);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
env->ReleaseStringUTFChars(modelPath, c_model_path);
|
||||||
|
#endif
|
||||||
|
|
||||||
return reinterpret_cast<jlong>(detector);
|
return reinterpret_cast<jlong>(detector);
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
|
|
||||||
env->ReleaseStringUTFChars(modelPath, c_model_path);
|
|
||||||
|
|
||||||
std::string errMsg = "Failed to initialize C++ YoloDetector: " + std::string(e.what());
|
std::string errMsg = "Failed to initialize C++ YoloDetector: " + std::string(e.what());
|
||||||
throwJavaException(env, errMsg.c_str());
|
throwJavaException(env, errMsg.c_str());
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue