OpenCV (4.1.2) error !_src.empty() in function 'cvtColor'
Question:
everyone. I`m trying to run a Unet script via Google Colab for training. But I have an error with cv2 function. Here is the part of the code where the error happened:
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 = []
The error message is following:
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'
I have tried to use from skimage import io
img = io.imread(file_path)
instead of cv2, but it has not worked.
Answers:
This is what happens when None
is passed to cv2.cvtColor()
After attempting to open and read an image, check that it worked.
img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is not None:
# Didn't work; do something here.
everyone. I`m trying to run a Unet script via Google Colab for training. But I have an error with cv2 function. Here is the part of the code where the error happened:
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 = []
The error message is following:
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'
I have tried to use from skimage import io
img = io.imread(file_path)
instead of cv2, but it has not worked.
This is what happens when None
is passed to cv2.cvtColor()
After attempting to open and read an image, check that it worked.
img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is not None:
# Didn't work; do something here.