完成linux的测试
This commit is contained in:
parent
74f762c8e9
commit
84125e62ec
|
|
@ -1,4 +1,13 @@
|
|||
# sishu-yolo-sdk
|
||||
|
||||
javac -h cpp/include src/main/java/com/bonus/sdk/YoloSdk.java src/main/java/com/bonus/sdk/Detection.java
|
||||
|
||||
windows
|
||||
cmake -S cpp -B build/windows -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake
|
||||
|
||||
cmake --build build/windows --config Release
|
||||
|
||||
linux
|
||||
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 cmake -S cpp -B build/linux
|
||||
<!-- cmake -S cpp -B build/linux -->
|
||||
cmake --build build/linux
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ cmake_minimum_required(VERSION 3.18)
|
|||
project(MyYoloSdk CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# --- 1.
|
||||
find_package(JNI REQUIRED)
|
||||
|
||||
# --- 2.
|
||||
if(WIN32)
|
||||
find_package(JNI COMPONENTS JNI REQUIRED)
|
||||
message(STATUS "Configuring for Windows (using vcpkg OpenCV + manual ONNX)")
|
||||
|
||||
#
|
||||
|
|
@ -16,6 +14,11 @@ if(WIN32)
|
|||
set(ORT_MANUAL_PATH ${CMAKE_SOURCE_DIR}/../prebuilt_libs/onnxruntime-win-x64-1.23.2)
|
||||
|
||||
elseif(UNIX)
|
||||
set(JNI_INCLUDE_DIRS
|
||||
"/usr/lib/jvm/java-11-openjdk-amd64/include"
|
||||
"/usr/lib/jvm/java-11-openjdk-amd64/include/linux"
|
||||
)
|
||||
set(JNI_LIBRARIES "/usr/lib/jvm/java-11-openjdk-amd64/lib/server/libjvm.so")
|
||||
message(STATUS "Configuring for Linux (using apt OpenCV + manual ONNX)")
|
||||
|
||||
#
|
||||
|
|
|
|||
|
|
@ -96,12 +96,8 @@ YoloDetector::YoloDetector(const ORTCHAR_T* model_path, int in_width, int in_hei
|
|||
|
||||
Ort::SessionOptions session_options;
|
||||
|
||||
|
||||
OrtCUDAProviderOptions cuda_options;
|
||||
session_options.AppendExecutionProvider_CUDA(cuda_options);
|
||||
// session_options.AppendExecutionProvider_CPU(0);
|
||||
|
||||
|
||||
|
||||
session = std::make_unique<Ort::Session>(env, model_path, session_options);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_bonus_sdk_YoloSdk_nativePredict
|
|||
jclass detClass = env->FindClass("com/bonus/sdk/Detection");
|
||||
if (!detClass) return nullptr;
|
||||
|
||||
jmethodID detConstructor = env->GetMethodID(detClass, "<init>", "(IFFIIII)V");
|
||||
jmethodID detConstructor = env->GetMethodID(detClass, "<init>", "(IFIIII)V");
|
||||
if (!detConstructor) return nullptr;
|
||||
|
||||
jobjectArray resultArray = env->NewObjectArray(results_cpp.size(), detClass, nullptr);
|
||||
|
|
|
|||
29
pom.xml
29
pom.xml
|
|
@ -14,7 +14,20 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
|
|
@ -25,7 +38,9 @@
|
|||
<includes>
|
||||
<include>libmy_yolo_sdk.so</include>
|
||||
<include>libonnxruntime.so*</include>
|
||||
<include>libopencv_world.so*</include>
|
||||
<include>libopencv_dnn.so*</include>
|
||||
<include>libopencv_imgproc.so*</include>
|
||||
<include>libopencv_core.so*</include>
|
||||
</includes>
|
||||
<targetPath>lib/linux-x86_64</targetPath>
|
||||
</resource>
|
||||
|
|
@ -36,7 +51,9 @@
|
|||
<includes>
|
||||
<include>my_yolo_sdk.dll</include>
|
||||
<include>onnxruntime.dll</include>
|
||||
<include>opencv_world*.dll</include>
|
||||
<include>opencv_core4.dll</include>
|
||||
<include>opencv_dnn4.dll</include>
|
||||
<include>opencv_imgproc4.dll</include>
|
||||
</includes>
|
||||
<targetPath>lib/win-x64</targetPath>
|
||||
</resource>
|
||||
|
|
@ -52,6 +69,12 @@
|
|||
<target>11</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.2.5</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1,4 +1,4 @@
|
|||
package com.bonus.sdk; //
|
||||
package com.bonus.sdk;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBufferByte;
|
||||
|
|
@ -10,9 +10,11 @@ import java.util.Set;
|
|||
|
||||
public class YoloSdk implements AutoCloseable {
|
||||
|
||||
private long nativeHandle;
|
||||
private long nativeHandle; //
|
||||
private static final Set<String> loadedLibraries = new HashSet<>();
|
||||
|
||||
/**
|
||||
* */
|
||||
static {
|
||||
try {
|
||||
loadSdkLibrary();
|
||||
|
|
@ -29,7 +31,7 @@ public class YoloSdk implements AutoCloseable {
|
|||
String[] dependencyLibs = {};
|
||||
|
||||
if (osName.contains("win") && osArch.contains("64")) {
|
||||
|
||||
|
||||
libPathInJar = "/lib/win-x64/";
|
||||
|
||||
dependencyLibs = new String[]{
|
||||
|
|
@ -40,20 +42,18 @@ public class YoloSdk implements AutoCloseable {
|
|||
"opencv_core4.dll",
|
||||
"opencv_imgproc4.dll",
|
||||
"opencv_dnn4.dll"
|
||||
|
||||
};
|
||||
|
||||
sdkLibName = "my_yolo_sdk.dll";
|
||||
|
||||
} else if ((osName.contains("nix") || osName.contains("nux")) && osArch.contains("64")) {
|
||||
|
||||
libPathInJar = "/lib/linux-x86_64/";
|
||||
|
||||
|
||||
dependencyLibs = new String[]{
|
||||
"libonnxruntime.so.1.23.2",
|
||||
"libopencv_core.so.4.6.0",
|
||||
"libopencv_imgproc.so.4.6.0",
|
||||
"libopencv_dnn.so.4.6.0"
|
||||
"libonnxruntime.so.1.23.2",
|
||||
"libopencv_core.so.4.6.0",
|
||||
"libopencv_imgproc.so.4.6.0",
|
||||
"libopencv_dnn.so.4.6.0"
|
||||
};
|
||||
|
||||
sdkLibName = "libmy_yolo_sdk.so";
|
||||
|
|
@ -62,18 +62,20 @@ public class YoloSdk implements AutoCloseable {
|
|||
throw new UnsupportedOperationException("Unsupported OS/Arch: " + osName + "/" + osArch);
|
||||
}
|
||||
|
||||
// 1.
|
||||
for (String lib : dependencyLibs) {
|
||||
loadLibraryFromJar(libPathInJar + lib);
|
||||
}
|
||||
|
||||
// 2.
|
||||
loadLibraryFromJar(libPathInJar + sdkLibName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* */
|
||||
private static void loadLibraryFromJar(String path) throws IOException {
|
||||
String libName = new File(path).getName();
|
||||
if (loadedLibraries.contains(libName)) {
|
||||
return;
|
||||
return; //
|
||||
}
|
||||
|
||||
try (InputStream in = YoloSdk.class.getResourceAsStream(path)) {
|
||||
|
|
@ -90,6 +92,7 @@ public class YoloSdk implements AutoCloseable {
|
|||
}
|
||||
}
|
||||
|
||||
// ---
|
||||
private native long nativeInit(String modelPath, int inputWidth, int inputHeight);
|
||||
private native void nativeRelease(long handle);
|
||||
private native Detection[] nativePredict(
|
||||
|
|
@ -124,6 +127,8 @@ public class YoloSdk implements AutoCloseable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* */
|
||||
private byte[] getBgrBytes(BufferedImage image) {
|
||||
if (image.getType() == BufferedImage.TYPE_3BYTE_BGR) {
|
||||
return ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,102 @@
|
|||
package com.bonus.sdk;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* */
|
||||
public class YoloSdkTest {
|
||||
|
||||
|
||||
|
||||
private static final String modelPath = "test_data/model.onnx";
|
||||
private static final String imagePath = "test_data/test_image.jpg";
|
||||
|
||||
private static File modelFile;
|
||||
private static File imageFile;
|
||||
|
||||
/**
|
||||
* */
|
||||
@BeforeAll
|
||||
public static void setupTestFiles() {
|
||||
System.out.println("--- [YoloSdkTest] Setting up test files...");
|
||||
|
||||
modelFile = new File(modelPath);
|
||||
imageFile = new File(imagePath);
|
||||
|
||||
|
||||
|
||||
boolean modelExists = modelFile.exists();
|
||||
boolean imageExists = imageFile.exists();
|
||||
|
||||
System.out.println("Checking model: " + modelFile.getAbsolutePath() + " (Exists: " + modelExists + ")");
|
||||
System.out.println("Checking image: " + imageFile.getAbsolutePath() + " (Exists: " + imageExists + ")");
|
||||
|
||||
|
||||
|
||||
Assumptions.assumeTrue(modelExists, "SKIPPING TEST: Model file not found at " + modelPath);
|
||||
Assumptions.assumeTrue(imageExists, "SKIPPING TEST: Test image not found at " + imagePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* */
|
||||
@Test
|
||||
public void testSdkInitializationAndPrediction() {
|
||||
System.out.println("--- [YoloSdkTest] Running testSdkInitializationAndPrediction...");
|
||||
|
||||
try {
|
||||
System.out.println("Loading image from: " + imageFile.getAbsolutePath());
|
||||
BufferedImage image = ImageIO.read(imageFile);
|
||||
Assertions.assertNotNull(image, "Failed to read image. ImageIO.read() returned null.");
|
||||
|
||||
|
||||
System.out.println("Initializing YoloSdk with model: " + modelFile.getAbsolutePath());
|
||||
|
||||
|
||||
try (YoloSdk sdk = new YoloSdk(modelFile.getAbsolutePath(), 640, 640)) {
|
||||
|
||||
System.out.println("Native SDK initialized successfully.");
|
||||
|
||||
System.out.println("Running prediction...");
|
||||
|
||||
Detection[] results = sdk.predict(image, 0.45f, 0.5f);
|
||||
System.out.println("Prediction complete.");
|
||||
|
||||
Assertions.assertNotNull(results, "Prediction results array should not be null.");
|
||||
|
||||
System.out.println("--- [YoloSdkTest] PREDICTION RESULTS ---");
|
||||
System.out.println("--- [YoloSdkTest] Found " + results.length + " objects ---");
|
||||
if (results.length == 0) {
|
||||
System.out.println("No objects detected.");
|
||||
} else {
|
||||
System.out.println("Detected " + results.length + " objects:");
|
||||
for (Detection d : results) {
|
||||
System.out.println(" - " + d.toString());
|
||||
}
|
||||
}
|
||||
System.out.println("--- [YoloSdkTest] END OF RESULTS ---");
|
||||
|
||||
|
||||
}
|
||||
System.out.println("SDK closed successfully.");
|
||||
|
||||
} catch (IOException e) {
|
||||
Assertions.fail("Failed to read image file", e);
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
Assertions.fail("SDK test failed with a runtime exception: " + e.getMessage());
|
||||
}
|
||||
|
||||
System.out.println("--- [YoloSdkTest] Test finished successfully. ---");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 3.3 MiB |
Loading…
Reference in New Issue