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

问题描述 投票:0回答:1

各位,我想通过Google Colab运行一个Unet脚本进行培训。我试图通过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,但一直没有效果。

python google-colaboratory cv2 unity3d-unet
1个回答
0
投票

尝试打开并读取图片后,检查是否成功。

img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is not None:
    # Didn't work; do something here.
© www.soinside.com 2019 - 2024. All rights reserved.