读取文件夹内图像--方法2
读取文件夹内图像:供深度学习算法调用:Pytorch_Unet图像分割
import torch.utils.data as data
import PIL.Image as Image
import os
def make_dataset_txt(root, txt_file):
imgs=[]
with open(txt_file) as f:
indexs=f.readlines()
for i in range( len(indexs) ):
index = str( indexs ).split('.')
img=os.path.join(root, index+".jpg")
mask=os.path.join(root,index+".png")
imgs.append((img,mask))
return imgs
class LiverDataset(data.Dataset):
def __init__(self, root, txt_file, transform=None, target_transform=None):
imgs = make_dataset_txt(root, txt_file)
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
x_path, y_path = self.imgs
img_x = Image.open(x_path)
img_y = Image.open(y_path)
if self.transform is not None:
img_x = self.transform(img_x)
if self.target_transform is not None:
img_y = self.target_transform(img_y)
return img_x, img_y
def __len__(self):
return len(self.imgs)txt_file:000_label.png
001_label.png
002_label.png
003_label.png
004_label.png
005_label.png
006_label.png
007_label.png
008_label.png
009_label.png
010_label.png
011_label.png
……
页:
[1]