Commit 546b4279 authored by limm's avatar limm
Browse files

add csrc and mmdeploy module

parent 502f4fb9
Pipeline #2810 canceled with stages
package mmdeploy;
/** @description: the Model class. */
public class Model {
static {
System.loadLibrary("mmdeploy_java");
}
private final long modelHandle;
/** Initialize a new instance of the Model class.
* @param path: model path.
*/
public Model(String path) {
modelHandle = create(path);
}
/** Release the instance of Model. */
public void release() {
destroy(modelHandle);
}
/** Get model handle.
* @return: model handle.
*/
public long handle() {
return modelHandle;
}
private native long create(String path);
private native void destroy(long modelHandle);
}
package mmdeploy;
/** @description: PixelFormat. */
public enum PixelFormat {
BGR(0),
RGB(1),
GRAYSCALE(2),
NV12(3),
NV21(4),
BGRA(5);
final int value;
/** Initialize a new instance of the PixelFormat class.
* @param value: the value.
*/
PixelFormat(int value) {
this.value = value;
}
}
package mmdeploy;
/** @description: the PointF class. */
public class PointF {
/** x coordinate. */
public float x;
/** y coordinate. */
public float y;
/** Initialize a new instance of the PointF class.
* @param x: x coordinate.
* @param y: y coordinate.
*/
public PointF(float x, float y) {
this.x = x;
this.y = y;
}
}
package mmdeploy;
/** @description: the Java API class of PoseDetector. */
public class PoseDetector {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single pose estimation result of a picture. */
public static class Result {
/** Points. */
public PointF[] point;
/** Scores of points */
public float[] score;
/** Initializes a new instance of the Result class.
* @param point: points.
* @param score: scores of points.
*/
public Result(PointF[] point, float [] score) {
this.point = point;
this.score = score;
}
}
/** Initializes a new instance of the PoseDetector class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create PoseDetector failed exception.
*/
public PoseDetector(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create PoseDetector failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @return: results of each input mat.
* @exception Exception: apply PoseDetector failed exception.
*/
public Result[][] apply(Mat[] images) throws Exception{
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply PoseDetector failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[1];
System.arraycopy(results, offset, row, 0, 1);
offset += 1;
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @return: result of input mat.
* @exception Exception: apply PoseDetector failed exception.
*/
public Result[] apply(Mat image) throws Exception{
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply PoseDetector failed!");
}
return results;
}
/** Release the instance of PoseDetector. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images);
}
package mmdeploy;
/** @description: the Java API class of PoseTracker. */
public class PoseTracker {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
private long stateHandle;
/** @description: Single tracking result of a bbox. */
public static class Result {
/** Keypoints. */
public PointF[] keypoints;
/** Scores. */
public float[] scores;
/** Bbox */
public Rect bbox;
/** Target ID. */
public int targetID;
/** Initializes a new instance of the Result class.
* @param keypoints: keypoints.
* @param scores: scores.
* @param bbox: bbox.
* @param targetID: target ID.
*/
public Result(PointF[] keypoints, float[] scores, Rect bbox, int targetID) {
this.keypoints = keypoints;
this.scores = scores;
this.bbox = bbox;
this.targetID = targetID;
}
}
/** @description: PoseTracker parameters. */
public static class Params {
/** Det interval. */
public int detInterval;
/** Det label. */
public int detLabel;
/** Det threshold. */
public float detThr;
/** Det min bbox size. */
public float detMinBboxSize;
/** Det nms threshold. */
public float detNmsThr;
/** Pose max number of bboxes. */
public int poseMaxNumBboxes;
/** Pose keypoint threshold. */
public float poseKptThr;
/** Pose min keypoints. */
public int poseMinKeypoints;
/** Pose bbox scale. */
public float poseBboxScale;
/** Pose min bbox size. */
public float poseMinBboxSize;
/** Pose nms threshold. */
public float poseNmsThr;
/** Keypoint sigmas */
public float[] keypointSigmas;
/** Keypoint sigmas size. */
public int keypointSigmasSize;
/** Track iou threshold. */
public float trackIouThr;
/** Track max missing. */
public int trackMaxMissing;
/** Track history size. */
public int trackHistorySize;
/** std weight position. */
public float stdWeightPosition;
/** std weight velocity. */
public float stdWeightVelocity;
/** Smooth params. */
public float[] smoothParams;
/** Initializes a new instance of the Params class.
* @param detInterval: det interval.
* @param detLabel: det label.
* @param detThr: det threshold.
* @param detMinBboxSize: det min bbox size.
* @param detNmsThr: det nms threshold.
* @param poseMaxNumBboxes: pose max number of bboxes.
* @param poseKptThr: pose keypoint threshold.
* @param poseMinKeypoints: pose min keypoints.
* @param poseBboxScale: pose bbox scale.
* @param poseMinBboxSize: pose min bbox size.
* @param poseNmsThr: pose nms threshold.
* @param keypointSigmas: keypoint sigmas.
* @param keypointSigmasSize: keypoint sigmas size.
* @param trackIouThr: track iou threshold.
* @param trackMaxMissing: track max missing.
* @param trackHistorySize: track history size.
* @param stdWeightPosition: std weight position.
* @param stdWeightVelocity: std weight velocity.
* @param smoothParams: smooth params.
*/
public Params(int detInterval, int detLabel, float detThr, float detMinBboxSize, float detNmsThr, int poseMaxNumBboxes,
float poseKptThr, int poseMinKeypoints, float poseBboxScale, float poseMinBboxSize, float poseNmsThr, float[] keypointSigmas,
int keypointSigmasSize, float trackIouThr, int trackMaxMissing, int trackHistorySize, float stdWeightPosition, float stdWeightVelocity,
float[] smoothParams) {
this.detInterval = detInterval;
this.detLabel = detLabel;
this.detThr = detThr;
this.detMinBboxSize = detMinBboxSize;
this.detNmsThr = detNmsThr;
this.poseMaxNumBboxes = poseMaxNumBboxes;
this.poseKptThr = poseKptThr;
this.poseMinKeypoints = poseMinKeypoints;
this.poseBboxScale = poseBboxScale;
this.poseMinBboxSize = poseMinBboxSize;
this.poseNmsThr = poseNmsThr;
this.keypointSigmas = keypointSigmas.clone();
this.keypointSigmasSize = keypointSigmasSize;
this.trackIouThr = trackIouThr;
this.trackMaxMissing = trackMaxMissing;
this.trackHistorySize = trackHistorySize;
this.stdWeightPosition = stdWeightPosition;
this.stdWeightVelocity = stdWeightVelocity;
this.smoothParams = smoothParams.clone();
}
}
/** Initializes a new instance of the PoseTracker class.
* @param detect: detect model.
* @param pose: pose model.
* @param context: context.
* @exception Exception: create PoseTracker failed exception.
*/
public PoseTracker(Model detect, Model pose, Context context) throws Exception{
handle = create(detect.handle(), pose.handle(), context.handle());
if (handle == -1) {
throw new Exception("Create PoseDetector failed!");
}
}
/** Initializes a new instance of the Params class.
* @return: default value of params.
*/
public Params initParams() {
Params params = setDefaultParams();
return params;
}
/** Initializes a new instance of the State class.
* @param params: params.
* @return: the handle of State.
*/
public long createState(Params params) {
stateHandle = createState(handle, params);
return stateHandle;
}
/** Get information of each frame in a batch.
* @param states: states of each frame in a batch.
* @param frames: input mats.
* @param detects: use detects result or not.
* @exception Exception: apply PoseTracker failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(long[] states, Mat[] frames, int[] detects) throws Exception{
int[] counts = new int[frames.length];
Result[] results = apply(handle, states, frames, detects, counts);
if (results == null) {
throw new Exception("Apply PoseTracker failed!");
}
Result[][] rets = new Result[frames.length][];
int offset = 0;
for (int i = 0; i < frames.length; ++i) {
Result[] row = new Result[counts[i]];
if (counts[i] >= 0) {
System.arraycopy(results, offset, row, 0, counts[i]);
}
offset += counts[i];
rets[i] = row;
}
return rets;
}
/** Get information of one frame.
* @param state: state of frame.
* @param frame: input mat.
* @param detect: use detect result or not.
* @exception Exception: apply PoseTracker failed exception.
* @return: result of input mat.
*/
public Result[] apply(long state, Mat frame, int detect) throws Exception{
long[] states = new long[]{state};
Mat[] frames = new Mat[]{frame};
int[] detects = new int[]{detect};
int[] counts = new int[1];
Result[] results = apply(handle, states, frames, detects, counts);
if (results == null) {
throw new Exception("Apply PoseTracker failed!");
}
return results;
}
/** Release the instance of PoseTracker. */
public void release() {
destroy(handle);
}
/** Release the instance of State. */
public void releaseState(long state) {
destroyState(state);
}
private native long create(long detect, long pose, long context);
private native void destroy(long handle);
private native long createState(long pipeline, Params params);
private native void destroyState(long state);
public native Params setDefaultParams();
private native Result[] apply(long handle, long[] states, Mat[] frames, int[] detects, int[] counts);
}
package mmdeploy;
/** @description: the Profiler class. */
public class Profiler {
static {
System.loadLibrary("mmdeploy_java");
}
private final long profilerHandle;
/** Initialize a new instance of the Profiler class.
* @param path: profiler path.
*/
public Profiler(String path) {
profilerHandle = create(path);
}
/** Release the instance of Profiler. */
public void release() {
destroy(profilerHandle);
}
/** Get profiler handle.
* @return: profiler handle.
*/
public long handle() {
return profilerHandle;
}
private native long create(String path);
private native void destroy(long profilerHandle);
}
package mmdeploy;
/** @description: the Rect class. */
public class Rect {
/** left coordinate. */
public float left;
/** top coordinate. */
public float top;
/** right coordinate. */
public float right;
/** bottom coordinate. */
public float bottom;
/** Initialize a new instance of the Rect class.
* @param left: left coordinate.
* @param top: top coordinate.
* @param right: right coordinate.
* @param bottom: bottom coordinate.
*/
public Rect(float left, float top, float right, float bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
package mmdeploy;
/** @description: the Java API class of Restorer. */
public class Restorer {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single image restore result of a picture. */
public static class Result {
/** Result mat. */
public Mat res;
/** Initializes a new instance of the Result class.
* @param res: result mat.
*/
public Result(Mat res) {
this.res = res;
}
}
/** Initializes a new instance of the Restorer class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create Restorer failed exception.
*/
public Restorer(String modelPath, String deviceName, int deviceId) throws Exception {
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create Restorer failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @exception Exception: apply Restorer failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(Mat[] images) throws Exception {
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply Restorer failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[1];
System.arraycopy(results, offset, row, 0, 1);
offset += 1;
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @exception Exception: apply Restorer failed exception.
* @return: result of input mat.
*/
public Result[] apply(Mat image) throws Exception{
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply Restorer failed!");
}
return results;
}
/** Release the instance of Restorer. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images);
}
package mmdeploy;
/** @description: the Java API class of RotatedDetector. */
public class RotatedDetector {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single rotated detection result of a picture. */
public static class Result {
/** Label ID. */
public int label_id;
/** Score. */
public float score;
/** Rotated bbox. */
public float[] rbbox;
/** Initializes a new instance of the Result class.
* @param label_id: label ID.
* @param score: score.
* @param rbbox: rotated bbox.
*/
public Result(int label_id, float score, float[] rbbox) {
this.label_id = label_id;
this.score = score;
this.rbbox = rbbox;
}
}
/** Initializes a new instance of the RotatedDetector class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create RotatedDetector failed exception.
*/
public RotatedDetector(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create RotatedDetector failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @exception Exception: apply RotatedDetector failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(Mat[] images) throws Exception{
int[] counts = new int[images.length];
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply RotatedDetector failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[counts[i]];
if (counts[i] >= 0) {
System.arraycopy(results, offset, row, 0, counts[i]);
}
offset += counts[i];
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @exception Exception: apply RotatedDetector failed exception.
* @return: result of input mat.
*/
public Result[] apply(Mat image) throws Exception{
int[] counts = new int[1];
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply RotatedDetector failed!");
}
return results;
}
/** Release the instance of Rotated Detector. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images, int[] count);
}
package mmdeploy;
/** @description: the Scheduler class. */
public class Scheduler {
static {
System.loadLibrary("mmdeploy_java");
}
private static long schedulerHandle;
/** Initialize a new instance of the Scheduler class.
* @param scheduler: scheduler handle.
*/
public Scheduler(long scheduler) {
schedulerHandle = scheduler;
}
/** Create thread pool scheduler.
* @param numThreads: thread number.
* @return: scheduler handle.
*/
public static long threadPool(int numThreads) {
schedulerHandle = createThreadPool(numThreads);
return schedulerHandle;
}
/** Create single thread scheduler.
* @return: scheduler handle.
*/
public static long thread() {
schedulerHandle = createThread();
return schedulerHandle;
}
/** Get scheduler handle.
* @return: scheduler handle.
*/
public long handle() {
return schedulerHandle;
}
/** Release the instance of Scheduler. */
public void release() {
destroy(schedulerHandle);
}
private static native long createThreadPool(int numThreads);
private static native long createThread();
private native void destroy(long schedulerHandle);
}
package mmdeploy;
public class Segmentor {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single image segmentation result of a picture. */
public static class Result {
/** Height. */
public int height;
/** Width. */
public int width;
/** Number of classes. */
public int classes;
/** Segmentation mask. */
public int[] mask;
/** Segmentation score. */
public float[] score;
/** Initializes a new instance of the Result class.
* @param height: height.
* @param width: width.
* @param classes: number of classes.
* @param mask: segmentation mask.
* @param score: segmentation score.
*/
public Result(int height, int width, int classes, int [] mask, float [] score) {
this.height = height;
this.width = width;
this.classes = classes;
this.mask = mask;
this.score = score;
}
}
/** Initializes a new instance of the Segmentor class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create Segmentator failed exception.
*/
public Segmentor(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create Segmentor failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @exception Exception: apply Segmentor failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(Mat[] images) throws Exception{
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply Segmentor failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[1];
System.arraycopy(results, offset, row, 0, 1);
offset += 1;
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @exception Exception: apply Segmentor failed exception.
* @return: result of input mat.
*/
public Result[] apply(Mat image) throws Exception{
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply Segmentor failed!");
}
return results;
}
/** Release the instance of Segmentor. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images);
}
package mmdeploy;
/** @description: the Java API class of TextDetector. */
public class TextDetector {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single text detection result of a picture. */
public static class Result {
/** Bbox. */
public PointF[] bbox;
/** Score. */
public float score;
/** Initializes a new instance of the Result class.
* @param bbox: bbox.
* @param score: score.
*/
public Result(PointF[] bbox, float score) {
this.bbox = bbox;
this.score = score;
}
}
/** Initializes a new instance of the TextDetector class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create TextDetector failed exception.
*/
public TextDetector(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create TextDetector failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @exception Exception: apply TextDetector failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(Mat[] images) throws Exception{
int[] counts = new int[images.length];
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply TextDetector failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[counts[i]];
if (counts[i] >= 0) {
System.arraycopy(results, offset, row, 0, counts[i]);
}
offset += counts[i];
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @exception Exception: apply TextDetector failed exception.
* @return: result of input mat.
*/
public Result[] apply(Mat image) throws Exception{
int[] counts = new int[1];
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply TextDetector failed!");
}
return results;
}
/** Release the instance of TextDetector. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images, int[] count);
}
package mmdeploy;
/** @description: the Java API class of TextRecognizer. */
public class TextRecognizer {
static {
System.loadLibrary("mmdeploy_java");
}
private final long handle;
/** @description: Single text recognition result of a picture. */
public static class Result {
/** Text. */
public byte [] text;
/** Score. */
public float [] score;
/** Initializes a new instance of the Result class.
* @param text: text.
* @param score: score.
*/
public Result(byte [] text, float [] score) {
this.text = text;
this.score = score;
}
}
/** Initializes a new instance of the TextRecognizer class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create TextRecognizer failed exception.
*/
public TextRecognizer(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create TextRecognizer failed!");
}
}
/** Get information of each image in a batch.
* @param images: input mats.
* @exception Exception: apply TextRecognizer failed exception.
* @return: results of each input mat.
*/
public Result[][] apply(Mat[] images) throws Exception{
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply TextRecognizer failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Result[] row = new Result[1];
System.arraycopy(results, offset, row, 0, 1);
offset += 1;
rets[i] = row;
}
return rets;
}
/** Get information of one image.
* @param image: input mat.
* @exception Exception: apply TextDetector failed exception.
* @return: result of input mat.
*/
public Result[] apply(Mat image) throws Exception{
Mat[] images = new Mat[]{image};
Result[] results = apply(handle, images);
if (results == null) {
throw new Exception("Apply TextRecognizer failed!");
}
return results;
}
/** Get information of one image from bboxes.
* @param image: input mat.
* @param bbox: bboxes information.
* @param bbox_count: numter of bboxes
* @return: result of input mat.
*/
public Result[] applyBbox(Mat image, TextDetector.Result[] bbox, int[] bbox_count) {
Mat[] images = new Mat[]{image};
return applyBbox(handle, images, bbox, bbox_count);
}
/** Release the instance of TextRecognizer. */
public void release() {
destroy(handle);
}
private native long create(String modelPath, String deviceName, int deviceId);
private native void destroy(long handle);
private native Result[] apply(long handle, Mat[] images);
private native Result[] applyBbox(long handle, Mat[] images, TextDetector.Result[] bbox, int[] bbox_count);
}
# Copyright (c) OpenMMLab. All rights reserved.
project(mmdeploy_java)
if (NOT ANDROID)
find_package(JNI REQUIRED)
else ()
set(JNI_LIBRARIES)
endif()
mmdeploy_add_library(${PROJECT_NAME} SHARED EXCLUDE
mmdeploy_Classifier.cpp
mmdeploy_Detector.cpp
mmdeploy_Segmentor.cpp
mmdeploy_Restorer.cpp
mmdeploy_PoseDetector.cpp
mmdeploy_TextDetector.cpp
mmdeploy_TextRecognizer.cpp
mmdeploy_PoseTracker.cpp
mmdeploy_Context.cpp
mmdeploy_Device.cpp
mmdeploy_Model.cpp
mmdeploy_Profiler.cpp
mmdeploy_Scheduler.cpp
mmdeploy_RotatedDetector.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE
${JNI_INCLUDE_DIRS})
mmdeploy_load_static(${PROJECT_NAME} MMDeployStaticModules)
mmdeploy_load_dynamic(${PROJECT_NAME} MMDeployDynamicModules)
target_link_libraries(${PROJECT_NAME} PRIVATE
${JNI_LIBRARIES} MMDeployLibs)
install(TARGETS ${PROJECT_NAME}
DESTINATION lib)
#ifndef MMDEPLOY_CSRC_APIS_JAVA_NATIVE_COMMON_H_
#define MMDEPLOY_CSRC_APIS_JAVA_NATIVE_COMMON_H_
#include <jni.h>
#include <vector>
#include "mmdeploy/apis/c/mmdeploy/common.h"
#include "mmdeploy/core/logger.h"
#include "mmdeploy/core/utils/formatter.h"
template <typename F>
static auto With(JNIEnv *env, jobjectArray imgs, F f) noexcept {
auto mat_clazz = env->FindClass("mmdeploy/Mat");
auto shape_field = env->GetFieldID(mat_clazz, "shape", "[I");
auto format_field = env->GetFieldID(mat_clazz, "format", "I");
auto type_field = env->GetFieldID(mat_clazz, "type", "I");
auto data_field = env->GetFieldID(mat_clazz, "data", "[B");
auto num = env->GetArrayLength(imgs);
std::vector<mmdeploy_mat_t> mats;
std::vector<jbyteArray> datum;
mats.reserve(num);
datum.reserve(num);
for (int i = 0; i < num; ++i) {
auto obj = env->GetObjectArrayElement(imgs, i);
auto shape_obj = env->GetObjectField(obj, shape_field);
auto shape = env->GetIntArrayElements((jintArray)shape_obj, nullptr);
auto format = env->GetIntField(obj, format_field);
auto type = env->GetIntField(obj, type_field);
auto &mat = mats.emplace_back();
mat.height = shape[0];
mat.width = shape[1];
mat.channel = shape[2];
env->ReleaseIntArrayElements((jintArray)shape_obj, shape, JNI_ABORT);
mat.format = (mmdeploy_pixel_format_t)format;
mat.type = (mmdeploy_data_type_t)type;
auto data_obj = env->GetObjectField(obj, data_field);
mat.data = (uint8_t *)env->GetByteArrayElements((jbyteArray)data_obj, nullptr);
datum.push_back((jbyteArray)data_obj);
}
auto ret = f(mats.data(), mats.size()); // ! f must not throw
for (int i = 0; i < num; ++i) {
env->ReleaseByteArrayElements(datum[i], (jbyte *)mats[i].data, JNI_ABORT);
}
return ret;
}
#endif // MMDEPLOY_CSRC_APIS_JAVA_NATIVE_COMMON_H_
#include "mmdeploy_Classifier.h"
#include <numeric>
#include "mmdeploy/apis/c/mmdeploy/classifier.h"
#include "mmdeploy/apis/java/native/common.h"
#include "mmdeploy/core/logger.h"
jlong Java_mmdeploy_Classifier_create(JNIEnv *env, jobject, jstring modelPath, jstring deviceName,
jint device_id) {
auto model_path = env->GetStringUTFChars(modelPath, nullptr);
auto device_name = env->GetStringUTFChars(deviceName, nullptr);
mmdeploy_classifier_t classifier{};
auto ec =
mmdeploy_classifier_create_by_path(model_path, device_name, (int)device_id, &classifier);
env->ReleaseStringUTFChars(modelPath, model_path);
env->ReleaseStringUTFChars(deviceName, device_name);
if (ec) {
MMDEPLOY_ERROR("failed to create classifier, code = {}", ec);
return -1;
}
return (jlong)classifier;
}
void Java_mmdeploy_Classifier_destroy(JNIEnv *, jobject, jlong handle) {
MMDEPLOY_DEBUG("Java_mmdeploy_Classifier_destroy");
mmdeploy_classifier_destroy((mmdeploy_classifier_t)handle);
}
jobjectArray Java_mmdeploy_Classifier_apply(JNIEnv *env, jobject thiz, jlong handle,
jobjectArray images, jintArray counts) {
return With(env, images, [&](const mmdeploy_mat_t imgs[], int size) -> jobjectArray {
mmdeploy_classification_t *results{};
int *result_count{};
auto ec = mmdeploy_classifier_apply((mmdeploy_classifier_t)handle, imgs, size, &results,
&result_count);
if (ec) {
MMDEPLOY_ERROR("failed to apply classifier, code = {}", ec);
return NULL;
}
auto result_cls = env->FindClass("mmdeploy/Classifier$Result");
auto result_ctor = env->GetMethodID(result_cls, "<init>", "(IF)V");
auto total = std::accumulate(result_count, result_count + size, 0);
auto array = env->NewObjectArray(total, result_cls, nullptr);
for (int i = 0; i < total; ++i) {
auto res = env->NewObject(result_cls, result_ctor, (jint)results[i].label_id,
(jfloat)results[i].score);
env->SetObjectArrayElement(array, i, res);
}
auto counts_array = env->GetIntArrayElements(counts, nullptr);
for (int i = 0; i < size; ++i) {
counts_array[i] = result_count[i];
}
env->ReleaseIntArrayElements(counts, counts_array, 0);
mmdeploy_classifier_release_result(results, result_count, size);
return array;
});
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mmdeploy_Classifier */
#ifndef _Included_mmdeploy_Classifier
#define _Included_mmdeploy_Classifier
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: mmdeploy_Classifier
* Method: create
* Signature: (Ljava/lang/String;Ljava/lang/String;I)J
*/
JNIEXPORT jlong JNICALL Java_mmdeploy_Classifier_create(JNIEnv *, jobject, jstring, jstring, jint);
/*
* Class: mmdeploy_Classifier
* Method: destroy
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_mmdeploy_Classifier_destroy(JNIEnv *, jobject, jlong);
/*
* Class: mmdeploy_Classifier
* Method: apply
* Signature: (J[Lmmdeploy/Mat;[I)[Lmmdeploy/Classifier/Result;
*/
JNIEXPORT jobjectArray JNICALL Java_mmdeploy_Classifier_apply(JNIEnv *, jobject, jlong,
jobjectArray, jintArray);
#ifdef __cplusplus
}
#endif
#endif
#include "mmdeploy_Context.h"
#include <numeric>
#include "mmdeploy/apis/c/mmdeploy/common.h"
#include "mmdeploy/apis/c/mmdeploy/executor.h"
#include "mmdeploy/apis/c/mmdeploy/model.h"
#include "mmdeploy/apis/java/native/common.h"
#include "mmdeploy/core/logger.h"
jlong Java_mmdeploy_Context_create(JNIEnv *env, jobject) {
mmdeploy_context_t context{};
mmdeploy_context_create(&context);
return (jlong)context;
}
jint Java_mmdeploy_Context_add(JNIEnv *env, jobject, jlong context_, jint contextType, jstring name,
jlong handle) {
auto object_name = env->GetStringUTFChars(name, nullptr);
if ((int)contextType == MMDEPLOY_TYPE_SCHEDULER) {
mmdeploy_context_add((mmdeploy_context_t)context_, (mmdeploy_context_type_t)contextType,
object_name, (mmdeploy_scheduler_t)handle);
} else if ((int)contextType == MMDEPLOY_TYPE_MODEL) {
mmdeploy_context_add((mmdeploy_context_t)context_, (mmdeploy_context_type_t)contextType,
object_name, (mmdeploy_model_t)handle);
} else if ((int)contextType == MMDEPLOY_TYPE_DEVICE) {
mmdeploy_context_add((mmdeploy_context_t)context_, (mmdeploy_context_type_t)contextType,
nullptr, (mmdeploy_device_t)handle);
} else if ((int)contextType == MMDEPLOY_TYPE_PROFILER) {
mmdeploy_context_add((mmdeploy_context_t)context_, (mmdeploy_context_type_t)contextType,
nullptr, (mmdeploy_profiler_t)handle);
} else {
MMDEPLOY_ERROR("wrong context type, got {}", (int)contextType);
return MMDEPLOY_E_NOT_SUPPORTED;
}
env->ReleaseStringUTFChars(name, object_name);
return 0;
}
void Java_mmdeploy_Context_destroy(JNIEnv *, jobject, jlong context_) {
MMDEPLOY_DEBUG("Java_mmdeploy_Context_destroy");
mmdeploy_context_destroy((mmdeploy_context_t)context_);
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mmdeploy_Context */
#ifndef _Included_mmdeploy_Context
#define _Included_mmdeploy_Context
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: mmdeploy_Context
* Method: create
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_mmdeploy_Context_create(JNIEnv *, jobject);
/*
* Class: mmdeploy_Context
* Method: add
* Signature: (JILjava/lang/String;J)I
*/
JNIEXPORT jint JNICALL Java_mmdeploy_Context_add(JNIEnv *, jobject, jlong, jint, jstring, jlong);
/*
* Class: mmdeploy_Context
* Method: destroy
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_mmdeploy_Context_destroy(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif
#endif
#include "mmdeploy_Detector.h"
#include <numeric>
#include "mmdeploy/apis/c/mmdeploy/detector.h"
#include "mmdeploy/apis/java/native/common.h"
#include "mmdeploy/core/logger.h"
jlong Java_mmdeploy_Detector_create(JNIEnv *env, jobject, jstring modelPath, jstring deviceName,
jint device_id) {
auto model_path = env->GetStringUTFChars(modelPath, nullptr);
auto device_name = env->GetStringUTFChars(deviceName, nullptr);
mmdeploy_detector_t detector{};
auto ec = mmdeploy_detector_create_by_path(model_path, device_name, (int)device_id, &detector);
env->ReleaseStringUTFChars(modelPath, model_path);
env->ReleaseStringUTFChars(deviceName, device_name);
if (ec) {
MMDEPLOY_ERROR("failed to create detector, code = {}", ec);
return -1;
}
return (jlong)detector;
}
void Java_mmdeploy_Detector_destroy(JNIEnv *, jobject, jlong handle) {
MMDEPLOY_DEBUG("Java_mmdeploy_Detector_destroy"); // maybe use info?
mmdeploy_detector_destroy((mmdeploy_detector_t)handle);
}
jobjectArray Java_mmdeploy_Detector_apply(JNIEnv *env, jobject thiz, jlong handle,
jobjectArray images, jintArray counts) {
return With(env, images, [&](const mmdeploy_mat_t imgs[], int size) -> jobjectArray {
mmdeploy_detection_t *results{};
int *result_count{};
auto ec =
mmdeploy_detector_apply((mmdeploy_detector_t)handle, imgs, size, &results, &result_count);
if (ec) {
MMDEPLOY_ERROR("failed to apply detector, code = {}", ec);
return NULL;
}
auto result_cls = env->FindClass("mmdeploy/Detector$Result");
auto result_ctor =
env->GetMethodID(result_cls, "<init>", "(IFLmmdeploy/Rect;Lmmdeploy/InstanceMask;)V");
auto total = std::accumulate(result_count, result_count + size, 0);
auto array = env->NewObjectArray(total, result_cls, nullptr);
auto rect_cls = env->FindClass("mmdeploy/Rect");
auto rect_ctor = env->GetMethodID(rect_cls, "<init>", "(FFFF)V");
auto instance_mask_cls = env->FindClass("mmdeploy/InstanceMask");
auto instance_mask_ctor = env->GetMethodID(instance_mask_cls, "<init>", "(II[C)V");
for (int i = 0; i < total; ++i) {
auto rect = env->NewObject(rect_cls, rect_ctor, (jfloat)results[i].bbox.left,
(jfloat)results[i].bbox.top, (jfloat)results[i].bbox.right,
(jfloat)results[i].bbox.bottom);
int width, height;
char *data;
jcharArray jmask;
if (results[i].mask == nullptr) {
width = 0;
height = 0;
data = nullptr;
jmask = env->NewCharArray(0);
} else {
width = results[i].mask->width;
height = results[i].mask->height;
data = results[i].mask->data;
jmask = env->NewCharArray(width * height);
env->SetCharArrayRegion(jmask, 0, width * height, (const jchar *)data);
}
auto instance_mask =
env->NewObject(instance_mask_cls, instance_mask_ctor, (jint)height, (jint)width, jmask);
auto res = env->NewObject(result_cls, result_ctor, (jint)results[i].label_id,
(jfloat)results[i].score, rect, instance_mask);
env->SetObjectArrayElement(array, i, res);
}
auto counts_array = env->GetIntArrayElements(counts, nullptr);
for (int i = 0; i < size; ++i) {
counts_array[i] = result_count[i];
}
env->ReleaseIntArrayElements(counts, counts_array, 0);
mmdeploy_detector_release_result(results, result_count, size);
return array;
});
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment