/
image.py
279 lines (237 loc) · 8.87 KB
/
image.py
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import io
import os
import base64
import json
import numpy as np
from easycv.collection import Collection, auto_compute
from easycv.errors.io import InvalidImageInputSource
from easycv.io import save, valid_image_source, get_image_array, show, random_dog_image
from easycv.output import Output
from easycv.transforms.base import Transform
import cv2
class Image(Collection):
"""
This class represents an image.
Images can be created from a NumPy array containing the **image** data, a path to a local file
or a link to an **image** on the web. :doc:`Transforms <transforms/index>` and \
:doc:`Pipelines <pipeline>` can easily be applied to any **image**.
If the image is lazy, computations will be delayed until needed or until the image is \
computed. This can facilitate large scale processing and distributed computation.
:param source: Image data source. An array representing the image or a path/link to a file \
containing the image
:type source: :class:`str`/:class:`~numpy:numpy.ndarray`
:param pipeline: Pipeline to be applied to the image at creation time, defaults to None
:type pipeline: :class:`~easycv.pipeline.Pipeline`, optional
:param lazy: `True` if the image is lazy (computations are delayed until needed), defaults to \
False
:type lazy: :class:`boolean`, optional
"""
def __init__(self, source, pipeline=None, lazy=False):
if not valid_image_source(source):
raise InvalidImageInputSource()
super().__init__(pending=pipeline, lazy=lazy)
if self._lazy:
self._source = source
self._img = None
else:
self._img = self._pending(get_image_array(source))["image"]
self._pending.clear()
@classmethod
def random(cls, lazy=False):
"""
Get a random image. Currently all images are from `DogApi <https://dog.ceo/dog-api/>`_.
:return: Random Image
:rtype: :class:`Image`
"""
path = random_dog_image()
return cls(path, lazy=lazy)
@property
def loaded(self):
"""
Check if **image** is loaded or if it still needs to be downloaded/decoded.
:return: `True` if loaded, `False` otherwise
:rtype: :class:`bool`
"""
return self._img is not None
@property
@auto_compute
def height(self):
"""
Returns image height.
:return: Image height
:rtype: :class:`int`
"""
return self._img.shape[0]
@property
@auto_compute
def width(self):
"""
Returns **image** width.
:return: Image width
:rtype: :class:`int`
"""
return self._img.shape[1]
@property
@auto_compute
def channels(self):
"""
Returns **image** number of channels.
:return: Image numeber of channels
:rtype: :class:`int`
"""
if len(self._img.shape) == 2:
return 1
else:
return self._img.shape[2]
@property
@auto_compute
def array(self):
"""
Returns a NumPy array that represents the **image**.
:return: Image as NumPy array
:rtype: :class:`~numpy:numpy.ndarray`
"""
return self._img
def load(self):
"""
Loads the **image** if it isn't already loaded
"""
if not self.loaded:
self._img = get_image_array(self._source)
def apply(self, transform, in_place=False):
"""
Returns a new **image** with the :doc:`transform <transforms/index>` or \
:doc:`pipeline <pipeline>` applied.
If the image is lazy the transform/pipeline will be stored as a pending operation
(no computation is done).
If `in_place` is *True* the operation will change the **current image** instead of \
returning a new Image.
:param transform: Transform/Pipeline to be applied
:type transform: :class:`~easycv.transforms.base.Transform`/\
:class:`~easycv.pipeline.Pipeline`
:param in_place: `True` to change the current **image**, `False` to return a new one with \
the transform applied, defaults to `False`
:type in_place: :class:`bool`, optional
:return: The new **image** if `in_place` is *False*
:rtype: :class:`~eascv.image.Image`
"""
if isinstance(transform, Transform):
transform.initialize()
outputs = transform.outputs
if self._lazy:
if outputs == {}: # If transform outputs an image
if in_place:
self._pending.add_transform(transform)
else:
new_source = self._img if self.loaded else self._source
new_image = Image(new_source, pipeline=self._pending, lazy=True)
new_image.apply(transform, in_place=True)
return new_image
else:
self.load()
return Output(self._img, pending=transform)
else:
self.load()
if outputs == {}: # If transform outputs an image
if in_place:
self._img = transform(self._img)["image"]
else:
new_image = transform(self._img.copy())["image"]
return Image(new_image)
else:
return transform(self._img)
def compute(self, in_place=True):
"""
Returns a new **image** with all the pending operations applied.
If `in_place` is *True* the pending operations will be applied
to the current **image** instead.
:param in_place: `True` to change the current **image**, `False` to return a new one with \
the pending transforms applied, defaults to `True`
:type in_place: :class:`bool`, optional
:return: The new **image** if `in_place` is *False*
:rtype: :class:`~eascv.image.Image`
"""
self.load()
if in_place:
self._img = self._pending(self._img)["image"]
self._pending.clear()
return self
else:
result = Image(self._pending(self._img)["image"], lazy=self._lazy)
return result
@auto_compute
def encode(self):
"""
Returns a encoded version of the **image**
:return: Encoded image
:rtype: :class:`str`
"""
image_data = base64.b64encode(self.array.copy(order="C")).decode("utf-8")
encoded = {
"width": self.width,
"height": self.height,
"channels": self.channels,
"dtype": str(self.array.dtype),
"data": image_data,
}
return json.dumps(encoded)
@classmethod
def decode(cls, encoded):
"""
Creates an image by decoding a previously encoded encoded image.
:return: Decoded image
:rtype: :class:`~eascv.image.Image`
"""
encoded = json.loads(encoded)
shape = (encoded["height"], encoded["width"], encoded["channels"])
image_data = bytes(encoded["data"], encoding="utf-8")
image_array = np.frombuffer(
base64.decodebytes(image_data), dtype=encoded["dtype"]
)
return cls(image_array.reshape(shape))
@auto_compute
def show(self, name="Image"):
"""
Opens a popup window with the **image** displayed on it. This window is resizable and\
supports zoom/pan. If its impossible to open a popup window this method will return the
image instead.
:param name: Window name, defaults to "Image"
:type name: :class:`str`, optional
"""
if "DISPLAY" in os.environ:
show(self._img, name=name)
else:
return self
@auto_compute
def save(self, filename):
"""
Saves an **image** to a file under a given filename.
:param filename: Filename to save
:type filename: :class:`str`, optional
"""
save(self.array, filename)
@auto_compute
def __eq__(self, other):
return (
isinstance(other, Image)
and np.array_equal(other.array, self.array)
and self.pending == other.pending
)
@auto_compute
def __repr__(self):
return "<image size={}x{} at 0x{}>".format(
self.height, self.width, id(self._img)
)
def _repr_png_(self):
b = io.BytesIO()
save(self._img, b, "PNG")
return b.getvalue()
def hash(self, hash_size=8):
"""
Function to calculate the hash function of an Image using dhash
:param hash_size: Square root of the number of bits of the hash, defaults to 8
:type hash_size: :class:`int`, optional
"""
resized = cv2.resize(self.array, (hash_size + 1, hash_size))
diff = resized[:, 1:] > resized[:, :-1]
return sum([2 ** i for (i, v) in enumerate(diff.flatten()) if v])