public Mat detect(Mat capturedImage) {

    List<DetectedElement> detectedElements;

    // loops over all the activated detectors
    for (ElementsDetector detector : detectors.values()) {

      // gets the elements detected by this detector
      detectedElements = detector.detectElements(capturedImage);
      for (DetectedElement detectedElement : detectedElements) {

        // gets the image transformed by the detector
        capturedImage = detectedElement.getTransformedImage();

        // if has to recognize a face
        if (isRecognizerActive
            && detectedElement != null
            && detectedElement.getDetectedImageElement() != null) {

          assert (detectors.size() == 1
              && detectors.containsKey(Constants.DEFAULT_FACE_CLASSIFIER));

          // recognizes the face
          RecognizedFace recognizedFace =
              recognizerManager.recognizeFace(detectedElement.getDetectedImageElement());
          String name;
          if (recognizedFace == Constants.UNKNOWN_FACE) {
            name = recognizedFace.getName();
          } else {
            int percentage =
                (int)
                    (100
                        * (Constants.FACE_RECOGNITION_THRESHOLD - recognizedFace.getConfidence())
                        / Constants.FACE_RECOGNITION_THRESHOLD);
            name = recognizedFace.getName() + "  [" + percentage + "%]";
          }

          // writes the name of the recognized person (sort of embossed)
          Point position = detectedElement.getPosition();
          position.y -= 11;
          position.x -= 1;
          Imgproc.putText(
              capturedImage,
              name,
              position,
              Core.FONT_HERSHEY_TRIPLEX,
              Constants.RECOGNIZED_NAME_FONT_SIZE,
              Constants.BLACK);

          position.y += 1;
          position.x += 1;
          Imgproc.putText(
              capturedImage,
              name,
              position,
              Core.FONT_HERSHEY_TRIPLEX,
              Constants.RECOGNIZED_NAME_FONT_SIZE,
              colors[2]);
        }
      }
    }

    return capturedImage;
  }
Esempio n. 2
0
  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    if (mAbsoluteFaceSize == 0) {
      int height = mGray.rows();
      if (Math.round(height * mRelativeFaceSize) > 0) {
        mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
      }
    }

    if (mZoomWindow == null || mZoomWindow2 == null) CreateAuxiliaryMats();

    MatOfRect faces = new MatOfRect();

    if (mJavaDetector != null)
      mJavaDetector.detectMultiScale(
          mGray,
          faces,
          1.1,
          2,
          2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
          new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
          new Size());

    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++) {
      Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
      xCenter = (facesArray[i].x + facesArray[i].width + facesArray[i].x) / 2;
      yCenter = (facesArray[i].y + facesArray[i].y + facesArray[i].height) / 2;
      Point center = new Point(xCenter, yCenter);

      Imgproc.circle(mRgba, center, 10, new Scalar(255, 0, 0, 255), 3);

      Imgproc.putText(
          mRgba,
          "[" + center.x + "," + center.y + "]",
          new Point(center.x + 20, center.y + 20),
          Core.FONT_HERSHEY_SIMPLEX,
          0.7,
          new Scalar(255, 255, 255, 255));

      Rect r = facesArray[i];
      // compute the eye area
      Rect eyearea =
          new Rect(
              r.x + r.width / 8,
              (int) (r.y + (r.height / 4.5)),
              r.width - 2 * r.width / 8,
              (int) (r.height / 3.0));
      // split it
      Rect eyearea_right =
          new Rect(
              r.x + r.width / 16,
              (int) (r.y + (r.height / 4.5)),
              (r.width - 2 * r.width / 16) / 2,
              (int) (r.height / 3.0));
      Rect eyearea_left =
          new Rect(
              r.x + r.width / 16 + (r.width - 2 * r.width / 16) / 2,
              (int) (r.y + (r.height / 4.5)),
              (r.width - 2 * r.width / 16) / 2,
              (int) (r.height / 3.0));
      // draw the area - mGray is working grayscale mat, if you want to
      // see area in rgb preview, change mGray to mRgba
      Imgproc.rectangle(mRgba, eyearea_left.tl(), eyearea_left.br(), new Scalar(255, 0, 0, 255), 2);
      Imgproc.rectangle(
          mRgba, eyearea_right.tl(), eyearea_right.br(), new Scalar(255, 0, 0, 255), 2);

      if (learn_frames < 5) {
        teplateR = get_template(mJavaDetectorEye, eyearea_right, 24);
        teplateL = get_template(mJavaDetectorEye, eyearea_left, 24);
        learn_frames++;
      } else {
        // Learning finished, use the new templates for template
        // matching
        match_eye(eyearea_right, teplateR, method);
        match_eye(eyearea_left, teplateL, method);
      }

      // cut eye areas and put them to zoom windows
      Imgproc.resize(mRgba.submat(eyearea_left), mZoomWindow2, mZoomWindow2.size());
      Imgproc.resize(mRgba.submat(eyearea_right), mZoomWindow, mZoomWindow.size());
    }

    return mRgba;
  }