I train my Model like this:
// Prepare Training Data
Mat trainData = new Mat();
int numWidth = 20;
for (int i = 0; i < digits.size().height; i += numWidth ) {
for (int j = 0; j < digits.size().width; j += numWidth) {
//Mat num = digits.submat(new Rect(j * numWidth, i * numWidth, numWidth, numWidth));
Mat num = new Mat(digits, new Rect(j, i, numWidth, numWidth));
// Convert to float data, needed for KNearest
num.convertTo(num, CvType.CV_32F);
num = num.reshape(1, 1);
trainData.push_back(num);
}
}
// Train Model
model = KNearest.create();
model.train(trainData, Ml.ROW_SAMPLE, Converters.vector_int_to_Mat(trainLabels));
And later I try to predict like this:
Mat number = new Mat(square, new Rect(bounds.x, bounds.y, bounds.width, bounds.height));
Imgproc.resize(number, number, new Size(20, 20));
saveImage("digit.png", number, context);
number.convertTo(number, CvType.CV_32F);
number = number.reshape(1, 1);
Mat res = new Mat();
float p = model.findNearest(number, 1, res);
But I get this Exception:
E/cv::error(): OpenCV(3.4.3) Error: Assertion failed (test_samples.type() == CV_32F && test_samples.cols == samples.cols) in virtual float cv::ml::BruteForceImpl::findNearest(cv::InputArray, int, cv::OutputArray, cv::OutputArray, cv::OutputArray) const, file /build/3_4_pack-android/opencv/modules/ml/src/knearest.cpp, line 312 E/org.opencv.ml: ml::findNearest_12() caught cv::Exception: OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/ml/src/knearest.cpp:312: error: (-215:Assertion failed) test_samples.type() == CV_32F && test_samples.cols == samples.cols in function 'virtual float cv::ml::BruteForceImpl::findNearest(cv::InputArray, int, cv::OutputArray, cv::OutputArray, cv::OutputArray) const' E/MethodChannel#openCV: Failed to handle method call CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/ml/src/knearest.cpp:312: error: (-215:Assertion failed) test_samples.type() == CV_32F && test_samples.cols == samples.cols in function 'virtual float cv::ml::BruteForceImpl::findNearest(cv::InputArray, int, cv::OutputArray, cv::OutputArray, cv::OutputArray) const' ] at org.opencv.ml.KNearest.findNearest_2(Native Method) at org.opencv.ml.KNearest.findNearest(KNearest.java:79) at com.wallmann.flutter_sudoku_solver.SudokuSolver.readNumbers(SudokuSolver.java:291) at com.wallmann.flutter_sudoku_solver.SudokuSolver.solveChallenge(SudokuSolver.java:46) at com.wallmann.flutter_sudoku_solver.MainActivity.lambda$configureFlutterEngine$0$MainActivity(MainActivity.java:48) at com.wallmann.flutter_sudoku_solver.-$$Lambda$MainActivity$g3xDyIoaci2vBR5JzA2Kry58MDg.onMethodCall(Unknown Source:2) at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231) at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93) at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:325) at android.os.Looper.loop(Looper.java:142) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, cv::Exception: OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/ml/src/knearest.cpp:312: error: (-215:Assertion failed) test_samples.type() == CV_32F && test_samples.cols == samples.cols in function 'virtual float cv::ml::BruteForceImpl::findNearest(cv::InputArray, int, cv::OutputArray, cv::OutputArray, cv::OutputArray) const' , null) #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7) #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33) <asynchronous suspension=""> #2 _MyHomePageState.takeImg (package:flutter_sudoku_solver/main.dart:46:28) #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14) #4 _InkResponseState.build.<anonymous closure=""> (package:flutter/src/material/ink_well.dart:789:36) #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11) #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5) #8 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7) #9 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) #10 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20) #11 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) #12 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) #13 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) #14 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) #15 _rootRunUnary (dart:async/zone.dart:1138:13) #16 _CustomZone.runUnary (dart:async/zone.dart:1031:19) #17 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7) #18 _invoke1 (dart:ui/hooks.dart:273:10) #19 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
Whats interesting is, while training, the num
Mat I train is a 20x20 crop of a larger image, when resphaping it, it turn into a 1x1200 Mat. I would expect it to become a 1x400 Mat, how is that possible?
When prediction, I crop by Mat to predict 20x20, then reshape it and it turns into a 1x400 Mat, as I would expect.
I suppose the reason for the Exception is, it expected an Mat of Size 1x1200 but recieved on of Size 1x400, right? How can fix this issue?
Thanks in advance!
Any Ideas?