0

This is the complete error I'm getting:

<ipython-input-4-77b9831c6ca8> in main(argv)
    187             im_fn_list = get_images()
    188             for im_fn in im_fn_list:
--> 189                 im = cv2.imread(im_fn)[:, :, ::-1]
    190                 logger.debug('image file:{}'.format(im_fn))
    191 

TypeError: 'NoneType' object is not subscriptable

This is the part of code where it points to, I guess here opencv reads the list:

im_fn_list = get_images()
            for im_fn in im_fn_list:
                im = cv2.imread(im_fn)[:, :, ::-1]
                logger.debug('image file:{}'.format(im_fn))

and this is the function to get images:


def get_images():
    '''
    find image files in test data path
    :return: list of files found
    '''
    files = []
    exts = ['jpg', 'png', 'jpeg', 'JPG']
    for parent, dirnames, filenames in os.walk(FLAGS.test_data_path):
        for filename in filenames:
            for ext in exts:
                if filename.endswith(ext):
                    files.append(os.path.join(parent, filename))
                    break
    logger.info('Find {} images'.format(len(files)))
    return files

No matter which image I load, it gives me TypeError: 'NoneType' object is not subscriptable. Im stuck at this point, I reinstalled opencv and opevncv-contrib, didn't work at all.

Mel
  • 429
  • 1
  • 4
  • 12

1 Answers1

0

Are you sure get_images() is working as intended? Perhaps, you can print im_fn or try reading im_fn with another library to make sure you are accessing the file properly. Your code may also just be getting tripped up on the first file in the list due to something such as permission restrictions. Try putting im = cv2.imread(im_fn)[:, :, ::-1] in a try block to see if the rest of your files have this problem.

How to solve the 'NoneType' object is not subscriptable is opencv

Matthew Thomas
  • 594
  • 7
  • 13
  • I printed im_fn and got no results as it wasn't getting images but instead of making changes in code, I tried to get same images from another folder and it worked. Then I created another folder, it worked for it too. Then I tried to give same path as first one and it doesn't work for it again. I made sure the path is correct but still won't work for some folders. Even sometimes when I put 2 images in that folder, it says "Find 10 images". I don't understand why its working for some folders and for some don't. – Mel Jun 05 '20 at 05:10