Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualdvid committed Sep 8, 2018
2 parents 72235b9 + f61aed0 commit 0e37e77
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/specs/vision_spec.md
Expand Up @@ -337,6 +337,7 @@ When used as a setter, the machine learning ready data is resized to the specifi
**Exceptions**

A `TypeError` is raised if the type of the parameter is not the expected type.
A `AttributeError` is raised if the parameter is not a tuple of length 2.

#### 1.3.12 fail

Expand Down
32 changes: 31 additions & 1 deletion gapml/vision.py
Expand Up @@ -971,7 +971,7 @@ def flatten(self, flatten):
for image in self._data:
image._imgdata = image._imgdata.flatten()
else:
# Already not Flattened
# Not Flattened
if len(self._data[0].shape) != 1:
return
if self._resize != None:
Expand All @@ -980,6 +980,36 @@ def flatten(self, flatten):
resize = self._data[0]._raw.shape
for image in self._data:
image._imgdata = image._imgdata.reshape( resize )

@property
def resize(self):
""" dummy property """
return None

@resize.setter
def resize(self, resize):
""" Resize the Image Data """
if not isinstance(resize, tuple):
raise TypeError("Tuple expected for resize")

if len(resize) != 2:
raise AttributeError("Tuple for resize must be in form (height, width)")

# there are no images
if len(self) == 0:
return

# openCV uses width, height
resize = ( resize[1], resize[0] )

# Data is flatten, so let's unflatten it first
if len(self._data[0].shape) == 1:
self.flatten = False

for image in self._data:
image._imgdata = cv2.resize(image._imgdata, resize, interpolation=cv2.INTER_AREA)
image._shape = image._imgdata.shape


def __next__(self):
""" Iterate through the training set (single image at a time) """
Expand Down
28 changes: 28 additions & 0 deletions tests/image_test.py
Expand Up @@ -1365,6 +1365,34 @@ def test_142(self):
self.assertEquals(images[0].shape, (2,))
os.remove('foobar.h5')

def test_143(self):
""" Images = resize """
images = Images(['files/0_100.jpg', 'files/1_100.jpg'], [1,2], config=['nostore', 'resize=(60,60)'])
images.resize = (20,30)
self.assertEquals(images[0].shape, (20, 30, 3))
self.assertEquals(images[1].shape, (20, 30, 3))

def test_144(self):
""" Images = resize after flatten """
images = Images(['files/0_100.jpg', 'files/1_100.jpg'], [1,2], config=['nostore', 'flatten', 'resize=(60,60)'])
images.resize = (20,30)
self.assertEquals(images[0].shape, (20, 30, 3))
self.assertEquals(images[1].shape, (20, 30, 3))

def test_145(self):
""" Images - invalid arg for resize """
images = Images(['files/0_100.jpg', 'files/1_100.jpg'], [1,2], config=['nostore', 'flatten', 'resize=(60,60)'])
with pytest.raises(TypeError):
images.resize = 'a'
with pytest.raises(AttributeError):
images.resize = (1,2,3)

def test_146(self):
""" Images - resize ignored for zero images """
images = Images()
images.resize = (20,30)


def bug_139(self):
""" Image - async, not a valid image """
f = open("tmp.jpg", "w")
Expand Down

0 comments on commit 0e37e77

Please sign in to comment.