Hi I am very new to OpenCV,using opencv 3, I am facing problem while training and prediction the image could you please help me to solve the problem. There is no any tutorial related to FaceRecognition in OpenCV through Java. I Started using SVM bug I am not very conversant with the SVM what ever I did coding It gives me 0 as a predict lable always I think I am missing something . Please help me .... :(
public class FaceRecDemo{
public static void main(String[] args) {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
String trainingDir = args[0];
Mat testImage = Imgcodecs.imread("F:\\personal\\OpenCVTraining\\lena1.png",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
File root = new File(trainingDir);
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".pgm")
|| name.endsWith(".png");
}
};
File[] imageFiles = root.listFiles(imgFilter);
int num_files = imageFiles.length;
int img_area = 480*640;
Mat trainingMat = new Mat(num_files,img_area,CvType.CV_32FC1);
Mat labels = new Mat(num_files, 1, CvType.CV_32SC1);
int ii = 0; // Current column in training_mat
int file_num=0;
for (File image : imageFiles) {
Mat img_mat = Imgcodecs.imread(image.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Imgcodecs.imwrite("E:\\JavaCVProjects\\OpenCVJava\\hello.jpg",img_mat);
for (int i = 0; i<img_mat.rows(); i++) {
for (int j = 0; j < img_mat.cols(); j++) {
trainingMat.put(file_num, ii++, img_mat.get(i, j));
}
}
labels.put(file_num,0,file_num+10);
file_num++;
}
Mat predictData = new Mat(1, testImage.cols()*testImage.rows(), CvType.CV_32F);
int predictDataCol = 0;
for(int i=0;i<testImage.rows();i++)
{
for (int j=0;j<testImage.cols();j++) {
predictData.put(1,predictDataCol++,testImage.get(i, j));
}
}
SVM svm = SVM.create();
svm.setKernel(2);
svm.train(trainingMat,Ml.ROW_SAMPLE,labels);
Mat results = new Mat();
int label = (int) svm.predict(predictData, results, 0);
System.out.println(label);
}
}