I am trying to split my image data set into train test and val. It has already created the train, test and val folders, but its empty as I keep encountering an error. This is the code I am trying to run on jupyter notebook, I have imported the required libraries like os, numoy, shutil, random:
# # Creating Train / Val / Test folders
root_dir = 'Desktop/sem_8_project/brain/brain_tumor_dataset/' # data root path
classes_dir = ['no', 'yes'] #total labels
val_ratio = 0.15
test_ratio = 0.05
for cls in classes_dir:
os.makedirs(root_dir +'train/' + cls)
os.makedirs(root_dir +'val/' + cls)
os.makedirs(root_dir +'test/' + cls)
for cls in classes_dir:
src = root_dir + cls # Folder to copy images from
allFileNames = os.listdir(src)
np.random.shuffle(allFileNames)
train_FileNames, val_FileNames, test_FileNames = np.split(np.array(allFileNames),
and this is the error it shows:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-34-e2cee9006649> in <module>
3 src = root_dir + cls # Folder to copy images from
4
----> 5 allFileNames = os.listdir(src)
6 np.random.shuffle(allFileNames)
7 train_FileNames, val_FileNames, test_FileNames = np.split(np.array(allFileNames),
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Desktop/sem_8_project/brain/brain_tumor_dataset/no'
Advertisement
Answer
In the last 4 lines of code instead of using: allFileNames = os.listdir(src) I used:
allFileNames = []
for filename in os.listdir(src):
img = cv2.imread(os.path.join(src,filename))
if img is not None:
allFileNames.append(img)
This worked for me and the directory error got fixed.