TensorImage

public class TensorImage

TensorImage is the wrapper class for Image object. When using image processing utils in TFLite.support library, it's common to convert image objects in variant types to TensorImage at first.

At present, only RGB images are supported, and the A channel is always ignored.

Details of data storage: a TensorImage object may have 2 potential sources of truth: a Bitmap or a TensorBuffer. TensorImage maintains the state and only converts one to the other when needed. A typical use case of TensorImage is to first load a Bitmap image, then process it using ImageProcessor, and finally get the underlying ByteBuffer of the TensorBuffer and feed it into the TFLite interpreter.

IMPORTANT: to achieve the best performance, TensorImage avoids copying data whenever it's possible. Therefore, it doesn't own its data. Callers should not modify data objects those are passed to load(Bitmap) or load(TensorBuffer, ColorSpaceType).

IMPORTANT: all methods are not proved thread-safe.

Public Constructors

TensorImage()
Initializes a TensorImage object.
TensorImage(DataType dataType)
Initializes a TensorImage object with the specified data type.

Public Methods

static TensorImage
createFrom(TensorImage src, DataType dataType)
Creates a deep-copy of a given TensorImage with the desired data type.
static TensorImage
fromBitmap(Bitmap bitmap)
Initializes a TensorImage object of DataType.UINT8 with a Bitmap .
Bitmap
getBitmap()
Returns a Bitmap representation of this TensorImage.
ByteBuffer
getBuffer()
Returns a ByteBuffer representation of this TensorImage with the expected data type.
ColorSpaceType
getColorSpaceType()
Gets the color space type of this TensorImage.
DataType
getDataType()
Gets the data type of this TensorImage.
int
getHeight()
Gets the image height.
Image
getMediaImage()
Returns an Image representation of this TensorImage.
TensorBuffer
getTensorBuffer()
Returns a TensorBuffer representation of this TensorImage with the expected data type.
int
getWidth()
Gets the image width.
void
load(TensorBuffer buffer, ColorSpaceType colorSpaceType)
Loads a TensorBuffer containing pixel values with the specific ColorSpaceType.
void
load(Bitmap bitmap)
Loads a Bitmap image object into this TensorImage.
void
load(int[] pixels, int[] shape)
Loads an int array as RGB pixels into this TensorImage, representing the pixels inside.
void
load(float[] pixels, int[] shape)
Loads a float array as RGB pixels into this TensorImage, representing the pixels inside.
void
load(ByteBuffer buffer, ImageProperties imageProperties)
Loads a ByteBuffer containing pixel values with the specific ImageProperties.
void
load(TensorBuffer buffer, ImageProperties imageProperties)
Loads a TensorBuffer containing pixel values with the specific ImageProperties.
void
load(Image image)
Loads an Image object into this TensorImage.

Inherited Methods

Public Constructors

public TensorImage ()

Initializes a TensorImage object.

Note: the data type of this TensorImage is DataType.UINT8. Use TensorImage(DataType) if other data types are preferred.

public TensorImage (DataType dataType)

Initializes a TensorImage object with the specified data type.

When getting a TensorBuffer or a ByteBuffer from this TensorImage, such as using getTensorBuffer() and getBuffer(), the data values will be converted to the specified data type.

Note: the shape of a TensorImage is not fixed. It can be adjusted to the shape of the image being loaded to this TensorImage.

Parameters
dataType the expected data type of the resulting TensorBuffer. The type is always fixed during the lifetime of the TensorImage. To convert the data type, use createFrom(TensorImage, DataType) to create a copy and convert data type at the same time.
Throws
IllegalArgumentException if dataType is neither DataType.UINT8 nor DataType.FLOAT32

Public Methods

public static TensorImage createFrom (TensorImage src, DataType dataType)

Creates a deep-copy of a given TensorImage with the desired data type.

Parameters
src the TensorImage to copy from
dataType the expected data type of newly created TensorImage