The above is a common Bug in Android and I found in numerous places and most of the places recommended a solution of cleaning the memory.
https://groups.google.com/forum/#!topic/javacv/tniCCfSyg4c
https://code.google.com/p/javacv/issues/attachmentText?id=121&aid=1210011000&name=main.java&token=lCt0M3aJfIOKi8TDckxXmNe82_Q%3A1381308179983
I've tried all solution to solve the problem in Android but nothing worked, but same thing is fine in System\PC because of more memory and process I guess , if thats the reason The Code Sample-
import static com.googlecode.javacv.cpp.opencv_contrib.createEigenFaceRecognizer;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.os.Environment;
import android.util.Log;
import com.googlecode.javacv.cpp.opencv_contrib.FaceRecognizer;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_core.MatVector;
public class OpenCVFaceRecognizer {
static List<File> pathList = new ArrayList<File>();
//pictureFile.getAbsolutePath()
public static int verifyFace(String strTestImage){
IplImage testImage = cvLoadImage(strTestImage);
Log.i("testImagetestImage ", testImage.toString());
String path = Environment.getExternalStorageDirectory().toString()
+ "/FaceR_DB/FaceDB";
Log.i("PATH 2", path);
File root = new File(path);
if(pathList != null && pathList.size()>0){
pathList.clear();
}
if(!root.exists() || !root.isDirectory()){
return -1;
}
List<File> listFilesForFolder = listFilesForFolder(root);
FilenameFilter pngFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".png");
}
};
Log.i("SIZEEEEEEee", ""+listFilesForFolder.size());
if(listFilesForFolder.size()<1){
return -1;
}
MatVector images = new MatVector(listFilesForFolder.size());
int[] labels = new int[listFilesForFolder.size()];
int counter = 0;
int label;
IplImage img;
IplImage grayImg;
for (File image : listFilesForFolder) {
// Get image and label:
img = cvLoadImage(image.getAbsolutePath());
String[] split = image.getName().split( "\\." );
// System.out.println("NAME "+);
label = Integer.parseInt(image.getName().split("\\.")[0]);
// Convert image to grayscale:
grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);
cvCvtColor(img, grayImg, CV_BGR2GRAY);
// Append it in the image list:
images.put(counter, grayImg);
// And in the labels list:
labels[counter] = label;
Log.i("labels ", ""+labels+" img "+img);
//img.release();
cvReleaseImage(img); // tried both separately or together, still doesn't work\
// img = null;
System.gc();
// Increase counter for next image:
counter++;
Log.i("RELEASE ", "Released "+counter);
}
FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
Log.i("After images", ""+images.size()+" labels "+labels.length+" faceRecognizer "+faceRecognizer);
faceRecognizer.train(images, labels);
Log.i("PROJECTIOn ", "-- "+faceRecognizer.getDouble( "threshold" ));
faceRecognizer.set( "threshold", 10000 );
//get the MAT : projectedTrainFaceMat LINE 700
//CvMat mat = faceRecognizer.getMat( testImage.gto );
// faceRecognizer.save("C:\\Users\\choudhar\\Bunta_DB\\fisherface_at.yml");
// CvMat eigenvalues = faceRecognizer.getMat("eigenvalues");
//CvMat W = faceRecognizer.getMat("eigenvectors");
//CvMat mean = faceRecognizer.getMat("mean");
// Load the test image:
IplImage greyTestImage = IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);
// And get a prediction:
// faceRecognizer.predict( greyTestImage, labels, new double[] {0.5} );
return faceRecognizer.predict(greyTestImage);
}
private static List<File> listFilesForFolder(final File folder) {
FilenameFilter pngFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}
};
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
pathList.add(fileEntry);
System.out.println("%%% "+fileEntry.getName());
}
}
Log.i("pathListpathList**^^ ", ""+pathList.size());
return pathList;
}
public static void main(String[] args) {
// String trainingDir = args[0];
IplImage testImage = cvLoadImage("C:\\Users\\choudhar\\Bunta_DB\\FaceDB\\Really\\4.png");
File root = new File("C:\\Users\\choudhar\\Bunta_DB\\FaceDB\\num");
//C:\Users\choudhar\Bunta_DB\FaceDB\Really
FilenameFilter pngFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".png");
}
};
File[] imageFiles = root.listFiles(pngFilter);
MatVector images = new MatVector(imageFiles.length);
int[] labels = new int[imageFiles.length];
int counter = 0;
int label;
IplImage img;
IplImage grayImg;
for (File image : imageFiles) {
// Get image and label:
img = cvLoadImage(image.getAbsolutePath());
String[] split = image.getName().split( "\\." );
System.out.println("NAME "+split[0]);
label = Integer.parseInt(image.getName().split("\\.")[0]);
// Convert image to grayscale:
grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);
cvCvtColor(img, grayImg, CV_BGR2GRAY);
// Append it in the image list:
images.put(counter, grayImg);
// And in the labels list:
labels[counter] = label;
// Increase counter for next image:
counter++;
}
// FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
//FaceRecognizer faceRecognizer = createLBPHFaceRecognizer();
faceRecognizer.train(images, labels);
System.out.println("-- "+faceRecognizer.getDouble( "threshold" ));
faceRecognizer.set( "threshold", 10000 );
//get the MAT : projectedTrainFaceMat LINE 700
//CvMat mat = faceRecognizer.getMat( testImage.gto );
// faceRecognizer.save("C:\\Users\\choudhar\\Bunta_DB\\fisherface_at.yml");
// CvMat eigenvalues = faceRecognizer.getMat("eigenvalues");
//CvMat W = faceRecognizer.getMat("eigenvectors");
//CvMat mean = faceRecognizer.getMat("mean");
// Load the test image:
IplImage greyTestImage = IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);
// And get a prediction:
// faceRecognizer.predict( greyTestImage, labels, new double[] {0.5} );
int predictedLabel = faceRecognizer.predict(greyTestImage);
// faceRecognizer.getAlgorithm( arg0 )
//
// To get the confidence of a prediction call the model with:
//
// int predictedLabel = -1;
// double confidence = 0.0;
// model->predict(testSample, predictedLabel, confidence);
System.out.println("Predicted label: " + predictedLabel+" INFO "+faceRecognizer.position());
}
}