函数 'cvtColor' 中的 OpenCV (4.1.2) 错误 !_src.empty()

OpenCV (4.1.2) error !_src.empty() in function 'cvtColor'

各位。我正在尝试通过 Google Colab 运行 Unet 脚本进行训练。但是我对 cv2 函数有错误。这是发生错误的代码部分:

def prepare_data(img_shape, ids, reader):
    img_count = len(ids)
    x_data = np.empty((img_count,) + img_shape,
                       dtype='uint8')
    y_data = np.empty((img_count, img_shape[0], img_shape[1], 1),
                       dtype='uint8')

   for i, idx in enumerate(ids):
       path = reader.get_image_path_by_id(idx)
       img = cv2.imread(path, cv2.IMREAD_COLOR)
       img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
       img = cv2.resize(img, dsize=(img_shape[1], img_shape[0]))

       mask = []

错误信息如下:

Traceback (most recent call last):
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 247, in <module> train(train_ids=fold4_train, val_ids=fold4_test, fold_num=fold_num, restart=args.restart)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 162, in train train_data, train_masks = prepare_data(img_shape, train_ids, dataset_reader)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 110, in prepare_data
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我试过用from skimage import io img = io.imread(file_path)代替cv2,但是没用。

尝试打开并阅读图像后,检查它是否有效。

img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is not None:
    # Didn't work; do something here.