cv2-如何更正'NoneType'对象不可下标

时间:2019-12-25 10:14:37

标签: python-3.x numpy

我想以numpy数组加载图像。我想访问Class-1,Class-2和Class-3中的所有图像和蒙版。它适用于2类,但随后引发错误。目录结构为:

train_id->class 1->images
                  ->masks
        ->class 2->images
                 ->masks
        ->class 3->images
                 ->masks

但是它给了我这个错误 追溯:

TypeError                                 Traceback (most recent call last)
<ipython-input-25-c92e70234988> in <module>()
      9       # print(path_image)#+image_file)
     10       # print(path_mask)#+mask_file)
---> 11       img=cv.imread(path_image+image_file)[:,:,:IMG_CHANNELS]
     12       img_ = cv.resize(img, (IMG_HEIGHT, IMG_WIDTH))
     13       img_=cv.cvtColor(img_,cv.COLOR_BGR2RGB)

TypeError: 'NoneType' object is not subscriptable

代码:

i=0
for n, id_ in enumerate(train_ids):
    print(id_)
    path = TRAIN_PATH +'//'+ id_
    print(path)
    path_image = path + '//images//'
    path_mask = path + '//masks//'
    for image_file, mask_file in zip(sorted(os.listdir(path_image)), sorted(os.listdir(path_mask))):
      # print(path_image)#+image_file)
      # print(path_mask)#+mask_file)
      img=cv.imread(path_image+image_file)[:,:,:IMG_CHANNELS]
      img_ = cv.resize(img, (IMG_HEIGHT, IMG_WIDTH))
      img_=cv.cvtColor(img_,cv.COLOR_BGR2RGB)
      plt.imshow(img_)
      plt.show()
      X_train[i] = img_
      print(path_image+image_file)
      img2 = cv.imread(path_mask+mask_file)
      img1 = cv.resize(img2, (IMG_HEIGHT, IMG_WIDTH))
      img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
      Y_train[i] = img1
      plt.imshow(img1)
      plt.show()
      print(path_mask+mask_file)
      i=i+1

0 个答案:

没有答案