Ejemplo n.º 1
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);
    }
  }