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 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', 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)