FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docarray/docarray/documents/image.py at fix-num-docs · docarray/docarray · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
docarray
/
docarray
Public
Notifications
You must be signed in to change notification settings
Fork
243
Star
3.1k
Code
Issues
68
Pull requests
43
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
docarray
/
docarray
/
documents
/
image.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (89 loc) · 3.28 KB
Breadcrumbs
docarray
/
docarray
/
documents
/
image.py
Copy path
File metadata and controls
116 lines (89 loc) · 3.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
from
typing
import
TYPE_CHECKING
,
Any
,
Optional
,
Type
,
TypeVar
,
Union
import
numpy
as
np
from
docarray
.
base_doc
import
BaseDoc
from
docarray
.
typing
import
AnyEmbedding
,
ImageBytes
,
ImageUrl
from
docarray
.
typing
.
tensor
.
abstract_tensor
import
AbstractTensor
from
docarray
.
typing
.
tensor
.
image
.
image_tensor
import
ImageTensor
from
docarray
.
utils
.
_internal
.
misc
import
import_library
if
TYPE_CHECKING
:
import
tensorflow
as
tf
# type: ignore
import
torch
else
:
tf
=
import_library
(
'tensorflow'
,
raise_error
=
False
)
torch
=
import_library
(
'torch'
,
raise_error
=
False
)
T
=
TypeVar
(
'T'
,
bound
=
'ImageDoc'
)
class
ImageDoc
(
BaseDoc
):
"""
Document for handling images.
It can contain:
- an [`ImageUrl`][docarray.typing.url.ImageUrl] (`Image.url`)
- an [`ImageTensor`](../../../api_references/typing/tensor/image) (`Image.tensor`)
- an [`AnyEmbedding`](../../../api_references/typing/tensor/embedding) (`Image.embedding`)
- an [`ImageBytes`][docarray.typing.bytes.ImageBytes] object (`ImageDoc.bytes_`)
You can use this Document directly:
```python
from docarray.documents import ImageDoc
# use it directly
image = ImageDoc(
url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true'
)
image.tensor = image.url.load()
# model = MyEmbeddingModel()
# image.embedding = model(image.tensor)
```
You can extend this Document:
```python
from docarray.documents import ImageDoc
from docarray.typing import AnyEmbedding
from typing import Optional
# extend it
class MyImage(ImageDoc):
second_embedding: Optional[AnyEmbedding]
image = MyImage(
url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true'
)
image.tensor = image.url.load()
# model = MyEmbeddingModel()
# image.embedding = model(image.tensor)
# image.second_embedding = model(image.tensor)
```
You can use this Document for composition:
```python
from docarray import BaseDoc
from docarray.documents import ImageDoc, TextDoc
# compose it
class MultiModalDoc(BaseDoc):
image: ImageDoc
text: TextDoc
mmdoc = MultiModalDoc(
image=ImageDoc(
url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true'
),
text=TextDoc(text='hello world, how are you doing?'),
)
mmdoc.image.tensor = mmdoc.image.url.load()
# or
mmdoc.image.bytes_ = mmdoc.image.url.load_bytes()
mmdoc.image.tensor = mmdoc.image.bytes_.load()
```
"""
url
:
Optional
[
ImageUrl
]
tensor
:
Optional
[
ImageTensor
]
embedding
:
Optional
[
AnyEmbedding
]
bytes_
:
Optional
[
ImageBytes
]
@
classmethod
def
validate
(
cls
:
Type
[
T
],
value
:
Union
[
str
,
AbstractTensor
,
Any
],
)
->
T
:
if
isinstance
(
value
,
str
):
value
=
cls
(
url
=
value
)
elif
(
isinstance
(
value
, (
AbstractTensor
,
np
.
ndarray
))
or
(
torch
is
not
None
and
isinstance
(
value
,
torch
.
Tensor
))
or
(
tf
is
not
None
and
isinstance
(
value
,
tf
.
Tensor
))
):
value
=
cls
(
tensor
=
value
)
elif
isinstance
(
value
,
bytes
):
value
=
cls
(
byte
=
value
)
return
super
().
validate
(
value
)
Back
|
FazBrowse Home
|
New Git URL