Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv SVM error -5

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

opencv SVM error -5

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

opencv SVM error -5

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

opencv SVM error -5

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 version: Java(TM) SE Runtime Environment (8.0_121-b13) (build
1.8.0_121-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  0x00007f79f4000098
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/tomna/NetBeansProjects/main/hs_err_pid2467.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#