i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that
OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]
but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code
void run() throws IOException {
String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/4.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat();
//// here i prepare the neural network//////////
ANN_MLP knn = ANN_MLP.create();
TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);
knn.setTrainMethod(ANN_MLP.BACKPROP);
knn.setBackpropWeightScale(0.05f);
knn.setBackpropMomentumScale(0.05f);
knn.setTermCriteria(s);
" ///////////here i identify that 3 layer 1 input 5 hidden 1 output ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
Mat layers = new Mat(3,1,CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(1));
layers.row(1).setTo(new Scalar(5));
layers.row(2).setTo(new Scalar(1));
knn.setLayerSizes(layers);
int i = 0;
for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
Core.addWeighted(image, 1.5, image, 0, 0, image);
image.convertTo(image, CvType.CV_32F); // Convert to float
Imgproc.resize(image, image, new Size(60, 74));
Mat imgresized = image.reshape(1, 1); // make continous
trainingData.push_back(imgresized);
int nImagesPerClass = 5;
int id = 1 + (i / nImagesPerClass);
trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
i++;
System.out.println(trainingData);
}
Mat response = new Mat();
Mat tmp;
tmp = trainingLabels.reshape(1, 1); // make continuous
tmp.convertTo(response, CvType.CV_32F); // Convert to float
System.out.println(trainingData.cols());
knn.train(trainingData, Ml.ROW_SAMPLE, response); <<-- "error point here when i output trainiData.rows=45 and trainiData.rows.cols = 4440 so i guess the bad argument is for the cols ? "
Mat test = new Mat();
Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
Core.addWeighted(test, 0.5, test, 0, 0, test);
Imgproc.resize(test, test, new Size(60, 74));
test.convertTo(test, CvType.CV_32F);
Mat results = new Mat();
int label = (int) knn.predict(test.reshape(1, 1), results, 0);
System.out.println(label);