From 651f3e688afa17c49a0f42bdec441654c9161074 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 21:18:02 -0400 Subject: [PATCH 1/4] Make the "classify" method handle more input types --- tensorpy/image_base.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tensorpy/image_base.py b/tensorpy/image_base.py index 1c39e16..8dbd581 100644 --- a/tensorpy/image_base.py +++ b/tensorpy/image_base.py @@ -5,7 +5,7 @@ import uuid from BeautifulSoup import BeautifulSoup from os import listdir -from os.path import isfile, join +from os.path import isdir, isfile, join from PIL import Image from StringIO import StringIO from tensorpy import classify_image @@ -79,27 +79,31 @@ def get_all_images_on_page(page_url): return image_url_list -def get_image_classification(image_url): +def classify_image_url(image_url): + """ Classify an image from a URL. """ downloads_folder = settings.DOWNLOADS_FOLDER hex_name = 'temp_image_%s' % uuid.uuid4().get_hex() hex_name_png = hex_name + '.png' hex_name_jpg = hex_name + '.jpg' - web_core.save_file_as(image_url, hex_name_png) convert_image_file_to_jpg( "%s/%s" % (downloads_folder, hex_name_png)) os.rename(downloads_folder + "/" + hex_name_png, downloads_folder + "/temp_image_png.png") - best_guess = classify_image.external_run( "%s/%s" % (downloads_folder, hex_name_jpg)) os.rename(downloads_folder + "/" + hex_name_jpg, downloads_folder + "/temp_image_jpg.jpg") - return best_guess.strip() +def get_image_classification(image_url): + # Keep original method name for backwards-compatibility + return classify_image_url(image_url) + + def classify_local_image(file_path): + """ Classify an image from a local file path. """ if not file_path.endswith('.jpg') and not file_path.endswith('.png'): raise Exception("Expecting a .jpg or .png file!") downloads_folder = settings.DOWNLOADS_FOLDER @@ -119,6 +123,7 @@ def classify_local_image(file_path): def classify_folder_images(folder_path, return_dict=False): + """ Classify all images from a local folder. """ classified_images_list = [] classified_images_dict = {} files = [f for f in listdir(folder_path) if isfile(join(folder_path, f))] @@ -138,6 +143,16 @@ def classify_folder_images(folder_path, return_dict=False): return classified_images_list -def classify(image_url): - """ A shorter method name for get_image_classification() """ - return get_image_classification(image_url) +def classify(image_url_or_path): + """ Classify an image from a URL or local file path. + If a local folder is provided, all images in the folder + will be classified. """ + is_valid_url = web_core.is_valid_url(image_url_or_path) + if is_valid_url: + return classify_image_url(image_url_or_path) + elif isfile(image_url_or_path): + return classify_local_image(image_url_or_path) + elif isdir(image_url_or_path): + return classify_folder_images(image_url_or_path, return_dict=True) + else: + raise Exception("Expecting an image URL, file path, or folder path!") From d8e9e4dc4cd284efab28380e954fe7b8f96d4241 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 21:19:27 -0400 Subject: [PATCH 2/4] Make the "classify" command handle more input types --- tensorpy/classify.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tensorpy/classify.py b/tensorpy/classify.py index 8da263f..5cfa60c 100644 --- a/tensorpy/classify.py +++ b/tensorpy/classify.py @@ -3,6 +3,7 @@ import threading import uuid from multiprocessing.dummy import Pool as ThreadPool +from os.path import isdir, isfile from tensorpy import classify_image from tensorpy import settings from tensorpy import image_base @@ -70,13 +71,24 @@ def main(): url = sys.argv[1] valid_url = web_core.is_valid_url(url) if not valid_url: - raise Exception("Error: %s is not a valid URL!" % url) + file_path = url + if isfile(file_path): + best_guess = image_base.classify_local_image(file_path) + elif isdir(file_path): + best_guess = image_base.classify_folder_images( + file_path, return_dict=True) + else: + raise Exception("Error: %s is not a valid image path!" % url) + print("\n*** Best match classification: ***") + print(best_guess) + print("") + return content_type = web_core.get_content_type(url) if content_type == 'other': raise Exception( "Error: %s does not evaluate to %s" % (url, expected_arg)) elif content_type == 'image': - best_guess = image_base.get_image_classification(url) + best_guess = image_base.classify_image_url(url) print("\n*** Best match classification: ***") print(best_guess) print("") From 3f7819d7dcb9e4bfe703da52c564b1724c474fc3 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 21:20:44 -0400 Subject: [PATCH 3/4] Update the ReadMe --- README.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2eaa7de..8016465 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,31 @@ classify "https://github.com/TensorPy/TensorPy/tree/master/examples/images" Classify a single image URL from a Python script: (See **[test_python_classify.py](https://github.com/TensorPy/TensorPy/blob/master/examples/test_python_classify.py)** for details.) ```bash -cd examples -python test_python_classify.py +python examples/test_python_classify.py +``` + +Classify an image from a local file path: + +```bash +classify examples/images/cat_animal.jpg +``` + +Classify all images from a local folder: + +```bash +classify examples/images/ +``` + +Classify an image from a local file path using a Python script: (See **[test_python_file_classify.py](https://github.com/TensorPy/TensorPy/blob/master/examples/test_python_file_classify.py)** for details.) + +```bash +python examples/test_python_file_classify.py +``` + +Classify all images in a local folder using a Python script: (See **[test_python_folder_classify.py](https://github.com/TensorPy/TensorPy/blob/master/examples/test_python_folder_classify.py)** for details.) + +```bash +python examples/test_python_folder_classify.py ``` ____________ From 30657793f136b5c93f30f427e0a17dccfab08c03 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 21:21:02 -0400 Subject: [PATCH 4/4] Version 1.0.17 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0bcbb42..4dc8aa7 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='tensorpy', - version='1.0.16', + version='1.0.17', url='http://tensorpy.com', author='Michael Mintz', author_email='@mintzworld',