ADD: Tests for Numpy input for image and added type conversion

This commit is contained in:
Chris Lee
2015-06-15 16:27:02 -04:00
committed by Madison May
parent 17500bcfa5
commit 14056cfa02
2 changed files with 84 additions and 9 deletions
+15 -9
View File
@@ -22,7 +22,7 @@ def image_preprocess(image, size=(48,48), batch=False):
b64_str = re.sub('^data:image/.+;base64,', '', image)
if os.path.isfile(image):
# check type of element
outImage = Image.open(image)
out_image = Image.open(image)
elif B64_PATTERN.match(b64_str) is not None:
return b64_str
else:
@@ -33,20 +33,26 @@ def image_preprocess(image, size=(48,48), batch=False):
"Input as lists of pixels will be deprecated in the next major update",
DeprecationWarning
)
outImage = process_list_image(image)
out_image = process_list_image(image)
elif isinstance(image, Image.Image):
outImage = image
out_image = image
elif type(image).__name__ == "ndarray": # image is from numpy/scipy
out_image = Image.fromarray(image)
if "float" in str(image.dtype) and image.min() > 0 and image.max() < 1:
image *= 255
try:
out_image = Image.fromarray(image.astype("uint8"))
except TypeError as e:
raise IndicoError("Please ensure the numpy array is acceptable by PIL. Values must be between 0 and 1 or between 0 and 255 in greyscale, rgb, or rgba format.")
else:
raise IndicoError("Image must be a filepath, base64 encoded string, or a numpy array")
# image resizing
outImage = outImage.resize(size)
out_image = out_image.resize(size)
# convert to base64
temp_output = StringIO.StringIO()
outImage.save(temp_output, format='PNG')
out_image.save(temp_output, format='PNG')
temp_output.seek(0)
output_s = temp_output.read()
@@ -87,7 +93,7 @@ def process_list_image(_list):
seq_obj = []
outImage = Image.new("RGB", (dimens[0], dimens[1]))
out_image = Image.new("RGB", (dimens[0], dimens[1]))
for i in xrange(dimens[0]):
for j in xrange(dimens[1]):
elem = _list[i][j]
@@ -105,6 +111,6 @@ def process_list_image(_list):
seq_obj.append((elem, ) * 3)
#Needs to be 0 - 255 in flattened list of (R, G, B)
outImage.putdata(data = seq_obj)
out_image.putdata(data = seq_obj)
return outImage
return out_image
+69
View File
@@ -421,6 +421,75 @@ class FullAPIRun(unittest.TestCase):
config.api_key = temp_api_key
class NumpyImagesRun(FullAPIRun):
"""
Testing numpy array as images
"""
def setUp(self):
self.api_key = config.api_key
try:
import numpy as np
globals()["np"] = np
except ImportError:
self.skipTest("Numpy is not installed!")
def test_float_numpy_arrays(self):
test_image = np.random.random(size=(48,48))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_float_RGB_numpy_arrays(self):
test_image = np.random.random(size=(48,48,3))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_float_RGBA_numpy_arrays(self):
test_image = np.random.random(size=(48,48,4))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_int_numpy_arrays(self):
test_image = np.random.randint(0, 255, size=(48,48))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_int_RGB_numpy_arrays(self):
test_image = np.random.randint(0, 255, size=(48,48, 3))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_int_RGBA_numpy_arrays(self):
test_image = np.random.randint(0, 255, size=(48,48, 3))
response = image_features(test_image)
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 2048)
self.check_range(response)
def test_invalid_int_numpy_arrays(self):
test_image = np.random.randint(255, 300, size=(48,48, 3))
self.assertRaises(IndicoError, image_features, test_image)
def test_invalid_int_numpy_arrays(self):
test_image = np.random.randint(255, 300, size=(48,48, 5))
self.assertRaises(IndicoError, image_features, test_image)
def flatten(container):
for i in container:
if isinstance(i, list) or isinstance(i, tuple):