|
| 1 | +from typing import Optional, TypeVar |
| 2 | + |
| 3 | +from docarray.base_document import BaseDocument |
| 4 | +from docarray.documents import Audio |
| 5 | +from docarray.typing import AnyEmbedding, AnyTensor |
| 6 | +from docarray.typing.tensor.video.video_tensor import VideoTensor |
| 7 | +from docarray.typing.url.video_url import VideoUrl |
| 8 | + |
| 9 | +T = TypeVar('T', bound='Video') |
| 10 | + |
| 11 | + |
| 12 | +class Video(BaseDocument): |
| 13 | + """ |
| 14 | + Document for handling video. |
| 15 | + The Video Document can contain a VideoUrl (`Video.url`), an Audio Document |
| 16 | + (`Video.audio`), a VideoTensor (`Video.video_tensor`), an AnyTensor representing |
| 17 | + the indices of the video's key frames (`Video.key_frame_indices`) and an |
| 18 | + AnyEmbedding (`Video.embedding`). |
| 19 | +
|
| 20 | + EXAMPLE USAGE: |
| 21 | +
|
| 22 | + You can use this Document directly: |
| 23 | +
|
| 24 | + .. code-block:: python |
| 25 | +
|
| 26 | + from docarray.documents import Video |
| 27 | +
|
| 28 | + # use it directly |
| 29 | + vid = Video( |
| 30 | + url='https://github.com/docarray/docarray/tree/feat-add-video-v2/tests/toydata/mov_bbb.mp4?raw=true' |
| 31 | + ) |
| 32 | + vid.audio.tensor, vid.video_tensor, vid.key_frame_indices = vid.url.load() |
| 33 | + model = MyEmbeddingModel() |
| 34 | + vid.embedding = model(vid.video_tensor) |
| 35 | +
|
| 36 | + You can extend this Document: |
| 37 | +
|
| 38 | + .. code-block:: python |
| 39 | +
|
| 40 | + from typing import Optional |
| 41 | +
|
| 42 | + from docarray.documents import Text, Video |
| 43 | +
|
| 44 | +
|
| 45 | + # extend it |
| 46 | + class MyVideo(Video): |
| 47 | + name: Optional[Text] |
| 48 | +
|
| 49 | +
|
| 50 | + video = MyVideo( |
| 51 | + url='https://github.com/docarray/docarray/blob/feat-rewrite-v2/tests/toydata/mov_bbb.mp4?raw=true' |
| 52 | + ) |
| 53 | + video.video_tensor = video.url.load_key_frames() |
| 54 | + model = MyEmbeddingModel() |
| 55 | + video.embedding = model(video.video_tensor) |
| 56 | + video.name = Text(text='my first video') |
| 57 | +
|
| 58 | + You can use this Document for composition: |
| 59 | +
|
| 60 | + .. code-block:: python |
| 61 | +
|
| 62 | + from docarray import BaseDocument |
| 63 | + from docarray.documents import Text, Video |
| 64 | +
|
| 65 | +
|
| 66 | + # compose it |
| 67 | + class MultiModalDoc(BaseDocument): |
| 68 | + video: Video |
| 69 | + text: Text |
| 70 | +
|
| 71 | +
|
| 72 | + mmdoc = MultiModalDoc( |
| 73 | + video=Video( |
| 74 | + url='https://github.com/docarray/docarray/blob/feat-rewrite-v2/tests/toydata/mov_bbb.mp4?raw=true' |
| 75 | + ), |
| 76 | + text=Text(text='hello world, how are you doing?'), |
| 77 | + ) |
| 78 | + mmdoc.video.video_tensor = mmdoc.video.url.load_key_frames() |
| 79 | + """ |
| 80 | + |
| 81 | + url: Optional[VideoUrl] |
| 82 | + audio: Optional[Audio] = Audio() |
| 83 | + video_tensor: Optional[VideoTensor] |
| 84 | + key_frame_indices: Optional[AnyTensor] |
| 85 | + embedding: Optional[AnyEmbedding] |
0 commit comments