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:
JavaScript
x
20
20
1
# # Creating Train / Val / Test folders
2
3
root_dir = 'Desktop/sem_8_project/brain/brain_tumor_dataset/' # data root path
4
classes_dir = ['no', 'yes'] #total labels
5
6
val_ratio = 0.15
7
test_ratio = 0.05
8
9
for cls in classes_dir:
10
os.makedirs(root_dir +'train/' + cls)
11
os.makedirs(root_dir +'val/' + cls)
12
os.makedirs(root_dir +'test/' + cls)
13
14
for cls in classes_dir:
15
src = root_dir + cls # Folder to copy images from
16
17
allFileNames = os.listdir(src)
18
np.random.shuffle(allFileNames)
19
train_FileNames, val_FileNames, test_FileNames = np.split(np.array(allFileNames),
20
and this is the error it shows:
JavaScript
1
12
12
1
---------------------------------------------------------------------------
2
FileNotFoundError Traceback (most recent call last)
3
<ipython-input-34-e2cee9006649> in <module>
4
3 src = root_dir + cls # Folder to copy images from
5
4
6
----> 5 allFileNames = os.listdir(src)
7
6 np.random.shuffle(allFileNames)
8
7 train_FileNames, val_FileNames, test_FileNames = np.split(np.array(allFileNames),
9
10
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Desktop/sem_8_project/brain/brain_tumor_dataset/no'
11
12
Advertisement
Answer
In the last 4 lines of code instead of using: allFileNames = os.listdir(src)
I used:
JavaScript
1
6
1
allFileNames = []
2
for filename in os.listdir(src):
3
img = cv2.imread(os.path.join(src,filename))
4
if img is not None:
5
allFileNames.append(img)
6
This worked for me and the directory error got fixed.