我正在尝试将我的图像数据集拆分为train test和val。它已经创建了train、test和val文件夹,但由于不断遇到错误,它是空的。这是我试图在jupyter笔记本上运行的代码,我已经导入了所需的库,如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),
这就是它显示的错误:
---------------------------------------------------------------------------
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'
正如错误消息所述,python找不到您提供的目录。您不能直接访问Desktop/,但必须指定其地址。尝试将此代码作为到桌面的链接。
os.path.join(os.environ['HOMEPATH'], 'Desktop')
在最后4行代码中,我没有使用:allFileNames = os.listdir(src)
,而是使用:
allFileNames = []
for filename in os.listdir(src):
img = cv2.imread(os.path.join(src,filename))
if img is not None:
allFileNames.append(img)
这对我很有效,目录错误也得到了修复。