FaceDetector
Stay organized with collections
Save and categorize content based on your preferences.
A FaceDetection
client for finding Face
s in a supplied
image.
A FaceDetector
is created via
FaceDetection.getClient(FaceDetectorOptions)
or FaceDetection.getClient()
,
if you wish to use the default options. For example, the code below creates a face detector
client with default options.
FaceDetector faceDetector = FaceDetection.getClient();
To perform face detection in an image, you first need to create an instance of
InputImage
from a
Bitmap
,
ByteBuffer
,
Image
etc. See
InputImage
documentation for more details. For example, the code below creates an
InputImage
from a
Bitmap
.
InputImage image = InputImage.fromBitmap(bitmap, rotationDegrees);
Then the code below can detect faces in the supplied
InputImage
.
Task<List<Face>> task = faceDetector.process(image);
task.addOnSuccessListener(...).addOnFailureListener(...);
Public Method Summary
abstract void |
close()
Closes the detector and releases its resources.
|
abstract Task<List<Face>>
|
|
abstract Task<List<Face>>
|
|
Inherited Method Summary
From interface java.io.Closeable
From interface java.lang.AutoCloseable
Public Methods
public abstract void close ()
Closes the detector and releases its resources.
public abstract Task<List<Face>>
process (MlImage
image)
Detects human faces from the supplied image.
This is an experimental API in beta version.
Create an MlImage
object using one of MlImage
's
builder methods. See MlImage
documentation for more details.
Note that the width of the provided image cannot be less than 32 if
FaceDetectorOptions.CONTOUR_MODE_ALL
is specified.
If the face detection model has not been downloaded yet when you are depending on
com.google.android.gms:play-services-mlkit-face-detection, the returned Task
will
contain an MlKitException
with error code MlKitException.UNAVAILABLE
.
If there is no face gets detected, the returned Task
will
contain an empty List
.
Detects human faces from the supplied image.
Create an InputImage
object using one of InputImage
's
factory methods. See InputImage
documentation for more details.
Note that the width of the provided image cannot be less than 32 if
FaceDetectorOptions.CONTOUR_MODE_ALL
is specified.
If the face detection model has not been downloaded yet when you are depending on
com.google.android.gms:play-services-mlkit-face-detection, the returned Task
will
contain an MlKitException
with error code MlKitException.UNAVAILABLE
.
If there is no face gets detected, the returned Task
will
contain an empty List
.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[null,null,["Last updated 2024-10-31 UTC."],[[["\u003cp\u003e\u003ccode\u003eFaceDetector\u003c/code\u003e is a client for finding faces within images, created using \u003ccode\u003eFaceDetection.getClient()\u003c/code\u003e with optional configurations.\u003c/p\u003e\n"],["\u003cp\u003eIt uses \u003ccode\u003eInputImage\u003c/code\u003e, created from sources like Bitmaps, for processing and detecting faces.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eprocess()\u003c/code\u003e method asynchronously detects faces in the input image, returning a list of \u003ccode\u003eFace\u003c/code\u003e objects or an empty list if none are found.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFaceDetector\u003c/code\u003e can be closed using the \u003ccode\u003eclose()\u003c/code\u003e method to release resources.\u003c/p\u003e\n"],["\u003cp\u003eFor contour detection with \u003ccode\u003eFaceDetectorOptions.CONTOUR_MODE_ALL\u003c/code\u003e, the input image width should be at least 32 pixels.\u003c/p\u003e\n"]]],["The `FaceDetector` interface detects faces in images. To use it, create a `FaceDetector` instance via `FaceDetection.getClient()`, then create an `InputImage` from a source like a `Bitmap`. The `process()` method, which takes either an `InputImage` or `MlImage`, detects faces and returns a `Task` containing a list of `Face` objects. The `close()` method releases detector resources. Ensure the image width is at least 32 if using contour mode.\n"],null,["# FaceDetector\n\npublic interface **FaceDetector** extends [Detector](/android/reference/com/google/mlkit/vision/interfaces/Detector)\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Face](/android/reference/com/google/mlkit/vision/face/Face)\\\u003e\\\u003e, [OptionalModuleApi](/android/reference/com/google/android/gms/common/api/OptionalModuleApi) \nA [FaceDetection](/android/reference/com/google/mlkit/vision/face/FaceDetection)\nclient for finding [Face](/android/reference/com/google/mlkit/vision/face/Face)s in a supplied\nimage.\n\nA [FaceDetector](/android/reference/com/google/mlkit/vision/face/FaceDetector)\nis created via [FaceDetection.getClient(FaceDetectorOptions)](/android/reference/com/google/mlkit/vision/face/FaceDetection#getClient(com.google.mlkit.vision.face.FaceDetectorOptions)) or [FaceDetection.getClient()](/android/reference/com/google/mlkit/vision/face/FaceDetection#getClient()),\nif you wish to use the default options. For example, the code below creates a face detector\nclient with default options. \n\n FaceDetector faceDetector = FaceDetection.getClient();\n \nTo perform face detection in an image, you first need to create an instance of `\n`[InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) from a [Bitmap](//developer.android.com/reference/android/graphics/Bitmap.html), [ByteBuffer](//developer.android.com/reference/java/nio/ByteBuffer.html), [Image](//developer.android.com/reference/android/media/Image.html) etc. See [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) documentation for more details. For example, the code below creates an [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) from a [Bitmap](//developer.android.com/reference/android/graphics/Bitmap.html). \n\n InputImage image = InputImage.fromBitmap(bitmap, rotationDegrees);\n \nThen the code below can detect faces in the supplied [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage). \n\n Task\u003cList\u003cFace\u003e\u003e task = faceDetector.process(image);\n task.addOnSuccessListener(...).addOnFailureListener(...);\n \n### Public Method Summary\n\n|------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](/android/reference/com/google/mlkit/vision/face/FaceDetector#close())() Closes the detector and releases its resources. |\n| abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Face](/android/reference/com/google/mlkit/vision/face/Face)\\\u003e\\\u003e | [process](/android/reference/com/google/mlkit/vision/face/FaceDetector#process(com.google.android.odml.image.MlImage))([MlImage](/android/reference/com/google/android/odml/image/MlImage) image) Detects human faces from the supplied image. |\n| abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Face](/android/reference/com/google/mlkit/vision/face/Face)\\\u003e\\\u003e | [process](/android/reference/com/google/mlkit/vision/face/FaceDetector#process(com.google.mlkit.vision.common.InputImage))([InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) image) Detects human faces from the supplied image. |\n\n### Inherited Method Summary\n\nFrom interface java.io.Closeable \n\n|---------------|---------|\n| abstract void | close() |\n\nFrom interface java.lang.AutoCloseable \n\n|---------------|---------|\n| abstract void | close() |\n\nPublic Methods\n--------------\n\n#### public abstract void **close** ()\n\nCloses the detector and releases its resources. \n\n#### public abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Face](/android/reference/com/google/mlkit/vision/face/Face)\\\u003e\\\u003e\n**process** ([MlImage](/android/reference/com/google/android/odml/image/MlImage) image)\n\nDetects human faces from the supplied image.\n\nThis is an experimental API in beta version.\n\nCreate an [MlImage](/android/reference/com/google/android/odml/image/MlImage)\nobject using one of [MlImage](/android/reference/com/google/android/odml/image/MlImage)'s\nbuilder methods. See [MlImage](/android/reference/com/google/android/odml/image/MlImage)\ndocumentation for more details.\n\nNote that the width of the provided image cannot be less than 32 if [FaceDetectorOptions.CONTOUR_MODE_ALL](/android/reference/com/google/mlkit/vision/face/FaceDetectorOptions#CONTOUR_MODE_ALL) is specified.\n\nIf the face detection model has not been downloaded yet when you are depending on\ncom.google.android.gms:play-services-mlkit-face-detection, the returned [Task](/android/reference/com/google/android/gms/tasks/Task) will\ncontain an [MlKitException](/android/reference/com/google/mlkit/common/MlKitException)\nwith error code [MlKitException.UNAVAILABLE](/android/reference/com/google/mlkit/common/MlKitException#UNAVAILABLE).\n\nIf there is no face gets detected, the returned [Task](/android/reference/com/google/android/gms/tasks/Task) will\ncontain an empty [List](//developer.android.com/reference/java/util/List.html). \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that asynchronously returns a [List](//developer.android.com/reference/java/util/List.html) of detected [Face](/android/reference/com/google/mlkit/vision/face/Face)s \n\n#### public abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Face](/android/reference/com/google/mlkit/vision/face/Face)\\\u003e\\\u003e\n**process** ([InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) image)\n\nDetects human faces from the supplied image.\n\nCreate an [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\nobject using one of [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)'s\nfactory methods. See [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\ndocumentation for more details.\n\nNote that the width of the provided image cannot be less than 32 if [FaceDetectorOptions.CONTOUR_MODE_ALL](/android/reference/com/google/mlkit/vision/face/FaceDetectorOptions#CONTOUR_MODE_ALL) is specified.\n\nIf the face detection model has not been downloaded yet when you are depending on\ncom.google.android.gms:play-services-mlkit-face-detection, the returned [Task](/android/reference/com/google/android/gms/tasks/Task) will\ncontain an [MlKitException](/android/reference/com/google/mlkit/common/MlKitException)\nwith error code [MlKitException.UNAVAILABLE](/android/reference/com/google/mlkit/common/MlKitException#UNAVAILABLE).\n\nIf there is no face gets detected, the returned [Task](/android/reference/com/google/android/gms/tasks/Task) will\ncontain an empty [List](//developer.android.com/reference/java/util/List.html). \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that asynchronously returns a [List](//developer.android.com/reference/java/util/List.html) of detected [Face](/android/reference/com/google/mlkit/vision/face/Face)s"]]