数据集读取总结Image.open和cv2.imread

数据集读取总结

生成可检索的地址字典集

 data_path = listfilename
data_file_name = ["nyu_images/", "nyu_depths/", "nyu_hha/", "nyu_labels/"]
examples = []
example = {}
for data_dir in data_file_name:
    img_dir = data_path + data_dir
    file_names = os.listdir(img_dir)


    for file_name in file_names:


        if data_dir == "nyu_images/":
            img_path = img_dir + file_name
            img_id = file_name.split(".jpg")[0]
            example["img_path"] = img_path
            example["img_id"] = img_id
            examples.append(example)
        if data_dir == "nyu_depths/":
            img_path = img_dir + file_name
            example["depth_path"] = img_path
            examples.append(example)
        if data_dir == "nyu_hha/":
            img_path = img_dir + file_name
            example["hha_path"] = img_path
            examples.append(example)
        if data_dir == "nyu_labels/":
            img_path = img_dir + file_name
            example["label_path"] = img_path
            examples.append(example)


num_examples = len(examples)


return examples,num_examples

批量读取数据集

    def __getitem__(self, index):
        #self.paths['images'][index]
        #print (self.opt.scale,self.opt.flip,self.opt.crop,self.opt.colorjitter)
        path=self.paths_dict[index]
#        print ("print index===========%d",index)
#       print(len(path))
#        print(path.keys())
        img_path=path["img_path"]
        depth_path = path["depth_path"]
        hha_path = path["hha_path"]
        label_path = path["label_path"]

        # img = cv2.imread(img_path, -1)
        # img=np.asanyarray(Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)))
        # depth =cv2.imread(depth_path,-1)
        # depth = np.asanyarray(Image.fromarray(cv2.cvtColor(depth, cv2.COLOR_BGR2RGB)))
        # HHA = cv2.imread(hha_path,-1)
        # HHA = np.asanyarray(Image.fromarray(cv2.cvtColor(HHA, cv2.COLOR_BGR2RGB)))
        # seg = cv2.imread(label_path, -1)
        # seg = np.asanyarray(Image.fromarray(cv2.cvtColor(seg, cv2.COLOR_BGR2RGB)))

        img = np.asarray(Image.open(img_path)).astype(np.uint8)
        depth = np.asarray(Image.open(depth_path)).astype(np.float32)/120. # 1/10 * depth
        HHA = np.asarray(Image.open(hha_path))
        seg = np.asarray(Image.open(label_path)).astype(np.uint8)

        params = get_params(self.opt, seg.shape)
        depth_tensor_tranformed = transform(depth, params, normalize=False,istrain=self.opt.isTrain)
        seg_tensor_tranformed = transform(seg, params, normalize=False,method='nearest',istrain=self.opt.isTrain)
        if self.opt.inputmode == 'bgr-mean':
            img_tensor_tranformed = transform(img, params, normalize=False, istrain=self.opt.isTrain, option=1)
            HHA_tensor_tranformed = transform(HHA, params, normalize=False, istrain=self.opt.isTrain, option=2)
        else:
            img_tensor_tranformed = transform(img, params, istrain=self.opt.isTrain, option=1)
            HHA_tensor_tranformed = transform(HHA, params, istrain=self.opt.isTrain, option=2)


        # print img_tensor_tranformed
        # print(np.unique(depth_tensor_tranformed.numpy()).shape)
        # print img_tensor_tranformed.size()
        return {'image':img_tensor_tranformed,
                'depth':depth_tensor_tranformed,
                'seg': seg_tensor_tranformed,
                'HHA': HHA_tensor_tranformed,
                'imgpath': label_path}
上一篇:opencv C++图像读取


下一篇:关于imread 在scipy库移除的问题