Class FaceDetector
java.lang.Object
com.codename1.ai.vision.FaceDetector
- All Implemented Interfaces:
VisionAnalyzer<Face[]>, AutoCloseable
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 Summary
ConstructorsConstructorDescriptionCreates an analyzer using the platform default backend and options.FaceDetector(VisionOptions options) Creates a reusable analyzer with explicit backend and result options. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Idempotently releases the retained native backend.final booleanTests the exact feature/backend pair configured for this analyzer.final AsyncResource<Face[]> process(VisionImage image) Starts one analysis using the retained native backend.
-
Constructor Details
-
FaceDetector
public FaceDetector()Creates an analyzer using the platform default backend and options.- See Also:
-
FaceDetector
Creates a reusable analyzer with explicit backend and result options.- Parameters:
options- configuration captured by this analyzer;nulluses defaults
-
-
Method Details
-
isSupported
public final boolean isSupported()Description copied from interface:VisionAnalyzerTests the exact feature/backend pair configured for this analyzer.- Specified by:
isSupportedin interfaceVisionAnalyzer<T>- Returns:
- whether this analyzer's feature/backend is available and open
-
process
Starts one analysis using the retained native backend.- Specified by:
processin interfaceVisionAnalyzer<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:
closein interfaceAutoCloseable- Specified by:
closein interfaceVisionAnalyzer<T>
-