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!
[x] 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
[x] Check that your version of TensorFlow is up-to-date. The installation instructions can be found here.
[x] 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.
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.
@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!
@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.
+1 @wmelton plese share the workaround.
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)
Most helpful comment
load image from path:
load image from bytes:
see tf doc