keras-team / keras-preprocessing

Utilities for working with image data, text data, and sequence data.
Other
1.02k stars 444 forks source link

BytesIO compatibility for "image.load_img" broken in 1.1.1 release #293

Open ndamclean opened 4 years ago

ndamclean commented 4 years ago

The most recent release on PyPi (1.1.1) seems to break the compatibility of the tensorflow.keras.preprocessing.image.load_img function with python io.BytesIO arguments as input.

This code snippet was working in the previous release (1.1.0). (requires pip-installable requests package to run)

import io
import requests
from tensorflow.keras.preprocessing import image

# random cat picture URL
# Use a different URL with an image if this stops working
image_url = 'https://tr.rbxcdn.com/893df9c3d669d75943b6089b6eec5278/420/420/Decal/Png'

response = requests.get(image_url)
response.raise_for_status()
f = io.BytesIO(response.content)
img_data = image.load_img(f)

print(img_data)

After updating to the 1.1.1 release, the above snippet crashes with the following error message:

Traceback (most recent call last):
  File "tmp2.py", line 13, in <module>
    img_data = image.load_img(f)
  File "/home/nmclean/.virtualenvs/charm/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 113, in load_img
    with open(path, 'rb') as f:
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
Dref360 commented 4 years ago

Hello! This commit seems to be the issue.

I can fix it tomorrow, but PRs are welcome :)

FadingWinds commented 4 years ago

I'm using 1.1.2 in Google Colab but still encounter this problem... downgrade to 1.1.0 works.

Dref360 commented 4 years ago

Oh right, I cannot merge the PR myself. Would anyone be available to make a PR? The diff is in #294 .

alphaAI-stack commented 3 years ago

this is may mybe a Keras-Preprocessing bug, i update Newest version Keras-Preprocessing 1.1.2,has the same problem

last,i back to pip3 install -U Keras-Preprocessing===1.1.0 fix it

my code is


response = requests.get(image_url, timeout=5, verify=False, headers={'connection': 'close'})
image_io = io.BytesIO(response.content)
img = image.load_img(image_io, target_size=(256, 256))
cryanp-usgs commented 3 years ago

Thanks for figuring this out! I was buffering some images to memory while assessing them with a keras model and code that worked last year was throwing the same error:

keras_preprocessing/image/utils.py", line 113, in load_img with open(path, 'rb') as f: TypeError: expected str, bytes or os.PathLike object, not _io.BytesIOBytesIO

...downgrading to keras-preprocessing===1.1.0 in both Windows and Ubuntu builds fixed it immediately!

TreeKat71 commented 3 years ago

I also encounter this issue ... the commit in #261 break io.ByteIo. Just want to know will fix it? Thanks

TreeKat71 commented 3 years ago

I do a fix in the #339