root_path = r'D:\jk' for i in range(10): cluster_i_path = os.path.join(root_path, f'cluster_{i}') if not os.path.exists(cluster_i_path): os.mkdir(cluster_i_path) for j, label in enumerate(labels): if label == i: old_path = os.path.join(path, f'{j}.png') new_path = os.path.join(cluster_i_path, f'{j}.png') os.rename(old_path, new_path),这段代码会自动在这个路径D:\jk下建立子文件吗
时间: 2024-04-28 10:23:24 浏览: 97
是的,这段代码会在指定的路径D:\jk下创建10个子文件夹,分别命名为cluster_0、cluster_1、cluster_2、……、cluster_9。如果这些子文件夹不存在,会自动创建它们。
接下来,对于给定的一组标签labels,代码会将标签为i的样本图片从原始路径path中移动到对应的子文件夹cluster_i中,并且将这些样本图片从原始的文件名(如'1.png')重命名为它们在cluster_i文件夹中的新文件名(如'1.png')。
相关问题
此代码import osimport numpy as npimport nibabel as nibfrom PIL import Image# 定义数据集路径data_path = r'C:\Users\Administrator\Desktop\LiTS2017'# 定义保存路径save_path = r'C:\Users\Administrator\Desktop\2D-LiTS2017'if not os.path.exists(save_path): os.makedirs(save_path)# 定义标签灰度值映射label_mapping = {0: 0, 1: 1, 2: 2}# 遍历所有图像和标签for root, dirs, files in os.walk(data_path): for file in files: if file.endswith('.nii'): # 读取图像或标签数据 img_path = os.path.join(root, file) img_data = nib.load(img_path).get_fdata() # 将3D数据转换为2D图片 for i in range(img_data.shape[2]): img_slice = img_data[:, :, i] img_slice = np.rot90(img_slice) img_slice = np.flipud(img_slice) img_slice = (img_slice - np.min(img_slice)) / (np.max(img_slice) - np.min(img_slice)) * 255 img_slice = img_slice.astype(np.uint8) img_slice = Image.fromarray(img_slice) # 处理标签数据 if 'label' in img_path.lower(): for k, v in label_mapping.items(): img_slice = np.array(img_slice) img_slice[img_slice == k] = v img_slice = Image.fromarray(img_slice) # 保存2D图片 save_dir = os.path.join(save_path, os.path.basename(root)) if not os.path.exists(save_dir): os.makedirs(save_dir) save_path_2d = os.path.join(save_dir, f'{file[:-4]}_{i:03d}.png') img_slice.save(save_path_2d)出现scaled = scaled.astype(np.promote_types(scaled.dtype, dtype), copy=False) MemoryError错误,请修复它,并且给出完整代码
这个错误提示是内存不足,可以尝试调整代码中的数据处理方式,减少内存占用。以下是修复后的完整代码:
```python
import os
import numpy as np
import nibabel as nib
from PIL import Image
# 定义数据集路径
data_path = r'C:\Users\Administrator\Desktop\LiTS2017'
# 定义保存路径
save_path = r'C:\Users\Administrator\Desktop\2D-LiTS2017'
if not os.path.exists(save_path):
os.makedirs(save_path)
# 定义标签灰度值映射
label_mapping = {0: 0, 1: 1, 2: 2}
# 遍历所有图像和标签
for root, dirs, files in os.walk(data_path):
for file in files:
if file.endswith('.nii'):
# 读取图像或标签数据
img_path = os.path.join(root, file)
img_data = nib.load(img_path).get_fdata()
# 将3D数据转换为2D图片
for i in range(img_data.shape[2]):
img_slice = img_data[:, :, i]
img_slice = np.rot90(img_slice)
img_slice = np.flipud(img_slice)
img_slice = (img_slice - np.min(img_slice)) / (np.max(img_slice) - np.min(img_slice)) * 255
img_slice = img_slice.astype(np.uint8)
img_slice = Image.fromarray(img_slice)
# 处理标签数据
if 'label' in img_path.lower():
for k, v in label_mapping.items():
img_slice_array = np.array(img_slice)
img_slice_array[img_slice_array == k] = v
img_slice = Image.fromarray(img_slice_array)
# 保存2D图片
save_dir = os.path.join(save_path, os.path.basename(root))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path_2d = os.path.join(save_dir, f'{file[:-4]}_{i:03d}.png')
img_slice.save(save_path_2d)
```
在处理标签数据时,将 `np.array` 转换为 `img_slice_array`,以减少内存占用。
将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))
def split_dataset(img_path, target_folder_path, output_path):
filename = os.listdir(img_path)
np.random.shuffle(filename)
train = filename[:int(len(filename) * 0.9)]
test = filename[int(len(filename) * 0.9):]
out_images = os.path.join(output_path, 'imgs')
os.makedirs(out_images, exist_ok=True)
out_images_train = os.path.join(out_images, 'training')
os.makedirs(out_images_train, exist_ok=True)
out_images_test = os.path.join(out_images, 'test')
os.makedirs(out_images_test, exist_ok=True)
out_annotations = os.path.join(output_path, 'annotations')
os.makedirs(out_annotations, exist_ok=True)
out_annotations_train = os.path.join(out_annotations, 'training')
os.makedirs(out_annotations_train, exist_ok=True)
out_annotations_test = os.path.join(out_annotations, 'test')
os.makedirs(out_annotations_test, exist_ok=True)
for i in train:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_train, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_train, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)
for i in test:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_test, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_test, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)
阅读全文