Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sync] Sync Java API to master #1856

Merged
merged 4 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/scripts/test_java_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
'configs': [
'https://media.githubusercontent.com/media/hanrui1sensetime/mmdeploy-javaapi-testdata/master/litehrnet.tar' # noqa: E501
]
},
{
'task':
'RotatedDetection',
'configs': [
'https://media.githubusercontent.com/media/hanrui1sensetime/mmdeploy-javaapi-testdata/master/gliding-vertex.tar' # noqa: E501
]
}
]

Expand Down
1 change: 1 addition & 0 deletions csrc/mmdeploy/apis/java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ add_jar(${PROJECT_NAME} SOURCES
mmdeploy/TextRecognizer.java
mmdeploy/Restorer.java
mmdeploy/PoseDetector.java
mmdeploy/RotatedDetector.java
OUTPUT_NAME mmdeploy
OUTPUT_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
46 changes: 42 additions & 4 deletions csrc/mmdeploy/apis/java/mmdeploy/Classifier.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
package mmdeploy;

/** @description: the Java API class of Classifier. */
public class Classifier {
static {
System.loadLibrary("mmdeploy_java");
}

private final long handle;

/** @description: Single classification result of a picture. */
public static class Result {

/** Class id. */
public int label_id;

/** Class score. */
public float score;

/** Initializes a new instance of the Result class.
* @param label_id: class id.
* @param score: class score.
*/
public Result(int label_id, float score) {
this.label_id = label_id;
this.score = score;
}
}

public Classifier(String modelPath, String deviceName, int deviceId) {
/** Initializes a new instance of the Classifier class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create Classifier failed exception.
*/
public Classifier(String modelPath, String deviceName, int deviceId) throws Exception{
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create Classifier failed!");
}
}

public Result[][] apply(Mat[] images) {
/** Get label information of each image in a batch.
* @param images: input mats.
* @return: results of each input mat.
* @exception Exception: apply Classifier failed exception.
*/
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 Classifier failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Expand All @@ -36,12 +64,22 @@ public Result[][] apply(Mat[] images) {
return rets;
}

public Result[] apply(Mat image) {
/** Get label information of one image.
* @param image: input mat.
* @return: result of input mat.
* @exception Exception: apply Classifier failed exception.
*/
public Result[] apply(Mat image) throws Exception{
int[] counts = new int[1];
Mat[] images = new Mat[]{image};
return apply(handle, images, counts);
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply Classifier failed!");
}
return results;
}

/** Release the instance of Classifier. */
public void release() {
destroy(handle);
}
Expand Down
4 changes: 4 additions & 0 deletions csrc/mmdeploy/apis/java/mmdeploy/DataType.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package mmdeploy;

/** @description: DataType. */
public enum DataType {
FLOAT(0),
HALF(1),
INT8(2),
INT32(3);
final int value;

/** Initializes a new instance of the DataType class.
* @param value: the value.
*/
DataType(int value) {
this.value = value;
}
Expand Down
52 changes: 48 additions & 4 deletions csrc/mmdeploy/apis/java/mmdeploy/Detector.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package mmdeploy;

/** @description: the Java API class of Detector. */
public class Detector {
static {
System.loadLibrary("mmdeploy_java");
}

private final long handle;

/** @description: Single detection result of a picture. */
public static class Result {

/** Bbox class id. */
public int label_id;

/** Bbox score. */
public float score;

/** Bbox coordinates. */
public Rect bbox;

/** Bbox mask. */
public InstanceMask mask;

/** Initializes a new instance of the Result class.
* @param label_id: bbox class id.
* @param score: bbox score.
* @param bbox: bbox coordinates.
* @param mask: bbox mask.
*/
public Result(int label_id, float score, Rect bbox, InstanceMask mask) {
this.label_id = label_id;
this.score = score;
Expand All @@ -20,13 +37,30 @@ public Result(int label_id, float score, Rect bbox, InstanceMask mask) {
}
}

public Detector(String modelPath, String deviceName, int deviceId) {
/** Initializes a new instance of the Detector class.
* @param modelPath: model path.
* @param deviceName: device name.
* @param deviceId: device ID.
* @exception Exception: create Detector failed exception.
*/
public Detector(String modelPath, String deviceName, int deviceId) throws Exception {
handle = create(modelPath, deviceName, deviceId);
if (handle == -1) {
throw new Exception("Create Detector failed!");
}
}

public Result[][] apply(Mat[] images) {
/** Get information of each image in a batch.
* @param images: input mats.
* @return: results of each input mat.
* @exception Exception: apply Detector failed exception.
*/
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 Detector failed!");
}
Result[][] rets = new Result[images.length][];
int offset = 0;
for (int i = 0; i < images.length; ++i) {
Expand All @@ -40,12 +74,22 @@ public Result[][] apply(Mat[] images) {
return rets;
}

public Result[] apply(Mat image) {
/** Get information of one image.
* @param image: input mat.
* @return: result of input mat.
* @exception Exception: apply Detector failed exception.
*/
public Result[] apply(Mat image) throws Exception{
int[] counts = new int[1];
Mat[] images = new Mat[]{image};
return apply(handle, images, counts);
Result[] results = apply(handle, images, counts);
if (results == null) {
throw new Exception("Apply Detector failed!");
}
return results;
}

/** Release the instance of Detector. */
public void release() {
destroy(handle);
}
Expand Down
11 changes: 10 additions & 1 deletion csrc/mmdeploy/apis/java/mmdeploy/InstanceMask.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package mmdeploy;

/** @description: InstanceMask. */
public class InstanceMask {

/** Mask shape. */
public int[] shape;
public char[] data;

/** Mask data. */
public char[] data;

/** Initialize a new instance of the InstanceMask class.
* @param height: height.
* @param width: width.
* @param data: mask data.
*/
public InstanceMask(int height, int width, char[] data) {
shape = new int[]{height, width};
this.data = data;
Expand Down
18 changes: 17 additions & 1 deletion csrc/mmdeploy/apis/java/mmdeploy/Mat.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package mmdeploy;

/** @description: Mat. */
public class Mat {

/** Shape. */
public int[] shape;

/** Pixel format. */
public int format;

/** Data type. */
public int type;
public byte[] data;

/** Mat data. */
public byte[] data;

/** Initialize a new instance of the Mat class.
* @param height: height.
* @param width: width.
* @param channel: channel.
* @param format: pixel format.
* @param type: data type.
* @param data: mat data.
*/
public Mat(int height, int width, int channel,
PixelFormat format, DataType type, byte[] data) {
shape = new int[]{height, width, channel};
Expand Down
4 changes: 4 additions & 0 deletions csrc/mmdeploy/apis/java/mmdeploy/PixelFormat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mmdeploy;

/** @description: PixelFormat. */
public enum PixelFormat {
BGR(0),
RGB(1),
Expand All @@ -9,6 +10,9 @@ public enum PixelFormat {
BGRA(5);
final int value;

/** Initialize a new instance of the PixelFormat class.
* @param value: the value.
*/
PixelFormat(int value) {
this.value = value;
}
Expand Down
10 changes: 9 additions & 1 deletion csrc/mmdeploy/apis/java/mmdeploy/PointF.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package mmdeploy;

/** @description: the PointF class. */
public class PointF {

/** x coordinate. */
public float x;
public float y;

/** 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;
Expand Down
46 changes: 42 additions & 4 deletions csrc/mmdeploy/apis/java/mmdeploy/PoseDetector.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
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;
}
}

public PoseDetector(String modelPath, String deviceName, int deviceId) {
/** 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!");
}
}

public Result[][] apply(Mat[] images) {
/** 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) {
Expand All @@ -33,11 +61,21 @@ public Result[][] apply(Mat[] images) {
return rets;
}

public Result[] apply(Mat image) {
/** 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};
return apply(handle, images);
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);
}
Expand Down
Loading