Я пытаюсь разработать распознавание лиц с помощью opencv в Android. Я успешно интегрировал opencv с JavaCv и Javacpp. Я тренировал изображения данных с SDCard, а затем распознавал с одним и тем же изображением, но каждый раз получал «0», хотя я изменил свои данные,но все же он показывает «0» достоверность даже при использовании похожих изображений.
private void imagePrediction() {
String trainingDir = Environment.getExternalStorageDirectory()
+ File.separator + "SpySignage_filter";
File root = new File(trainingDir);
FilenameFilter jpgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}
};
File[] imageFiles = root.listFiles(jpgFilter);
opencv_core.MatVector images = new opencv_core.MatVector(imageFiles.length);
int[] labels = new int[imageFiles.length];
int counter = 0;
int label;
opencv_core.IplImage img;
opencv_core.IplImage grayImg;
for (int i = 0; i < imageFiles.length; i++) {
// Get image and label:
img = cvLoadImage(imageFiles[i].getAbsolutePath());
label = Integer.parseInt(imageFiles[i].getName().split("\\-")[0]);
// Convert image to grayscale:
grayImg = opencv_core.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++;
}
// opencv_contrib.FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
// opencv_contrib.FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
// FaceRecognizer faceRecognizer = createLBPHFaceRecognizer()
opencv_contrib.FaceRecognizer faceRecognizer = com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer();
// opencv_contrib.FaceRecognizer faceRecognizer = createLBPHFaceRecognizer();
faceRecognizer.train(images, labels);
String dataTorecognize = Environment.getExternalStorageDirectory()
+ File.separator + "SpySignage_filter";
File rootDir = new File(dataTorecognize);
File[] imagelist = rootDir.listFiles(jpgFilter);
// for (int j = 0; j < imagelist.length; j++) {
// opencv_core.IplImage testImage = cvLoadImage( imagelist[j].getAbsolutePath());
opencv_core.IplImage testImage = cvLoadImage(Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpySignage_filter/2-hrithik_two.jpg");
opencv_core.IplImage greyTestImage = opencv_core.IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);
// And get a prediction:
// int predictedLabel = faceRecognizer.predict(greyTestImage);
// System.out.println("Predicted label: " + predictedLabel);
int n[] = new int[1];
double p[] = new double[1];
// opencv_core.IplImage ipl = MatToIplImage(m,WIDTH, HEIGHT);
// IplImage ipl = MatToIplImage(m,-1, -1);
faceRecognizer.predict(greyTestImage, n, p);
if (n[0] != -1) {
mProb = (int) p[0];
} else
mProb = -1;
// if ((n[0] != -1)&&(p[0]<95))
if (n[0] != -1) {
Toast.makeText(getApplicationContext(), "Image Recognized Confidence =" + mProb, Toast.LENGTH_LONG).show();
// return label.get(n[0]);
} else {
Toast.makeText(getApplicationContext(), "Image Not Recognized", Toast.LENGTH_LONG).show();
}
}