public void recognizeFileList(final String szFileTest) {
    System.out.println("===========================================");
    System.out.println("recognizing faces indexed from " + szFileTest);
    int i = 0;
    int nTestFaces = 0; // the number of test images
    CvMat trainPersonNumMat; // the person numbers during training
    float[] projectedTestFace;
    String answer;
    int nCorrect = 0;
    int nWrong = 0;
    double timeFaceRecognizeStart;
    double tallyFaceRecognizeTime;
    float confidence = 0.0f;

    // load test images and ground truth for person number
    testFaceImgArr = loadFaceImgArray(szFileTest);
    nTestFaces = testFaceImgArr.length;

    System.out.println(nTestFaces + " test faces loaded");

    // load the saved training data
    trainPersonNumMat = loadTrainingData();
    if (trainPersonNumMat == null) {
      return;
    }

    // project the test images onto the PCA subspace
    projectedTestFace = new float[nEigens];
    timeFaceRecognizeStart = (double) cvGetTickCount(); // Record the timing.

    for (i = 0; i < nTestFaces; i++) {
      int iNearest;
      int nearest;
      int truth;

      // project the test image onto the PCA subspace
      cvEigenDecomposite(
          testFaceImgArr[i], // obj
          nEigens, // nEigObjs
          eigenVectArr, // eigInput (Pointer)
          0, // ioFlags
          null, // userData
          pAvgTrainImg, // avg
          projectedTestFace); // coeffs

      // LOGGER.info("projectedTestFace\n" + floatArrayToString(projectedTestFace));

      final FloatPointer pConfidence = new FloatPointer(confidence);
      iNearest = findNearestNeighbor(projectedTestFace, new FloatPointer(pConfidence));
      confidence = pConfidence.get();
      truth = personNumTruthMat.data_i().get(i);
      nearest = trainPersonNumMat.data_i().get(iNearest);

      if (nearest == truth) {
        answer = "Correct";
        nCorrect++;
      } else {
        answer = "WRONG!";
        nWrong++;
      }
      System.out.println(
          "nearest = "
              + nearest
              + ", Truth = "
              + truth
              + " ("
              + answer
              + "). Confidence = "
              + confidence);
    }
    tallyFaceRecognizeTime = (double) cvGetTickCount() - timeFaceRecognizeStart;
    if (nCorrect + nWrong > 0) {
      System.out.println(
          "TOTAL ACCURACY: "
              + (nCorrect * 100 / (nCorrect + nWrong))
              + "% out of "
              + (nCorrect + nWrong)
              + " tests.");
      System.out.println(
          "TOTAL TIME: "
              + (tallyFaceRecognizeTime / (cvGetTickFrequency() * 1000.0 * (nCorrect + nWrong)))
              + " ms average.");
    }
  }
Beispiel #2
0
  /* From a normalized image file to recognized if match current trained data.
   */
  public void recognize(String tmpDetectImageOutput, String trainedOutput, String personName) {

    File folder = new File(tmpDetectImageOutput);
    File[] listOfFiles = folder.listFiles();
    ArrayList<String> testFaceFileNames = new ArrayList<String>();

    String answer = "";
    // load image to testFaces array list
    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        String file = listOfFiles[i].getName();
        String filepath = tmpDetectImageOutput + "/" + file;
        IplImage tmpImage = cvLoadImage(filepath, CV_LOAD_IMAGE_GRAYSCALE);
        if (tmpImage != null) {
          testFaces.add(tmpImage);
          testFaceFileNames.add(filepath);
        }
      }
    }

    CvMat trainPersonNumMat = loadTrainingData(trainedOutput, personName);

    //    int ntestfaces = testFaces.size() ;
    int nTestFaces = testFaces.size();
    LOGGER.info(trainedOutput + "/" + personName + ".xml");
    System.out.println("total: " + nTestFaces + " to be tested latter...");

    personNumTruthMat = cvCreateMat(1, nTestFaces, CV_32SC1); // type, 32-

    float[] projectedTestFace = new float[nEigens];
    float confidence = 0.0f;
    int nCorrect = 0;
    int nWrong = 0;
    double timeFaceRecognizeStart = (double) cvGetTickCount(); // supposedly to record the timing??
    for (int i = 0; i < nTestFaces; i++) {
      int iNearest;
      int nearest;
      int truth;

      // project the test image onto the PCA subspace
      LOGGER.info("before find decomposite..");
      cvEigenDecomposite(
          testFaces.get(i),
          nEigens, // nEigObjs
          eigenVectArr, // eigInput (Pointer)
          0, // ioFlags
          null, // userData
          pAvgTrainImg, // avg
          projectedTestFace); // coeffs

      // LOGGER.info("projectedTestFace\n" + floatArrayToString(projectedTestFace));

      final FloatPointer pConfidence = new FloatPointer(confidence);
      LOGGER.info("before find nearest...");
      iNearest = findNearestNeighbor(projectedTestFace, new FloatPointer(pConfidence));
      confidence = pConfidence.get();
      truth = personNumTruthMat.data_i().get(i);
      nearest = trainPersonNumMat.data_i().get(iNearest);

      if (nearest == truth) {
        answer = "Correct";
        nCorrect++;
      } else {
        answer = "WRONG!";
        nWrong++;
      }
      LOGGER.info(testFaceFileNames.get(i));
      LOGGER.info(
          "nearest = "
              + nearest
              + ", Truth = "
              + truth
              + " ("
              + answer
              + "). Confidence = "
              + confidence);
    }
  }