Home > Software design >  open cv can not read all files form a path
open cv can not read all files form a path

Time:11-24

I am working with a dataset of 72 images and 72 masks. I appended the images into a numpy ndarrayI want the cv2 to read the files from the path corresponding to the files in the numpy ndarray.

this is the path to images and masks:

images_dir = '/content/drive/MyDrive/dataset/images'
masks_dir = '/content/drive/MyDrive/dataset/masks'

#adding the images to the numpy ndarray

file_names = np.sort(os.listdir(images_dir)) 
file_names = np.char.split(file_names, '.')
filenames = np.array([])
for i in range(len(file_names)):
    filenames = np.append(filenames, file_names[i][0])

this is the function I want open cv to read every image and then masks from the corresponding path:

def augment_dataset(count):
    '''Function for data augmentation
        Input:
            count - total no. of images after augmentation = initial no. of images * count
        Output:
            writes augmented images (input images & segmentation masks) to the working directory
    '''
    transform_1 = augment(512, 512)
    transform_2 = augment(480, 480)
    transform_3 = augment(512, 512)
    transform_4 = augment(800, 800)
    transform_5 = augment(1024, 1024)
    transform_6 = augment(800, 800)
    transform_7 = augment(1600, 1600)
    transform_8 = augment(1920, 1280)
    
    i = 0
    for i in range(count):
        for file in filenames:
            tile = file.split('_')[1]
            img = cv2.imread(images_dir file '.jpg')
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            mask = cv2.imread(masks_dir file '.png')
            mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

when I run the code:

augment_dataset(8)

there is this error showing up:

---------------------------------------------------------------------------

error                                     Traceback (most recent call last)

<ipython-input-112-fae4beb79e15> in <module>()
----> 1 augment_dataset(8)

<ipython-input-111-121d55acd3fc> in augment_dataset(count)
     20             tile = file.split('_')[1]
     21             img = cv2.imread(images_dir file '.jpg')
---> 22             img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     23             mask = cv2.imread(masks_dir file '.png')
     24             mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

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

I know that this is because OpenCV did not read the files. so how can I make openCV to read the files?

CodePudding user response:

In These cases, it's better to print(path/to/directory) to see if the directory is correct or not. In this case, we can see that I missed a / in the path to the directory. So Python was not able to parse the data. also, you can use ose.path.exists(path/to/directory) to see if the path exists or not. if the returned value is False, you must check the specified path for errors

  • Related