public final class FaceDetector

  1. Object
  2. FaceDetector

ImplementsAutoCloseable, VisionAnalyzer<Face[]>

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.

Constructors

public FaceDetector()Creates an analyzer using the platform default backend and options.
public FaceDetector(VisionOptions options)Creates a reusable analyzer with explicit backend and result options.

Methods

public final synchronized boolean isSupported()Tests the exact feature/backend pair configured for this analyzer.
public final synchronized AsyncResource<Face[]> process(VisionImage image)Starts one analysis using the retained native backend.
public final synchronized void close()Idempotently releases the retained native backend.

Inherited methods

Constructor details

FaceDetector

public FaceDetector()
Creates an analyzer using the platform default backend and options.

FaceDetector

public FaceDetector(VisionOptions options)
Creates a reusable analyzer with explicit backend and result options.

Parameters

options VisionOptions
configuration captured by this analyzer; null uses defaults

Method details

isSupported

public final synchronized boolean isSupported()
Tests the exact feature/backend pair configured for this analyzer.

Returns

whether this analyzer’s feature/backend is available and open

process

public final synchronized AsyncResource<Face[]> process(VisionImage image)
Starts one analysis using the retained native backend.

Parameters

image VisionImage
immutable encoded or raw input

Returns

asynchronous typed result

close

public final synchronized void close()
Idempotently releases the retained native backend.