Provides an interface for performing image description.
It supports both standard and streaming inferences, as well as methods for preparing and cleaning up model resources.
Typical usage:
ImageDescriptionRequest request = ImageDescriptionRequest.builder(bitmap).build();
ListenableFuture future = imageDescriber.runInference(request);
Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(ImageDescriptionResult result) {
Log.d(TAG, result.getDescription());
}
@Override
public void onFailure(Throwable t) {
Log.e(TAG, "Failed to describe", t);
}
}, executor);
Public Method Summary
ListenableFuture<Integer> |
checkFeatureStatus()
Checks the current availability status of the image description feature.
|
void |
close()
Releases resources associated with the image description engine.
|
ListenableFuture<Void> |
downloadFeature(DownloadCallback callback)
Downloads the required model assets for the image description feature if they
are not already available.
|
ListenableFuture<String> |
getBaseModelName()
Returns the name of the base model used by this image describer instance.
|
ListenableFuture<Void> |
prepareInferenceEngine()
Prepares the inference engine for use by loading necessary models and
initializing runtime components.
|
ListenableFuture<ImageDescriptionResult> |
runInference(ImageDescriptionRequest
request, StreamingCallback streamingCallback)
Performs streaming image description inference on the provided image request.
|
ListenableFuture<ImageDescriptionResult> |
runInference(ImageDescriptionRequest
request)
Performs asynchronous image description on the provided image request.
|
Inherited Method Summary
Public Methods
public ListenableFuture<Integer> checkFeatureStatus ()
Checks the current availability status of the image description feature.
Returns
- a
ListenableFuture
resolving to aFeatureStatus
indicating feature readiness.
public void close ()
Releases resources associated with the image description engine.
This should be called when the image describer is no longer needed. Can be safely called multiple times.
public ListenableFuture<Void> downloadFeature (DownloadCallback callback)
Downloads the required model assets for the image description feature if they are not already available.
Use this method to proactively download models before inference. The provided
DownloadCallback
is invoked to report progress and completion status.
Parameters
callback | a non-null DownloadCallback
for receiving download updates. |
---|
Returns
- a
ListenableFuture
that completes when the download finishes or fails. It completes immediately if already downloaded.
public ListenableFuture<String> getBaseModelName ()
Returns the name of the base model used by this image describer instance.
The model name may be used for logging, debugging, or feature gating purposes.
Returns
- a
ListenableFuture
resolving to the base model name.
public ListenableFuture<Void> prepareInferenceEngine ()
Prepares the inference engine for use by loading necessary models and initializing runtime components.
If the models haven't been downloaded yet, it will trigger the download and wait for it to complete first.
While calling this method is optional, we recommend invoking it well before the first inference call to reduce the latency of the initial inference.
Returns
- a cancellable
ListenableFuture
that completes when the engine is ready.
public ListenableFuture<ImageDescriptionResult> runInference (ImageDescriptionRequest request, StreamingCallback streamingCallback)
Performs streaming image description inference on the provided image request.
Partial results are delivered incrementally through the provided StreamingCallback
.
This is useful to build more responsive UI. Streaming can be interrupted by a
GenAiException
.
In that case, consider removing any already streamed partial output from the UI.
The returned ListenableFuture
is cancellable. If the inference is no longer needed (e.g., the user navigates away),
calling future.cancel(true)
will attempt to interrupt the process and free
up resources.
Note that inference requests may fail under certain conditions such as:
- Invalid or malformed input in the
ImageDescriptionRequest
- Exceeded usage quota or failed safety checks
GenAiException
handling should be implemented when attaching the callback to the future.
Parameters
request | a non-null
ImageDescriptionRequest containing the input image. |
---|---|
streamingCallback | a non-null StreamingCallback
for receiving streamed results. |
Returns
- a cancellable
ListenableFuture
resolving to the finalImageDescriptionResult
.
public ListenableFuture<ImageDescriptionResult> runInference (ImageDescriptionRequest request)
Performs asynchronous image description on the provided image request.
This is the standard, non-streaming version of inference. The full description is returned once the model completes processing.
This method is non-blocking. To handle the result, callers should attach a listener
to the returned ListenableFuture
using
Futures.addCallback(ListenableFuture, FutureCallback super V>
, Executor)
or similar methods. The inference runs on background threads and
may complete at a later time depending on model availability and input size.
The returned ListenableFuture
is cancellable. If the inference is no longer needed (e.g., the user navigates away),
calling future.cancel(true)
will attempt to interrupt the process and free
up resources.
Note that inference requests may fail under certain conditions such as:
- Invalid or malformed input in the
ImageDescriptionRequest
- Exceeded usage quota or failed safety checks
GenAiException
handling should be implemented when attaching the callback to the future.
Parameters
request | a non-null
ImageDescriptionRequest containing input image. |
---|
Returns
- a cancellable
ListenableFuture
that resolves to aImageDescriptionResult
.