0

While passing a folder of images to a ML-script, I'll receive an error, maybe caused by a broken image:

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001C3BB0AAEA0>

My code:

for files in Files:
        if files.endswith(".jpg"):
            image_name = files
            image_check = image.load_img(image_name, target_size=(1200,1600))  
            images = image.img_to_array(image_check)
            images = np.expand_dims(images, axis=0)
            prediction = np.argmax(model.predict(images), axis=-1)

The script stops running, whenever this error occurs. Is it possible to just skip the according file and move over?

Thanks!

Flo
  • 11
  • 1

1 Answers1

0

I would be doing it the following way:

for files in Files:
    if files.endswith(".jpg"):
        try:
            image_name = files
            image_check = image.load_img(image_name, target_size=(1200,1600))  
            images = image.img_to_array(image_check)
            images = np.expand_dims(images, axis=0)
            prediction = np.argmax(model.predict(images), axis=-1)
        except:
            # NHI means 'Need Human Intervention'
            prediction = 'NHI'

The predictions with NHI value will be considered for RCA (root cause analysis).

Jeril
  • 7,858
  • 3
  • 52
  • 69
  • bare except is bad practice https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except –  Jan 24 '22 at 10:00
  • Using this code, my script does not work properly anymore. "prediction" can have the values 0 or 1 due to the employed model. With "try" and "except" it is not getting any values anymore... – Flo Jan 24 '22 at 10:26