'Image not resizing as it is not being loaded, where am I going wrong (opencv)

I am using this code to split a folder of 3000 images into seperate directories to use in a classifier.

the error is

'OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\resize.cpp:3784: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

Telling me the image variable is empty? I have checked the path it seems ok.. but am I missing something? More of a code check.

## build a cats v dogs image dataset

from os import listdir
from os.path import isfile, join
import cv2
import numpy as np
import sys
import os 
import shutil

mypath = r"data\catsdogs\images"

file_names = [f for f in listdir(mypath) if isfile(join(mypath,f))]

print(str(len(file_names))+' images loaded')

#extract 1000 images for training and 500 for validation

dog_count = 0
cat_count = 0
training_size = 1000
test_size = 500
training_images=[]
training_labels =[]
test_images = []
test_labels = []
size = 150

dog_dir_train = r"data\catsdogs\train\dogs"
cat_dir_train = r"data\catsdogs\train\cats"
dog_dir_val =  r"data\catsdogs\validation\dogs"
cat_dir_val =  r"data\catsdogs\validation\cats"

def make_dir(directory):
    if os.path.exists(directory):
        shutil.rmtree(directory)
    os.makedirs(directory)
    
make_dir(dog_dir_train)
make_dir(cat_dir_train)
make_dir(dog_dir_val)
make_dir(cat_dir_val)

def getZeros(number):
    if (number >10 and number <100):
        return "0"
    if (number <10):
        return "00"
    else:
        return ""
    
for i, file in enumerate(file_names):
    
    if file_names[i][0] == 'd':
        dog_count +=1
        image=cv2.imread(mypath+file)
        image=cv2.resize(image,(size,size), interpolation = cv2.INTER_AREA)
        if dog_count <= training_size:
            training_images.append(image)
            training_labels.append(1)
            zeros = getZeros(dog_count)
            cv2.imwrite(dog_dir_train +'dog' +str(zeros)+str(dog_count)+'.jpg',image)
        if dog_count> training_size and dog_count <=training_size+test_size:
            test_images.append(image)
            test_labels.append(1)
            zeros = getZeros(dog_count-1000)
            cv2.imwrite(dog_dir_val + 'dog' + str(zeros)+str(dog_count-1000) + '.jpg', image)
            
    if file_names[i][0] == 'c':
        cat_count +=1
        image=cv2.imread(mypath+file)
        image=cv2.resize(image,(size,size), interpolation = cv2.INTER_AREA)
        if cat_count <= training_size:
            training_images.append(image)
            training_labels.append(1)
            zeros = getZeros(cat_count)
            cv2.imwrite(cat_dir_train +'cat' +str(zeros)+str(cat_count)+'.jpg',image)
        if cat_count> training_size and cat_count <=training_size+test_size:
            test_images.append(image)
            test_labels.append(1)
            zeros = getZeros(cat_count-1000)
            cv2.imwrite(cat_dir_val + 'cat' + str(zeros)+str(cat_count-1000) + '.jpg', image)
            
            
            
        if dog_count == training_size+test_size and cat_count == training_size+test_size:
            break
        
print ('training and test data extraction complete')
            
            

I also am using 'r' before some paths. Is the best way as without it the code could not function.



Sources

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

Source: Stack Overflow

Solution Source