Skip to content

Commit

Permalink
update jave api && demo
Browse files Browse the repository at this point in the history
  • Loading branch information
irexyc committed Sep 21, 2023
1 parent 8307560 commit e661acb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
10 changes: 9 additions & 1 deletion csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
21 changes: 19 additions & 2 deletions csrc/mmdeploy/apis/java/native/mmdeploy_PoseDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<init>", "([Lmmdeploy/PointF;[F)V");
auto result_ctor =
env->GetMethodID(result_cls, "<init>", "([Lmmdeploy/PointF;[F[Lmmdeploy/Rect;[F)V");
// auto result_ctor =
// env->GetMethodID(result_cls, "<init>", "([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, "<init>", "(FF)V");
auto rect_cls = env->FindClass("mmdeploy/Rect");
auto rect_ctor = env->GetMethodID(rect_cls, "<init>", "(FFFF)V");

for (int i = 0; i < size; ++i) {
auto keypoint_array = env->NewObjectArray(results[i].length, pointf_cls, nullptr);
Expand All @@ -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);
Expand Down
43 changes: 41 additions & 2 deletions demo/java/src/PoseDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down

0 comments on commit e661acb

Please sign in to comment.