Skip to content

Commit 8b455e8

Browse files
committed
Fixed Algorithm.save and other methods work in Java
1 parent bd786f3 commit 8b455e8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

modules/java/generator/gen_java.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ def add_class(self, decl):
10051005
type_dict["Ptr_"+name] = \
10061006
{ "j_type" : classinfo.jname,
10071007
"jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
1008-
"jni_name" : "Ptr<"+classinfo.fullName(isCPP=True)+">(("+classinfo.fullName(isCPP=True)+"*)%(n)s_nativeObj)", "jni_type" : "jlong",
1008+
"jni_name" : "*((Ptr<"+classinfo.fullName(isCPP=True)+">*)%(n)s_nativeObj)", "jni_type" : "jlong",
10091009
"suffix" : "J" }
10101010
logging.info('ok: class %s, name: %s, base: %s', classinfo, name, classinfo.base)
10111011

@@ -1575,7 +1575,7 @@ def isSmartClass(self, ci):
15751575
# if parents are smart (we hope) then children are!
15761576
# if not we believe the class is smart if it has "create" method
15771577
ci.smart = False
1578-
if ci.base:
1578+
if ci.base or ci.name == 'Algorithm':
15791579
ci.smart = True
15801580
else:
15811581
for fi in ci.methods:

modules/ml/misc/java/test/MLTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.opencv.test.ml;
2+
3+
import org.opencv.ml.Ml;
4+
import org.opencv.ml.SVM;
5+
import org.opencv.core.Mat;
6+
import org.opencv.core.MatOfFloat;
7+
import org.opencv.core.MatOfInt;
8+
import org.opencv.core.CvType;
9+
import org.opencv.test.OpenCVTestCase;
10+
import org.opencv.test.OpenCVTestRunner;
11+
12+
public class MLTest extends OpenCVTestCase {
13+
14+
public void testSaveLoad() {
15+
Mat samples = new MatOfFloat(new float[] {
16+
5.1f, 3.5f, 1.4f, 0.2f,
17+
4.9f, 3.0f, 1.4f, 0.2f,
18+
4.7f, 3.2f, 1.3f, 0.2f,
19+
4.6f, 3.1f, 1.5f, 0.2f,
20+
5.0f, 3.6f, 1.4f, 0.2f,
21+
7.0f, 3.2f, 4.7f, 1.4f,
22+
6.4f, 3.2f, 4.5f, 1.5f,
23+
6.9f, 3.1f, 4.9f, 1.5f,
24+
5.5f, 2.3f, 4.0f, 1.3f,
25+
6.5f, 2.8f, 4.6f, 1.5f
26+
}).reshape(1, 10);
27+
Mat responses = new MatOfInt(new int[] {
28+
0, 0, 0, 0, 0, 1, 1, 1, 1, 1
29+
}).reshape(1, 10);
30+
SVM saved = SVM.create();
31+
assertFalse(saved.isTrained());
32+
33+
saved.train(samples, Ml.ROW_SAMPLE, responses);
34+
assertTrue(saved.isTrained());
35+
36+
String filename = OpenCVTestRunner.getTempFileName("yml");
37+
saved.save(filename);
38+
SVM loaded = SVM.load(filename);
39+
assertTrue(saved.isTrained());
40+
}
41+
42+
}

0 commit comments

Comments
 (0)