我是javacv的新手。我正在尝试创建一个用于面部识别的系统,并且面临识别率较低的问题。我使用EigenFaceRecognizer进行身份识别,并使用Georgia Tech Face Database作为照片数据库。我所有的照片均为640x480分辨率(我每人有12张照片,总共20个人)。
结果非常不稳定,有时无法正常工作。如何提高识别精度?我应该对照片做些额外的编辑吗,或者代码中有问题?
训练方法
public void train() {
File imagesDir = new File("C:\\Java_Eclipse\\FaceRecognitionWebcam\\src\\image");
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 = imagesDir.listFiles(imgFilter);
MatVector images = new MatVector(imageFiles.length);
Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
IntBuffer labelsBuf = labels.createBuffer();
int counter = 0;
for (File image : imageFiles) {
Mat img = imread(image.getAbsolutePath(), CV_LOAD_IMAGE_GRAYSCALE);
int label = Integer.parseInt(image.getName().split("\\-")[0]);
images.put(counter, img);
labelsBuf.put(counter, label);
counter++;
}
// FaceRecognizer faceRecognizer = FisherFaceRecognizer.create();
FaceRecognizer faceRecognizer = EigenFaceRecognizer.create();
// FaceRecognizer faceRecognizer = LBPHFaceRecognizer.create();
System.out.println("Train started");
faceRecognizer.train(images, labels);
faceRecognizer.save("C:\\Java_Eclipse\\FaceRecognitionWebcam\\src\\train_result_eigen.xml");
System.out.println("Train completed");
}
识别方法
public void findFaces(IplImage currentFrame) throws InterruptedException {
IntPointer labels = new IntPointer(1);
DoublePointer confidences = new DoublePointer(1);
opencv_core.CvMemStorage storage = new opencv_core.CvMemStorage().create();
opencv_core.CvSeq faces =
cvHaarDetectObjects(currentFrame, classifierFace, storage, 1.6, 8, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);
int total = faces.total();
if(total > 0) {
System.out.println("Total faces: " + total);
for(int i = 0; i < total; i++) {
opencv_core.CvRect r = new opencv_core.CvRect(cvGetSeqElem(faces, i));
int x = r.x(); int y = r.y(); int w = r.width(); int h = r.height();
IplImage greyImg = IplImage.create(currentFrame.width(), currentFrame.height(), IPL_DEPTH_8U, 1);
cvCvtColor(currentFrame, greyImg, CV_RGB2GRAY);//resize
rectangle(cvarrToMat(currentFrame), new Rect(x, y, w, h), new Scalar(0, 255, 0, 0), 2, 0, 0);
faceRecognizer.predict(cvarrToMat(greyImg), labels, confidences);
int label = labels.get(0);
double confidence = confidences.get(0);
String labelInfo = faceRecognizer.getLabelInfo(label).toString();
System.out.println("---------");
System.out.println("Person: " + i);
System.out.println("label = " + label);
System.out.println("confidence = " + confidence);
}
}
}