1

I want to detect smile on given image, but I get a bad argument error:

OpenCV Error: Incorrect size of input array (Input sample must be 1-dimensional vector) in cvPreparePredictData, file ..\..\..\..\opencv\modules\ml\src\inner_functions.cpp, line 1107
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception:    ..\..\..\..\opencv\modules\ml\src\inner_functions.cpp:1107: error: (-201) Input sample must be 1-dimensional vector in function cvPreparePredictData
]

at the last line of detectSmile(Mat face) method. The whole code:

public class SmileDetector {
private CascadeClassifier faceDetector;
private Mat image;
private Mat classes;
private Mat trainingData;
private Mat trainingImages;
private Mat trainingLabels;
CvSVM clasificador;


public void detectSmile(String filename) {
    init();
    detectFace(filename);
    Mat face = Highgui.imread("output.png", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    detectSmile(face);
}

private void init() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    faceDetector = new CascadeClassifier(new File(
            "src/main/resources/haarcascade_frontalface_alt.xml").getAbsolutePath());
    classes = new Mat();
    trainingData = new Mat();
    trainingImages = new Mat();
    trainingLabels = new Mat();
}

private void detectFace(String filename) {
    image = Highgui.imread(filename);
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    for (Rect rect : faceDetections.toArray()) {
        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                new Scalar(0, 255, 0));
    }
    Highgui.imwrite("output.png", image.submat(faceDetections.toArray()[0]));
}

private void trainSVM() {
    trainPositive();
    trainNegative();
    trainingImages.copyTo(trainingData);
    trainingData.convertTo(trainingData, CvType.CV_32FC1);
    trainingLabels.copyTo(classes);
}

private void trainPositive() {
    Mat img = new Mat();
    Mat con = Highgui.imread("D:\\cybuch\\workspace\\facerecognizer\\src\\main\\resources\\happy.png", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
    trainingImages.push_back(img.reshape(1, 1));
    trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32FC1));
}

private void trainNegative() {
    Mat img = new Mat();
    Mat con = Highgui.imread("D:\\cybuch\\workspace\\facerecognizer\\src\\main\\resources\\sad.png", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
    trainingImages.push_back(img.reshape(1,1));
    trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));   
}

private void detectSmile(Mat face) {
    trainSVM();
    CvSVMParams params = new CvSVMParams();
    params.set_kernel_type(CvSVM.LINEAR);
    clasificador = new CvSVM(trainingData, classes, new Mat(), new Mat(), params);  
    clasificador.save("svm.xml");
    clasificador.load("svm.xml");
    System.out.println(clasificador);
    Mat out = new Mat();
    face.convertTo(out, CvType.CV_32FC1);
    out.reshape(1, 1);
    System.out.println(out);
    System.out.println(clasificador.predict(out));
}
}

I use only one sample of training images, but I'll rewrite it later. For now I'd like just to make predictions working.

ZZ 5
  • 1,744
  • 26
  • 41

1 Answers1

2

reshape returns another Mat, it does not work in-place.

so, change your code to :

out = out.reshape(1, 1);

( or clasificador.predict(out.reshape(1,1)) )

(no fear, if i had a penny for everyone, who stepped into that pit, i'd be rich)

berak
  • 39,159
  • 9
  • 91
  • 89
  • Well, when I change the line for yours I get: The sample size is different from what has been used for training in function cvPreparePredictData – ZZ 5 Aug 28 '14 at 19:08
  • 4
    all images (used in training & prediction) have to be the same size – berak Aug 28 '14 at 19:23