ObjectDetector

  • ObjectDetector is used to find DetectedObjects within images in Android apps.

  • You create an ObjectDetector instance using ObjectDetection.getClient(ObjectDetectorOptionsBase).

  • Object detection is performed by passing an InputImage or MlImage to the process() method of the detector.

  • After processing, a Task is returned that provides a list of DetectedObjects, or an empty list if no objects are found.

  • When finished using the detector, release its resources by calling the close() method.

public interface ObjectDetector extends Detector<List<DetectedObject>>

An ObjectDetection client for finding DetectedObjects in a supplied image.

An ObjectDetector is created via ObjectDetection.getClient(ObjectDetectorOptionsBase).

Example:

ObjectDetector objectDetector = ObjectDetection.getClient(options);
 

To perform object detection in an image, you first need to create an instance of InputImage from a Bitmap, ByteBuffer, 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 objects in the supplied InputImage.

Task<List<DetectedObject>> task = objectDetector.process(image);
 task.addOnSuccessListener(...).addOnFailureListener(...);
 

Public Method Summary

abstract void
close()
Closes the detector and releases its resources.
abstract Task<List<DetectedObject>>
process(MlImage image)
Detects objects from a supplied image.
abstract Task<List<DetectedObject>>
process(InputImage image)
Detects objects from a supplied image.

Inherited Method Summary

Public Methods

public abstract void close ()

Closes the detector and releases its resources.

public abstract Task<List<DetectedObject>> process (MlImage image)

Detects objects from a 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 and height of the provided image cannot be less than 32.

When calling this method on an ObjectDetector created with CustomObjectDetectorOptions, the returned Task will contain an MlKitException with specific error code in the following cases: (1) with error code MlKitException.NOT_FOUND if cannot find the custom classifier model file; (2) with error code MlKitException.INVALID_ARGUMENT if the custom model file is not compatible with this SDK.

Returns

public abstract Task<List<DetectedObject>> process (InputImage image)

Detects objects from a supplied image.

Create an InputImage object using one of InputImage's factory methods. See InputImage documentation for more details.

Note that the width and height of the provided image cannot be less than 32.

When calling this method on an ObjectDetector created with CustomObjectDetectorOptions, the returned Task will contain an MlKitException with specific error code in the following cases: (1) with error code MlKitException.NOT_FOUND if cannot find the custom classifier model file; (2) with error code MlKitException.INVALID_ARGUMENT if the custom model file is not compatible with this SDK.

Returns