Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keras.preprocessing.image.load_img should support byte objects #11684

Closed
3 tasks done
wmelton opened this issue Nov 19, 2018 · 5 comments
Closed
3 tasks done

keras.preprocessing.image.load_img should support byte objects #11684

wmelton opened this issue Nov 19, 2018 · 5 comments

Comments

@wmelton
Copy link

wmelton commented Nov 19, 2018

Please make sure that the boxes below are checked before you submit your issue.
If your issue is an implementation question, please ask your question on StackOverflow or on the Keras Slack channel instead of opening a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

  • Check that your version of TensorFlow is up-to-date. The installation instructions can be found here.

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

Hello,

When building a predictor that uses a local file, the following function call works perfectly:
keras.preprocessing.image.load_img(img_file, target_size=target_size)

However, the keras.preprocessing.image class does not appear to have a similar mechanism for utilizing image bytes objects that have already been loaded into memory for real-time prediction.

It appears to be hard-coded to call pil_image.open(path), which doesn't work if image object is already read in to memory.

For example, if you loaded an image in to memory from a URL and then tried to pass it to the preprocessing class, you will get errors.

image = urllib.request.urlopen(url)
content = image.read()
keras.preprocessing.image.load_img(content, target_size=target_size)

Currently the above will result in an error such as:

AttributeError: 'int' object has no attribute 'read'

Here is the full Trace:
multiprocessing.pool.RemoteTraceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 2613, in open
    fp.seek(0)
AttributeError: 'int' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/data_utils.py", line 401, in get_index
    return _SHARED_SEQUENCES[uid][i]
  File "/src/handlers/dataGenerator.py", line 25, in __getitem__
    X, y = self.__data_generator(batch_samples)
  File "/src/handlers/dataGenerator.py", line 39, in __data_generator
    img = utils.load_image(sample, self.img_load_dims)
  File "/src/utils/utils.py", line 39, in load_image
    return np.asarray(keras.preprocessing.image.load_img(img_file, target_size=target_size))
  File "/usr/local/lib/python3.5/dist-packages/keras_preprocessing/image.py", line 498, in load_img
    img = pil_image.open(path)
  File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 2615, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'int' object has no attribute 'read'

Again - this is presumably because load_img is trying to read the file again from disk rather than understanding it has already been read and is in byte form.

@gabrieldemarmiesse
Copy link
Contributor

The thing is that as far as I remember, load_img is not part of the public API. It's done for keras internal needs. If you want to read complicated sources, I recommend the very good imageio.imread which can read images from pretty much everything, including URLs.

@wmelton
Copy link
Author

wmelton commented Nov 20, 2018

@gabrieldemarmiesse -

I'm definitely aware of how to load an image in to memory from anywhere, the issue was opened primarily because some inference needs would need to also use image.preprocessing class, but the class seems extremely geared towards local filesystem resources instead of remote ones.

You can leave this closed - I've already refactored a workaround.

Thanks!

@ghost
Copy link

ghost commented Sep 19, 2019

@wmelton Can you please share what the workaround is?
I am unable to load the remote image files in Keras. Your response is highly appreciated.

@vulcan25
Copy link

vulcan25 commented Feb 1, 2020

+1 @wmelton plese share the workaround.

@RifeWang
Copy link

RifeWang commented Mar 19, 2020

load image from path:

img = image.load_img(img_path, target_size=target_size)
img = image.img_to_array(img)

load image from bytes:
see tf doc

from PIL import Image
import io

img = Image.open(io.BytesIO(img_bytes))
img = img.convert('RGB')
img = img.resize(target_size, Image.NEAREST)
img = image.img_to_array(img)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants