diff --git a/indicoio/images/features.py b/indicoio/images/features.py index ea9608b..8582c82 100644 --- a/indicoio/images/features.py +++ b/indicoio/images/features.py @@ -1,9 +1,7 @@ -import json - import requests import numpy as np -from indicoio.utils import image_preprocess, api_handler +from indicoio.utils import image_preprocess, api_handler, is_url import indicoio.config as config def facial_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs): @@ -60,5 +58,6 @@ def image_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs): :type image: numpy.ndarray :rtype: List containing features """ - image = image_preprocess(image, batch=batch) + if not is_url(image, batch=batch): + image = image_preprocess(image, batch=batch) return api_handler(image, cloud=cloud, api="imagefeatures", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/images/fer.py b/indicoio/images/fer.py index 3365dec..4c6af93 100644 --- a/indicoio/images/fer.py +++ b/indicoio/images/fer.py @@ -1,5 +1,3 @@ -import json - import requests import numpy as np diff --git a/indicoio/utils/__init__.py b/indicoio/utils/__init__.py index d9ad14b..5944839 100644 --- a/indicoio/utils/__init__.py +++ b/indicoio/utils/__init__.py @@ -14,7 +14,7 @@ def api_handler(arg, cloud, api, batch=False, auth=None, **kwargs): if cloud: host = "%s.indico.domains" % cloud - else: + else: # default to indico public cloud host = config.PUBLIC_API_HOST @@ -142,3 +142,13 @@ def image_preprocess(image, batch=False): image = resize(image,(64,64)) image = image.tolist() return image + + +def is_url(data, batch=False): + if batch and isinstance(data[0], basestring): + return True + if not batch and isinstance(data, basestring): + return True + return False + + diff --git a/indicoio/utils/tests/test_utils.py b/indicoio/utils/tests/test_utils.py new file mode 100644 index 0000000..deb9274 --- /dev/null +++ b/indicoio/utils/tests/test_utils.py @@ -0,0 +1,15 @@ +from indicoio.utils import is_url + +def test_is_urls(): + boring_image = [0]*(32**2) + boring_images = [boring_image]*100 + + assert not is_url(boring_image, batch=False) + assert not is_url(boring_images, batch=True) + + url = 'http://picturepicture.com/picture' + urls = [url]*100 + + assert is_url(url, batch=False) + assert is_url(urls, batch=True) + diff --git a/tests/test_remote.py b/tests/test_remote.py index 0c9a218..27abf0c 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -50,6 +50,19 @@ class BatchAPIRun(unittest.TestCase): self.assertTrue(isinstance(response[0], list)) self.assertEqual(len(response[0]), 48) + # TODO: uncomment this test once the remote server is updated to + # deal with image_urls + # def test_batch_image_urls(self): + # test_data = ['http://textfac.es/static/ico/favicon.png', + # 'http://textfac.es/static/ico/favicon.png'] + # response = batch_facial_features(test_data, auth=self.auth) + # self.assertTrue(isinstance(response, list)) + # self.assertTrue(isinstance(response[0], list)) + # self.assertEqual(len(response[0]), 48) + + # TODO: add tests to test when one url is incorrect once we + # have decided how we are dealing with them + def test_batch_image_features_greyscale(self): test_data = [np.random.rand(64, 64).tolist()] response = batch_image_features(test_data, auth=self.auth) @@ -165,6 +178,16 @@ class FullAPIRun(unittest.TestCase): self.assertEqual(len(response), 48) self.check_range(response) + # TODO: uncomment this test once the remote server is updated to + # deal with image_urls + # def test_image_url(self): + # test_face = 'http://textfac.es/static/ico/favicon.png' + # response = facial_features(test_face) + + # self.assertTrue(isinstance(response, list)) + # self.assertEqual(len(response), 48) + # self.check_range(response) + def test_good_image_features_greyscale(self): test_image = np.random.rand(64, 64).tolist() response = image_features(test_image)