Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

____________
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 14 additions & 2 deletions tensorpy/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("")
Expand Down
31 changes: 23 additions & 8 deletions tensorpy/image_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))]
Expand All @@ -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!")