How to open an image

I had written in fastai v2 (still in dev) this bit of code.

def input_fn(request_body, content_type=JPEG_CONTENT_TYPE):
    logger.info('Deserializing the input data.')
    # process an image uploaded to the endpoint
    if content_type == JPEG_CONTENT_TYPE:
        img = open_image(BytesIO(request_body))
        return img

The idea was to run then learn.predict(img).

I am getting now an error as open_image cannot be found. How can I solve this issue? What happened to open_image?

Thanks a lot!!

open_image is still there. Did you make sure to upgrade your fastai version? (essentially what happened is the pip version of fastai2 became the 2.0.0 version of fastai

1 Like

Oh! Where is it to import it @muellerzr ? I am doing from fastai.vision.all import * and getting a not defined error. My fastai is 2.0.0 so this should be fine.

1 Like

Ah sorry, I blanked. Now we have PILImage.create instead to open an image up.

2 Likes

I am running this:

# Deserialize the Invoke request body into an object we can perform prediction on
def input_fn(request_body, content_type=JPEG_CONTENT_TYPE):
    logger.info('Deserializing the input data.')
    # process an image uploaded to the endpoint
    if content_type == JPEG_CONTENT_TYPE:
        return PILImage.create(io.BytesIO(request_body))
    # process a URL submitted to the endpoint
    if content_type == JSON_CONTENT_TYPE:
        img_request = requests.get(request_body['url'], stream=True)
        return PILImage.create(io.BytesIO(img_request.content))
    raise Exception('Requested unsupported ContentType in content_type: {}'.format(content_type))

but for some reason I get the error cannot identify image file <_io.BytesIO object at 0x7fe521605e08>
I tried with Pillow==6.2.0 as well as the last version of the library (7.2.1) but did not help either. I find it very weird as this runs in my sagemaker conda_pytorch_p36 kernel but not when I deploy the pytorch fastai estimator

import io
import requests
from PIL import Image
r = requests.get(urls[0], stream=True)
im = PILImage.create(io.BytesIO(r.content))

I don’t really know what else to try so any ideas are very welcome, getting a bit desperate in here.

Don’t run the io.Bytes bit. PILImage can accept that. Just do r.content (if you check the source you’ll notice it’ll actually check for that)

True! but without io.BytesIO I was getting this error so basically this is why I added it but it did not help get around the issue though