整理cornell数据集文件
要创建image、pos_label、pos_label、neg_label文件夹,否则无法将指定文件复制到文件夹。此外,cornell数据集的所在路径最好选择有子目录没有单独文件的目录,直接选择文件所在的路径会报错,可以选择文件的上一级路径。
python代码:
import os
import shutil
file_root = r"F:\python_work\mobilenet\cornell_data\01" # 注意目录里面只能有子文件夹不能有文件
image = r"F:/python_work/mobilenet/cornell_data/image" # 注意要在目录新建一个image文件夹,否则只会生成一个无效的image文件
pos_label = r"F:/python_work/mobilenet/cornell_data/pos_label"
neg_label = r"F:/python_work/mobilenet/cornell_data/neg_label"
point_cloud = r"F:/python_work/mobilenet/cornell_data/point_cloud"
root = [x[0] for x in os.walk(file_root)] # os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。
root = root[1:]
for path in root:
for x in os.scandir(path): # 函数scandir扫描dir目录下(不包括子目录)满足filter过滤模式的文件,返回的结果是compare函数经过排序的,并保存在namelist中。
if x.name.endswith(".png"):
needpath = '{path}/{name}'.format(path=path, name=x.name)
shutil.copy(needpath, image) # shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst。DST必须是完整的目标文件名;
continue
if x.name.endswith("pos.txt"):
needpath = '{path}/{name}'.format(path=path, name=x.name)
shutil.copy(needpath, pos_label)
continue
if x.name.endswith("neg.txt"):
needpath = '{path}/{name}'.format(path=path, name=x.name)
shutil.copy(needpath, neg_label)
continue
else:
needpath = '{path}/{name}'.format(path=path, name=x.name)
shutil.copy(needpath, point_cloud)
参考链接的代码,在point_cloud文件夹里面并没有成功分类,而是把所有的cornell数据集文件都复制进去了,所以我稍微改动了一点。
参考链接:https://blog.csdn.net/weixin_45839124/article/details/109270692