From e7b26c61101415ae37899423b3e735bf4feb5c03 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 02:59:51 -0400 Subject: [PATCH 1/3] Classify local images in a folder --- tensorpy/image_base.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tensorpy/image_base.py b/tensorpy/image_base.py index 2b6905b..e65e1e2 100644 --- a/tensorpy/image_base.py +++ b/tensorpy/image_base.py @@ -1,7 +1,11 @@ import os import requests +import shutil +import sys import uuid from BeautifulSoup import BeautifulSoup +from os import listdir +from os.path import isfile, join from PIL import Image from StringIO import StringIO from tensorpy import classify_image @@ -95,6 +99,41 @@ def get_image_classification(image_url): return best_guess.strip() +def classify_local_image(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 + hex_name = 'temp_image_%s' % uuid.uuid4().get_hex() + hex_name_png = hex_name + '.png' + hex_name_jpg = hex_name + '.jpg' + shutil.copy2(file_path, os.path.join(downloads_folder, 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 + + +def classify_folder_images(folder_path): + classified_images = [] + files = [f for f in listdir(folder_path) if isfile(join(folder_path, f))] + images = [f for f in files if (f.endswith('.jpg') or f.endswith('.png'))] + total = len(images) + counter = 0 + for image in images: + counter += 1 + sys.stdout.write("\rClassifying Image %d of %s..." % (counter, total)) + sys.stdout.flush() + classified_images.append( + classify_local_image(os.path.join(folder_path, image))) + sys.stdout.write("\rAll classifications have been completed!\n") + return classified_images + + def classify(image_url): """ A shorter method name for get_image_classification() """ return get_image_classification(image_url) From 8057618aeb1b456f56d0ba99f59cb9df0449d95c Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 03:00:11 -0400 Subject: [PATCH 2/3] Add more Python examples --- examples/test_python_file_classify.py | 4 ++++ examples/test_python_folder_classify.py | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 examples/test_python_file_classify.py create mode 100644 examples/test_python_folder_classify.py diff --git a/examples/test_python_file_classify.py b/examples/test_python_file_classify.py new file mode 100644 index 0000000..8523edf --- /dev/null +++ b/examples/test_python_file_classify.py @@ -0,0 +1,4 @@ +from tensorpy import image_base + +result = image_base.classify_local_image("images/cat_animal.jpg") +print("\nBest match classification:\n%s\n" % result) diff --git a/examples/test_python_folder_classify.py b/examples/test_python_folder_classify.py new file mode 100644 index 0000000..a5e1d66 --- /dev/null +++ b/examples/test_python_folder_classify.py @@ -0,0 +1,6 @@ +from tensorpy import image_base + +classifications = image_base.classify_folder_images('./images') +print("*** Displaying Image Classification Results: ***") +for classification in classifications: + print classification From 367d4db668a1ccadcafe928d7988157e9441711a Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 18 May 2017 03:02:00 -0400 Subject: [PATCH 3/3] Version 1.0.15 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9b5ad5b..977b511 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='tensorpy', - version='1.0.14', + version='1.0.15', url='http://tensorpy.com', author='Michael Mintz', author_email='@mintzworld',