From e661acb4dcece1fe1b2c788b944246b17bdd3920 Mon Sep 17 00:00:00 2001 From: irexyc Date: Thu, 21 Sep 2023 15:16:45 +0800 Subject: [PATCH] update jave api && demo --- .../apis/java/mmdeploy/PoseDetector.java | 10 ++++- .../java/native/mmdeploy_PoseDetector.cpp | 21 ++++++++- demo/java/src/PoseDetection.java | 43 ++++++++++++++++++- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java b/csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java index 03cdd66a06..9a8073899b 100644 --- a/csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java +++ b/csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java @@ -17,13 +17,21 @@ public static class Result { /** Scores of points */ public float[] score; + /** BBox */ + public Rect [] bbox; + + /** BBox score */ + public float[] bboxScore; + /** Initializes a new instance of the Result class. * @param point: points. * @param score: scores of points. */ - public Result(PointF[] point, float [] score) { + public Result(PointF[] point, float[] score, Rect[] bbox, float[] bboxScore) { this.point = point; this.score = score; + this.bbox = bbox; + this.bboxScore = bboxScore; } } diff --git a/csrc/mmdeploy/apis/java/native/mmdeploy_PoseDetector.cpp b/csrc/mmdeploy/apis/java/native/mmdeploy_PoseDetector.cpp index 4956555a6e..a6e1067817 100644 --- a/csrc/mmdeploy/apis/java/native/mmdeploy_PoseDetector.cpp +++ b/csrc/mmdeploy/apis/java/native/mmdeploy_PoseDetector.cpp @@ -37,10 +37,15 @@ jobjectArray Java_mmdeploy_PoseDetector_apply(JNIEnv *env, jobject thiz, jlong h return NULL; } auto result_cls = env->FindClass("mmdeploy/PoseDetector$Result"); - auto result_ctor = env->GetMethodID(result_cls, "", "([Lmmdeploy/PointF;[F)V"); + auto result_ctor = + env->GetMethodID(result_cls, "", "([Lmmdeploy/PointF;[F[Lmmdeploy/Rect;[F)V"); + // auto result_ctor = + // env->GetMethodID(result_cls, "", "([Lmmdeploy/PointF;[F;[Lmmdeploy/Rect;[F)V"); auto array = env->NewObjectArray(size, result_cls, nullptr); auto pointf_cls = env->FindClass("mmdeploy/PointF"); auto pointf_ctor = env->GetMethodID(pointf_cls, "", "(FF)V"); + auto rect_cls = env->FindClass("mmdeploy/Rect"); + auto rect_ctor = env->GetMethodID(rect_cls, "", "(FFFF)V"); for (int i = 0; i < size; ++i) { auto keypoint_array = env->NewObjectArray(results[i].length, pointf_cls, nullptr); @@ -51,7 +56,19 @@ jobjectArray Java_mmdeploy_PoseDetector_apply(JNIEnv *env, jobject thiz, jlong h } auto score_array = env->NewFloatArray(results[i].length); env->SetFloatArrayRegion(score_array, 0, results[i].length, (jfloat *)results[i].score); - auto res = env->NewObject(result_cls, result_ctor, keypoint_array, score_array); + auto bbox_array = env->NewObjectArray(results[i].num_bbox, rect_cls, nullptr); + for (int j = 0; j < results[i].num_bbox; j++) { + auto bboxj = + env->NewObject(rect_cls, rect_ctor, (jfloat)results[i].bboxes[j].left, + (jfloat)results[i].bboxes[j].top, (jfloat)results[i].bboxes[j].right, + (jfloat)results[i].bboxes[j].bottom); + env->SetObjectArrayElement(bbox_array, j, bboxj); + } + auto bbox_score_array = env->NewFloatArray(results[i].num_bbox); + env->SetFloatArrayRegion(bbox_score_array, 0, results[i].num_bbox, + (jfloat *)results[i].bbox_score); + auto res = env->NewObject(result_cls, result_ctor, keypoint_array, score_array, bbox_array, + bbox_score_array); env->SetObjectArrayElement(array, i, res); } mmdeploy_pose_detector_release_result(results, size); diff --git a/demo/java/src/PoseDetection.java b/demo/java/src/PoseDetection.java index 6c6bba4f7b..da4b500e10 100644 --- a/demo/java/src/PoseDetection.java +++ b/demo/java/src/PoseDetection.java @@ -9,8 +9,17 @@ import java.io.File; import java.io.IOException; +import org.opencv.core.Core; +import org.opencv.imgcodecs.Imgcodecs; +import org.opencv.imgproc.Imgproc; +import org.opencv.core.Point; +import org.opencv.core.Scalar; + /** @description: this is a class for PoseDetection java demo. */ public class PoseDetection { + static { + System.loadLibrary(Core.NATIVE_LIBRARY_NAME); + } /** The main function for PoseDetection Java demo. * @param deviceName: the device name of the demo. @@ -34,17 +43,47 @@ public static void main(String[] args) { poseEstimator = new PoseDetector(modelPath, deviceName, 0); // load image - Mat img = Utils.loadImage(imagePath); + org.opencv.core.Mat cvimg = Imgcodecs.imread(imagePath); + Mat img = Utils.cvMatToMat(cvimg); // apply pose estimator PoseDetector.Result[] result = poseEstimator.apply(img); // print results for (PoseDetector.Result value : result) { + int num_bbox = value.bbox.length; + int kpt_each_bbox = value.point.length / num_bbox; + for (int i = 0; i < num_bbox; i++) { + System.out.printf("bbox %d\n", i); + System.out.printf("left: %f, top: %f, right: %f, bottom: %f\n", value.bbox[i].left, + value.bbox[i].top, value.bbox[i].right, value.bbox[i].bottom); + + int base_idx = kpt_each_bbox * i; + for (int j = base_idx; j < base_idx + kpt_each_bbox; j++) { + System.out.printf("point %d, x: %d, y: %d\n", i, (int)value.point[j].x, (int)value.point[j].y); + } + System.out.printf("\n"); + } + } + + // save image + for (PoseDetector.Result value : result) { + for (int i = 0; i < value.bbox.length; i++) { + Point pt1 = new Point((int)value.bbox[i].left, (int)value.bbox[i].top); + Point pt2 = new Point((int)value.bbox[i].right, (int)value.bbox[i].bottom); + Scalar color = new Scalar(0, 0, 255); + int thickness = 2; + Imgproc.rectangle(cvimg, pt1, pt2, color, thickness); + } for (int i = 0; i < value.point.length; i++) { - System.out.printf("point %d, x: %d, y: %d\n", i, (int)value.point[i].x, (int)value.point[i].y); + Point center = new Point((int)value.point[i].x, (int)value.point[i].y); + int radius = 2; + Scalar color = new Scalar(0, 255, 0); + int thickness = 2; + Imgproc.circle(cvimg, center, radius, color, thickness); } } + Imgcodecs.imwrite("output_pose.jpg", cvimg); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); } finally {