'Trying to train object detection model using detecto and getting a CV2 error within dataset

I am using detecto and Google Colab Notebook to train and test my dataset. I used an image downloader off google and labelImg to create .xml files from my images. I am storing them in my drive in an object_detection folder. I am getting the below error:

Epoch 1 of 25
Begin iterating over training dataset
 68%|██████▊   | 75/111 [02:03<00:59,  1.64s/it]
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/detecto/utils.py in read_image(path)
    137     try:
--> 138         rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    139     except cv2.error as e:

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'


During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
8 frames
/usr/local/lib/python3.7/dist-packages/detecto/utils.py in read_image(path)
    138         rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    139     except cv2.error as e:
--> 140         raise ValueError(f'Could not convert image color: {str(e)}')
    141 
    142     return rgb_image

ValueError: Could not convert image color: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

This is the code that I currently have:_________________________

import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))

from detecto import core, utils, visualize
import numpy as np
import seaborn as sns
import torchvision.transforms as tf
from detecto.visualize import show_labeled_image, plot_prediction_grid

custom_transforms = tf.Compose([
tf.ToPILImage(),
tf.Resize(900),
tf.RandomHorizontalFlip(0.5),
tf.ColorJitter(saturation=0.2),
tf.ToTensor(),
utils.normalize_transform(),
])

dataset = core.Dataset('images/', transform = custom_transforms)
loader=core.DataLoader(dataset, batch_size=2, shuffle=True)
model = core.Model(['Card', 'Card Name'])
losses = model.fit(loader, dataset, epochs=25, lr_step_size=5, learning_rate= 0.001, verbose=True) 

Alternatively, I also tried:

import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))

from detecto import core, utils, visualize
import numpy as np
from detecto.visualize import show_labeled_image, plot_prediction_grid

dataset = core.Dataset('images/')
model = core.Model(['Card', 'Card Name'])
model.fit(dataset)

Please help me fix this issue.



Solution 1:[1]

I tried working with a smaller dataset of 30 images to train the model and figured out that a .gif image was accidentally in my dataset and unable to be read. Make sure there are no .gif files in the dataset and use a smaller test set if you get this error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1