|
| 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