Class FaceDetector

java.lang.Object
com.codename1.ai.vision.FaceDetector
All Implemented Interfaces:
VisionAnalyzer<Face[]>, AutoCloseable

public final class FaceDetector extends Object

Finds faces, their bounding boxes, their head angles, and -- where the backend supports it -- whether they are smiling.

Counting faces in the live camera, which is the usual shape for a selfie screen or a "hold still" capture:

VisionCameraView<Face[]> view =
        new VisionCameraView<Face[]>(new FaceDetector());
view.setFacing(CameraFacing.FRONT);
view.setListener(new VisionPipelineListener<Face[]>() {
    public void result(Face[] faces, VisionImage source) {
        shutter.setEnabled(faces.length == 1);
        status.setText(faces.length == 1
                ? "Looking good" : "Fit exactly one face in the frame");
    }
    public void error(Throwable error) {
        Log.e(error);
    }
});

Form form = new Form("Selfie", new BorderLayout());
form.add(BorderLayout.CENTER, view);
form.add(BorderLayout.SOUTH, BoxLayout.encloseY(status, shutter));
form.show();

Or over a still image, to crop to the face:

FaceDetector detector = new FaceDetector();
detector.process(VisionImage.fromImage(photo)).ready(faces -> {
    if (faces.length > 0) {
        Rectangle box = faces[0].getBounds()
                .toBounds(0, 0, photo.getWidth(), photo.getHeight());
        avatar.setIcon(photo.subImage(box.getX(), box.getY(),
                box.getWidth(), box.getHeight(), true));
    }
    detector.close();
}).except(error -> {
    Log.e(error);
    detector.close();
});

Read individual landmarks with Face.getLandmark(String) and the FaceLandmarks constants. Not every backend fills in every field: Face.getSmilingProbability() and Face.getTrackingId() are negative when unavailable.

  • Constructor Details

    • FaceDetector

      public FaceDetector()
      Creates an analyzer using the platform default backend and options.
      See Also:
    • FaceDetector

      public FaceDetector(VisionOptions options)
      Creates a reusable analyzer with explicit backend and result options.
      Parameters:
      options - configuration captured by this analyzer; null uses defaults
  • Method Details

    • isSupported

      public final boolean isSupported()
      Description copied from interface: VisionAnalyzer
      Tests the exact feature/backend pair configured for this analyzer.
      Specified by:
      isSupported in interface VisionAnalyzer<T>
      Returns:
      whether this analyzer's feature/backend is available and open
    • process

      public final AsyncResource<Face[]> process(VisionImage image)
      Starts one analysis using the retained native backend.
      Specified by:
      process in interface VisionAnalyzer<T>
      Parameters:
      image - immutable encoded or raw input
      Returns:
      asynchronous typed result
    • close

      public final void close()
      Idempotently releases the retained native backend.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface VisionAnalyzer<T>