opencv SVM error -5 [closed]
i tried to implement the svm and i keep getting this error and i cant find any solution
void run() throws IOException {
File[] trainingSetA = new File("/home/tomna/NetBeansProjects/main/src/main/PostiveTrain").listFiles();
File[] negative = new File("/home/tomna/NetBeansProjects/main/src/main/NegativeTrain").listFiles();
List<File[]> trainingSets = new ArrayList<File[]>();
trainingSets.add(trainingSetA);
trainingSets.add(negative);
int posACount = trainingSetA.length;
int negcount = negative.length;
Mat alabels = new Mat(posACount + negcount, 1, 5, new Scalar(-1));
alabels.rowRange(0, posACount).setTo(new Scalar(1));
Mat[] featuresA = new Mat[posACount];
Mat[] featuresN = new Mat[negcount];
for (File[] set : trainingSets) {
int count = 0;
for (File image : set) {
Mat img = Imgcodecs.imread(image.getAbsolutePath(), 0);
HOGDescriptor descriptor = new HOGDescriptor(new Size(32, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
MatOfFloat descriptors = new MatOfFloat();
descriptor.compute(img, descriptors);
if (set.equals(trainingSetA)) {
featuresA[count] = descriptors.t();
}
if (set.equals(negative)) {
featuresN[count] = descriptors.t();
}
count++;
System.out.println(count);
}
}
System.out.println("Adding Features to training matrix");
Mat trainingMatA = new Mat(posACount + negcount, featuresA[0].cols(), featuresA[0].type());
for (int i = 0; i < posACount; i++) {
featuresA[i].copyTo(trainingMatA.rowRange(i, i + 1));
}
for (int i = 0; i < negcount; i++) {
featuresN[i].copyTo(trainingMatA.rowRange(i+posACount,i +posACount+1));
}
System.out.println("Adding Martrix");
System.out.println("training model....");
SVM svm = SVM.create();
svm.setKernel(SVM.LINEAR);
svm.setType(SVM.C_SVC);
svm.setC(10);
svm.train(trainingMatA, Ml.ROW_SAMPLE, alabels); <--- the error is in this line
svm.save("/home/tomna/NetBeansProjects/main/src/main/data.xml");
the error is
OpenCV Error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in train
here is also different implemntation with the same error where i train positive images and extract HOG features and then the training function called where i keep getting the error mentioned above
public static void trainPositives() {
for (File file : new File(PATH_POSITIVE).listFiles()) {
Mat img = getMat(file.getAbsolutePath());
HOGDescriptor d = new HOGDescriptor(new Size(32, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
MatOfFloat descriptorsValues = new MatOfFloat();
MatOfPoint locations = new MatOfPoint();
d.compute(img, descriptorsValues, new Size(0, 0), new Size(0, 0), locations);
v_descriptors.push_back(descriptorsValues);
v_loacation.push_back(locations);
// trainingImages.push_back(img.reshape(1, 1));
// trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32S));
// System.out.println("Succesfully add"+ img);
}
}
public static void trains() {
trainingImages.copyTo(trainingData);
// trainingData.convertTo(trainingData, CvType.CV_32);
trainingLabels.copyTo(classes);
clasificador.setKernel(SVM.LINEAR);
clasificador.setType(SVM.C_SVC);
clasificador.setTermCriteria(S);
clasificador.train(v_descriptors, Ml.ROW_SAMPLE, v_loacation);
clasificador.save(XML);
}
public static Mat getMat(String path) {
Mat img = new Mat();
Mat convert_to_gray = Imgcodecs.imread(path, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
convert_to_gray.convertTo(img, CvType.CV_8UC1);
// System.out.println("ok"+img);
return img;
}
after suggested change
error changed
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f79f4000098, pid=2467, tid=0x00007f79fd320700
#
# JRE ...
as mentioned before, - you need integer, not float labels.
hey @berak i just edited my question with another implementation and i also have the same issue u suggested to change to integer labels could u point which line should be changed i keep getting fatal error when i make any changes to any types and thank you
Mat alabels = new Mat(posACount + negcount, 1, CVType.CV_32S, new Scalar(-1));
@berak so it gets worse now a fatal error is this because i have to many sample images ?
how would we know, without an error msg?
@berak i edited with the new error