| [ Web Proxy ] |
| Viewing: https://docs.docarray.org/API_reference/array/../../../../API_reference/typing/tensor/video/ | [Back] [Original] |
docarray.typing.tensor.video.video_ndarray
VideoNdArray
Bases: NdArray, VideoTensorMixin
Subclass of NdArray, to represent a video tensor.
Adds video-specific features to the tensor.
from typing import Optional
import numpy as np
from pydantic import parse_obj_as
from docarray import BaseDoc
from docarray.typing import VideoNdArray, VideoUrl
class MyVideoDoc(BaseDoc):
title: str
url: Optional[VideoUrl] = None
video_tensor: Optional[VideoNdArray] = None
doc_1 = MyVideoDoc(
title='my_first_video_doc',
video_tensor=np.random.random((100, 224, 224, 3)),
)
doc_2 = MyVideoDoc(
title='my_second_video_doc',
url='https://github.com/docarray/docarray/blob/main/tests/toydata/mov_bbb.mp4?raw=true',
)
doc_2.video_tensor = parse_obj_as(VideoNdArray, doc_2.url.load().video)
# doc_2.video_tensor.save(file_path='/tmp/file_2.mp4')
docarray/typing/tensor/video/video_ndarray.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. |
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:
t is equal to shape, return t.t is not equal to shape,
but can be reshaped to shape, return t reshaped to shape.t is not equal to shape
and cannot be reshaped to shape, 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. |
docarray/typing/tensor/abstract_tensor.py
__getitem__(item)
abstractmethod
__iter__()
abstractmethod
__setitem__(index, value)
abstractmethod
display(audio=None)
Display video data from tensor in notebook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio |
Optional[AudioTensor]
|
sound to play with video tensor |
None
|
docarray/typing/tensor/video/video_tensor_mixin.py
from_protobuf(pb_msg)
classmethod
Read ndarray from a proto msg
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pb_msg |
NdArrayProto
|
|
required |
Returns:
| Type | Description |
|---|---|
T
|
a numpy array |
docarray/typing/tensor/ndarray.py
get_comp_backend()
staticmethod
Return the computational backend of the tensor
Source code indocarray/typing/tensor/ndarray.py
save(file_path, audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Save video tensor to a .mp4 file.
import numpy as np
from docarray import BaseDoc
from docarray.typing.tensor.audio.audio_tensor import AudioTensor
from docarray.typing.tensor.video.video_tensor import VideoTensor
class MyDoc(BaseDoc):
video_tensor: VideoTensor
audio_tensor: AudioTensor
doc = MyDoc(
video_tensor=np.random.randint(low=0, high=256, size=(10, 200, 300, 3)),
audio_tensor=np.random.randn(100, 1, 1024).astype("float32"),
)
doc.video_tensor.save(
file_path="/tmp/mp_.mp4",
audio_tensor=doc.audio_tensor,
audio_format="flt",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
Union[str, BytesIO]
|
path to a .mp4 file. If file is a string, open the file by that name, otherwise treat it as a file-like object. |
required |
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
docarray/typing/tensor/video/video_tensor_mixin.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
to_bytes(audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Convert video tensor to VideoBytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
Returns:
| Type | Description |
|---|---|
VideoBytes
|
a VideoBytes object |
docarray/typing/tensor/video/video_tensor_mixin.py
to_protobuf()
Transform self into a NdArrayProto protobuf message
Source code indocarray/typing/tensor/ndarray.py
unwrap()
Return the original ndarray without any memory copy.
The original view rest intact and is still a Document NdArray
but the return object is a pure np.ndarray but both object share
the same memory layout.
from docarray.typing import NdArray
import numpy as np
from pydantic import parse_obj_as
t1 = parse_obj_as(NdArray, np.zeros((3, 224, 224)))
t2 = t1.unwrap()
# here t2 is a pure np.ndarray but t1 is still a Docarray NdArray
# But both share the same underlying memory
Returns:
| Type | Description |
|---|---|
ndarray
|
a |
docarray/typing/tensor/ndarray.py
docarray.typing.tensor.video.video_tensor_mixin
VideoTensorMixin
Bases: AbstractTensor, ABC
docarray/typing/tensor/video/video_tensor_mixin.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
__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. |
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:
t is equal to shape, return t.t is not equal to shape,
but can be reshaped to shape, return t reshaped to shape.t is not equal to shape
and cannot be reshaped to shape, 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. |
docarray/typing/tensor/abstract_tensor.py
__getitem__(item)
abstractmethod
__iter__()
abstractmethod
__setitem__(index, value)
abstractmethod
display(audio=None)
Display video data from tensor in notebook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio |
Optional[AudioTensor]
|
sound to play with video tensor |
None
|
docarray/typing/tensor/video/video_tensor_mixin.py
get_comp_backend()
abstractmethod
staticmethod
save(file_path, audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Save video tensor to a .mp4 file.
import numpy as np
from docarray import BaseDoc
from docarray.typing.tensor.audio.audio_tensor import AudioTensor
from docarray.typing.tensor.video.video_tensor import VideoTensor
class MyDoc(BaseDoc):
video_tensor: VideoTensor
audio_tensor: AudioTensor
doc = MyDoc(
video_tensor=np.random.randint(low=0, high=256, size=(10, 200, 300, 3)),
audio_tensor=np.random.randn(100, 1, 1024).astype("float32"),
)
doc.video_tensor.save(
file_path="/tmp/mp_.mp4",
audio_tensor=doc.audio_tensor,
audio_format="flt",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
Union[str, BytesIO]
|
path to a .mp4 file. If file is a string, open the file by that name, otherwise treat it as a file-like object. |
required |
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
docarray/typing/tensor/video/video_tensor_mixin.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
to_bytes(audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Convert video tensor to VideoBytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
Returns:
| Type | Description |
|---|---|
VideoBytes
|
a VideoBytes object |
docarray/typing/tensor/video/video_tensor_mixin.py
to_protobuf()
abstractmethod
docarray.typing.tensor.video.video_tensorflow_tensor
VideoTensorFlowTensor
Bases: TensorFlowTensor, VideoTensorMixin
Subclass of TensorFlowTensor,
to represent a video tensor. Adds video-specific features to the tensor.
from typing import Optional
import tensorflow as tf
from docarray import BaseDoc
from docarray.typing import VideoTensorFlowTensor, VideoUrl
class MyVideoDoc(BaseDoc):
title: str
url: Optional[VideoUrl]
video_tensor: Optional[VideoTensorFlowTensor]
doc_1 = MyVideoDoc(
title='my_first_video_doc',
video_tensor=tf.random.normal((100, 224, 224, 3)),
)
# doc_1.video_tensor.save(file_path='file_1.mp4')
doc_2 = MyVideoDoc(
title='my_second_video_doc',
url='https://github.com/docarray/docarray/blob/main/tests/toydata/mov_bbb.mp4?raw=true',
)
doc_2.video_tensor = doc_2.url.load().video
# doc_2.video_tensor.save(file_path='file_2.wav')
docarray/typing/tensor/video/video_tensorflow_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. |
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:
t is equal to shape, return t.t is not equal to shape,
but can be reshaped to shape, return t reshaped to shape.t is not equal to shape
and cannot be reshaped to shape, 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. |
docarray/typing/tensor/abstract_tensor.py
__iter__()
__setitem__(index, value)
Set a slice of this tensor's tf.Tensor
docarray/typing/tensor/tensorflow_tensor.py
display(audio=None)
Display video data from tensor in notebook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio |
Optional[AudioTensor]
|
sound to play with video tensor |
None
|
docarray/typing/tensor/video/video_tensor_mixin.py
from_ndarray(value)
classmethod
Create a TensorFlowTensor from a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value |
ndarray
|
the numpy array |
required |
Returns:
| Type | Description |
|---|---|
T
|
a |
docarray/typing/tensor/tensorflow_tensor.py
from_protobuf(pb_msg)
classmethod
Read ndarray from a proto msg.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pb_msg |
NdArrayProto
|
|
required |
Returns:
| Type | Description |
|---|---|
T
|
a |
docarray/typing/tensor/tensorflow_tensor.py
get_comp_backend()
staticmethod
Return the computational backend of the tensor
Source code indocarray/typing/tensor/tensorflow_tensor.py
save(file_path, audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Save video tensor to a .mp4 file.
import numpy as np
from docarray import BaseDoc
from docarray.typing.tensor.audio.audio_tensor import AudioTensor
from docarray.typing.tensor.video.video_tensor import VideoTensor
class MyDoc(BaseDoc):
video_tensor: VideoTensor
audio_tensor: AudioTensor
doc = MyDoc(
video_tensor=np.random.randint(low=0, high=256, size=(10, 200, 300, 3)),
audio_tensor=np.random.randn(100, 1, 1024).astype("float32"),
)
doc.video_tensor.save(
file_path="/tmp/mp_.mp4",
audio_tensor=doc.audio_tensor,
audio_format="flt",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
Union[str, BytesIO]
|
path to a .mp4 file. If file is a string, open the file by that name, otherwise treat it as a file-like object. |
required |
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
docarray/typing/tensor/video/video_tensor_mixin.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
to_bytes(audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Convert video tensor to VideoBytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
Returns:
| Type | Description |
|---|---|
VideoBytes
|
a VideoBytes object |
docarray/typing/tensor/video/video_tensor_mixin.py
to_protobuf()
Transform self into an NdArrayProto protobuf message.
Source code indocarray/typing/tensor/tensorflow_tensor.py
unwrap()
Return the original tf.Tensor without any memory copy.
The original view rest intact and is still a Document TensorFlowTensor
but the return object is a pure tf.Tensor but both object share
the same memory layout.
from docarray.typing import TensorFlowTensor
import tensorflow as tf
t1 = TensorFlowTensor.validate(tf.zeros((3, 224, 224)), None, None)
# here t1 is a docarray TensorFlowTensor
t2 = t1.unwrap()
# here t2 is a pure tf.Tensor but t1 is still a Docarray TensorFlowTensor
Returns:
| Type | Description |
|---|---|
Tensor
|
a |
docarray/typing/tensor/tensorflow_tensor.py
docarray.typing.tensor.video.video_torch_tensor
VideoTorchTensor
Bases: TorchTensor, VideoTensorMixin
Subclass of TorchTensor, to represent a video tensor.
Adds video-specific features to the tensor.
from typing import Optional
import torch
from docarray import BaseDoc
from docarray.typing import VideoTorchTensor, VideoUrl
class MyVideoDoc(BaseDoc):
title: str
url: Optional[VideoUrl] = None
video_tensor: Optional[VideoTorchTensor] = None
doc_1 = MyVideoDoc(
title='my_first_video_doc',
video_tensor=torch.randn(size=(100, 224, 224, 3)),
)
# doc_1.video_tensor.save(file_path='file_1.mp4')
doc_2 = MyVideoDoc(
title='my_second_video_doc',
url='https://github.com/docarray/docarray/blob/main/tests/toydata/mov_bbb.mp4?raw=true',
)
doc_2.video_tensor = doc_2.url.load().video
# doc_2.video_tensor.save(file_path='file_2.wav')
docarray/typing/tensor/video/video_torch_tensor.py
__deepcopy__(memo)
Custom implementation of deepcopy for TorchTensor to avoid storage sharing issues.
Source code indocarray/typing/tensor/torch_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. |
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:
t is equal to shape, return t.t is not equal to shape,
but can be reshaped to shape, return t reshaped to shape.t is not equal to shape
and cannot be reshaped to shape, 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. |
docarray/typing/tensor/abstract_tensor.py
__getitem__(item)
abstractmethod
__iter__()
abstractmethod
__setitem__(index, value)
abstractmethod
display(audio=None)
Display video data from tensor in notebook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio |
Optional[AudioTensor]
|
sound to play with video tensor |
None
|
docarray/typing/tensor/video/video_tensor_mixin.py
from_ndarray(value)
classmethod
Create a TorchTensor from a numpy array
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value |
ndarray
|
the numpy array |
required |
Returns:
| Type | Description |
|---|---|
T
|
a |
docarray/typing/tensor/torch_tensor.py
from_protobuf(pb_msg)
classmethod
Read ndarray from a proto msg
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pb_msg |
NdArrayProto
|
|
required |
Returns:
| Type | Description |
|---|---|
T
|
a |
docarray/typing/tensor/torch_tensor.py
get_comp_backend()
staticmethod
Return the computational backend of the tensor
Source code indocarray/typing/tensor/torch_tensor.py
new_empty(*args, **kwargs)
This method enables the deepcopy of TorchTensor by returning another instance of this subclass.
If this function is not implemented, the deepcopy will throw an RuntimeError from Torch.
docarray/typing/tensor/torch_tensor.py
save(file_path, audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Save video tensor to a .mp4 file.
import numpy as np
from docarray import BaseDoc
from docarray.typing.tensor.audio.audio_tensor import AudioTensor
from docarray.typing.tensor.video.video_tensor import VideoTensor
class MyDoc(BaseDoc):
video_tensor: VideoTensor
audio_tensor: AudioTensor
doc = MyDoc(
video_tensor=np.random.randint(low=0, high=256, size=(10, 200, 300, 3)),
audio_tensor=np.random.randn(100, 1, 1024).astype("float32"),
)
doc.video_tensor.save(
file_path="/tmp/mp_.mp4",
audio_tensor=doc.audio_tensor,
audio_format="flt",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
Union[str, BytesIO]
|
path to a .mp4 file. If file is a string, open the file by that name, otherwise treat it as a file-like object. |
required |
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
docarray/typing/tensor/video/video_tensor_mixin.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
to_bytes(audio_tensor=None, video_frame_rate=24, video_codec='h264', audio_frame_rate=48000, audio_codec='aac', audio_format='fltp')
Convert video tensor to VideoBytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_tensor |
Optional[AudioTensor]
|
AudioTensor containing the video's soundtrack. |
None
|
video_frame_rate |
int
|
video frames per second. |
24
|
video_codec |
str
|
the name of a video decoder/encoder. |
'h264'
|
audio_frame_rate |
int
|
audio frames per second. |
48000
|
audio_codec |
str
|
the name of an audio decoder/encoder. |
'aac'
|
audio_format |
str
|
the name of one of the audio formats supported by PyAV, such as 'flt', 'fltp', 's16' or 's16p'. |
'fltp'
|
Returns:
| Type | Description |
|---|---|
VideoBytes
|
a VideoBytes object |
docarray/typing/tensor/video/video_tensor_mixin.py
to_protobuf()
Transform self into a NdArrayProto protobuf message
docarray/typing/tensor/torch_tensor.py
unwrap()
Return the original torch.Tensor without any memory copy.
The original view rest intact and is still a Document TorchTensor
but the return object is a pure torch.Tensor but both object share
the same memory layout.
from docarray.typing import TorchTensor
import torch
from pydantic import parse_obj_as
t = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224))
# here t is a docarray TorchTensor
t2 = t.unwrap()
# here t2 is a pure torch.Tensor but t1 is still a Docarray TorchTensor
# But both share the same underlying memory
Returns:
| Type | Description |
|---|---|
Tensor
|
a |
docarray/typing/tensor/torch_tensor.py
| Web Proxy Viewer | New URL | Original Page |