ImageTensor
docarray.typing.tensor.image.image_ndarray
ImageNdArray
Bases: AbstractImageTensor
, NdArray
Subclass of NdArray
, to represent an image tensor.
Adds image-specific features to the tensor.
For instance the ability convert the tensor back to image bytes which are
optimized to send over the wire.
from typing import Optional
from docarray import BaseDoc
from docarray.typing import ImageBytes, ImageNdArray, ImageUrl
class MyImageDoc(BaseDoc):
title: str
tensor: Optional[ImageNdArray] = None
url: Optional[ImageUrl] = None
bytes: Optional[ImageBytes] = None
# from url
doc = MyImageDoc(
title='my_second_audio_doc',
url="https://upload.wikimedia.org/wikipedia/commons/8/80/"
"Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg",
)
doc.tensor = doc.url.load()
doc.bytes = doc.tensor.to_bytes()
Source code in docarray/typing/tensor/image/image_ndarray.py
docarray.typing.tensor.image.abstract_image_tensor
AbstractImageTensor
Bases: AbstractTensor
, ABC
Source code in docarray/typing/tensor/image/abstract_image_tensor.py
__docarray_validate_getitem__(item)
classmethod
This method validates the input to AbstractTensor.__class_getitem__
.
It is called at "class creation time", i.e. when a class is created with syntax of the form AnyTensor[shape].
The default implementation tries to cast any item
to a tuple of ints.
A subclass can override this method to implement custom validation logic.
The output of this is eventually passed to
AbstractTensor.__docarray_validate_shape__
as its shape
argument.
Raises ValueError
if the input item
does not pass validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Any
|
The item to validate, passed to |
required |
Returns:
Type | Description |
---|---|
Tuple[int]
|
The validated item == the target shape of this tensor. |
Source code in docarray/typing/tensor/abstract_tensor.py
__docarray_validate_shape__(t, shape)
classmethod
Every tensor has to implement this method in order to enable syntax of the form AnyTensor[shape]. It is called when a tensor is assigned to a field of this type. i.e. when a tensor is passed to a Document field of type AnyTensor[shape].
The intended behaviour is as follows:
- If the shape of
t
is equal toshape
, returnt
. - If the shape of
t
is not equal toshape
, but can be reshaped toshape
, returnt
reshaped toshape
. - If the shape of
t
is not equal toshape
and cannot be reshaped toshape
, raise a ValueError.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
T
|
The tensor to validate. |
required |
shape |
Tuple[Union[int, str], ...]
|
The shape to validate against. |
required |
Returns:
Type | Description |
---|---|
T
|
The validated tensor. |
Source code in docarray/typing/tensor/abstract_tensor.py
__getitem__(item)
abstractmethod
__iter__()
abstractmethod
__setitem__(index, value)
abstractmethod
display()
Display image data from tensor in notebook.
Source code in docarray/typing/tensor/image/abstract_image_tensor.py
get_comp_backend()
abstractmethod
staticmethod
save(file_path)
Save image tensor to an image file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to an image file. If file is a string, open the file by that name, otherwise treat it as a file-like object. |
required |
Source code in docarray/typing/tensor/image/abstract_image_tensor.py
to_bytes(format='PNG')
Convert image tensor to ImageBytes
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format |
str
|
the image format use to store the image, can be 'PNG' , 'JPG' ... |
'PNG'
|
Returns:
Type | Description |
---|---|
ImageBytes
|
an ImageBytes object |
Source code in docarray/typing/tensor/image/abstract_image_tensor.py
to_protobuf()
abstractmethod
docarray.typing.tensor.image.image_tensorflow_tensor
ImageTensorFlowTensor
Bases: TensorFlowTensor
, AbstractImageTensor
Subclass of TensorFlowTensor
,
to represent an image tensor. Adds image-specific features to the tensor.
For instance the ability convert the tensor back to
ImageBytes
which are
optimized to send over the wire.
from typing import Optional
from docarray import BaseDoc
from docarray.typing import ImageBytes, ImageTensorFlowTensor, ImageUrl
class MyImageDoc(BaseDoc):
title: str
tensor: Optional[ImageTensorFlowTensor]
url: Optional[ImageUrl]
bytes: Optional[ImageBytes]
doc = MyImageDoc(
title='my_second_image_doc',
url="https://upload.wikimedia.org/wikipedia/commons/8/80/"
"Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg",
)
doc.tensor = doc.url.load()
doc.bytes = doc.tensor.to_bytes()
Source code in docarray/typing/tensor/image/image_tensorflow_tensor.py
docarray.typing.tensor.image.image_torch_tensor
ImageTorchTensor
Bases: AbstractImageTensor
, TorchTensor
Subclass of TorchTensor
, to represent an image tensor.
Adds image-specific features to the tensor.
For instance the ability convert the tensor back to
ImageBytes
which are
optimized to send over the wire.
from typing import Optional
from docarray import BaseDoc
from docarray.typing import ImageBytes, ImageTorchTensor, ImageUrl
class MyImageDoc(BaseDoc):
title: str
tensor: Optional[ImageTorchTensor] = None
url: Optional[ImageUrl] = None
bytes: Optional[ImageBytes] = None
doc = MyImageDoc(
title='my_second_image_doc',
url="https://upload.wikimedia.org/wikipedia/commons/8/80/"
"Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg",
)
doc.tensor = doc.url.load()
doc.bytes = doc.tensor.to_bytes()