1
1
package mmdeploy ;
2
2
3
+ /** @description: the Java API class of Classifier. */
3
4
public class Classifier {
4
5
static {
5
6
System .loadLibrary ("mmdeploy_java" );
6
7
}
7
8
8
9
private final long handle ;
9
10
11
+ /** @description: Single classification result of a picture. */
10
12
public static class Result {
13
+
14
+ /** Class id. */
11
15
public int label_id ;
16
+
17
+ /** Class score. */
12
18
public float score ;
19
+
20
+ /** Initializes a new instance of the Result class.
21
+ * @param label_id: class id.
22
+ * @param score: class score.
23
+ */
13
24
public Result (int label_id , float score ) {
14
25
this .label_id = label_id ;
15
26
this .score = score ;
16
27
}
17
28
}
18
29
19
- public Classifier (String modelPath , String deviceName , int deviceId ) {
30
+ /** Initializes a new instance of the Classifier class.
31
+ * @param modelPath: model path.
32
+ * @param deviceName: device name.
33
+ * @param deviceId: device ID.
34
+ * @exception Exception: create Classifier failed exception.
35
+ */
36
+ public Classifier (String modelPath , String deviceName , int deviceId ) throws Exception {
20
37
handle = create (modelPath , deviceName , deviceId );
38
+ if (handle == -1 ) {
39
+ throw new Exception ("Create Classifier failed!" );
40
+ }
21
41
}
22
42
23
- public Result [][] apply (Mat [] images ) {
43
+ /** Get label information of each image in a batch.
44
+ * @param images: input mats.
45
+ * @return: results of each input mat.
46
+ * @exception Exception: apply Classifier failed exception.
47
+ */
48
+ public Result [][] apply (Mat [] images ) throws Exception {
24
49
int [] counts = new int [images .length ];
25
50
Result [] results = apply (handle , images , counts );
51
+ if (results == null ) {
52
+ throw new Exception ("Apply Classifier failed!" );
53
+ }
26
54
Result [][] rets = new Result [images .length ][];
27
55
int offset = 0 ;
28
56
for (int i = 0 ; i < images .length ; ++i ) {
@@ -36,12 +64,22 @@ public Result[][] apply(Mat[] images) {
36
64
return rets ;
37
65
}
38
66
39
- public Result [] apply (Mat image ) {
67
+ /** Get label information of one image.
68
+ * @param image: input mat.
69
+ * @return: result of input mat.
70
+ * @exception Exception: apply Classifier failed exception.
71
+ */
72
+ public Result [] apply (Mat image ) throws Exception {
40
73
int [] counts = new int [1 ];
41
74
Mat [] images = new Mat []{image };
42
- return apply (handle , images , counts );
75
+ Result [] results = apply (handle , images , counts );
76
+ if (results == null ) {
77
+ throw new Exception ("Apply Classifier failed!" );
78
+ }
79
+ return results ;
43
80
}
44
81
82
+ /** Release the instance of Classifier. */
45
83
public void release () {
46
84
destroy (handle );
47
85
}
0 commit comments