def __getitem__(self, item): img_target_path_1 = self.img_target_paths_1[item] img_target_path_2 = self.img_target_paths_2[item] img_target_1 = Image.open(img_target_path_1).convert('RGB') img_target_2 = Image.open(img_target_path_2).convert('RGB') img_target_1 = img_target_1.resize( [self.opt.loadSize_v[0], self.opt.loadSize_v[1]], Image.BICUBIC) img_target_2 = img_target_2.resize( [self.opt.loadSize_v[0], self.opt.loadSize_v[1]], Image.BICUBIC) lab_target_path = self.lab_target_paths[item] lab_target = read_flo_file(lab_target_path) lab_target_h, lab_target_w = lab_target.shape[0], lab_target.shape[1] lab_target = resample(lab_target, [self.opt.loadSize_v[1], self.opt.loadSize_v[0]]) lab_target = torch.from_numpy(lab_target.transpose(2, 0, 1)) lab_target[0] = lab_target[0] * self.opt.loadSize_v[0] / lab_target_w lab_target[1] = lab_target[1] * self.opt.loadSize_v[1] / lab_target_h img_target_1 = self.transform_no_augment(img_target_1) img_target_2 = self.transform_no_augment(img_target_2) # print('img_source:',img_source_1.size()) return { 'img_target': [img_target_1, img_target_2], 'lab_target': lab_target, 'img_target_paths': [img_target_path_1, img_target_path_2], 'lab_target_path': lab_target_path }
def load_opticalflows_flo(flow_dir, im_names, flo_im_ext): forwardflows = [] backwardflows = [] fw_flow_arr = [] bw_flow_arr = [] fw_flow_dir = os.path.join(flow_dir, 'fw') bw_flow_dir = os.path.join(flow_dir, 'bw') for flow_id in xrange(len(im_names) - 1): flow_name = im_names[flow_id] + flo_im_ext fw_flow_path = os.path.join(fw_flow_dir, flow_name) bw_flow_path = os.path.join(bw_flow_dir, flow_name) fw_flow = read_flo_file(fw_flow_path) ##fw_flow.shape=(1080, 1920, 2) bw_flow = read_flo_file(bw_flow_path) forwardflows.append(fw_flow) backwardflows.append(bw_flow) fw_flow_arr = np.asarray(forwardflows) ## list to array bw_flow_arr = np.asarray(backwardflows) # if flow_id==0: # visualize_flow(fw_flow) print 'load', len(forwardflows), 'forward and backward flow files done...' return fw_flow_arr, bw_flow_arr
def __getitem__(self, item): index = random.randint(0, self.img_target_size - 1) img_source_path_1 = self.img_source_paths_1[item % self.img_source_size] img_source_path_2 = self.img_source_paths_2[item % self.img_source_size] if self.opt.dataset_mode == 'paired': img_target_path_1 = self.img_target_paths_1[item % self.img_target_size] img_target_path_2 = self.img_target_paths_2[item % self.img_target_size] elif self.opt.dataset_mode == 'unpaired': img_target_path_1 = self.img_target_paths_1[index] img_target_path_2 = self.img_target_paths_2[index] else: raise ValueError('Data mode [%s] is not recognized' % self.opt.dataset_mode) img_source_1 = Image.open(img_source_path_1).convert('RGB') img_source_2 = Image.open(img_source_path_2).convert('RGB') img_target_1 = Image.open(img_target_path_1).convert('RGB') img_target_2 = Image.open(img_target_path_2).convert('RGB') img_source_1 = img_source_1.resize( [self.opt.loadSize[0], self.opt.loadSize[1]], Image.BICUBIC) img_source_2 = img_source_2.resize( [self.opt.loadSize[0], self.opt.loadSize[1]], Image.BICUBIC) img_target_1 = img_target_1.resize( [self.opt.loadSize[0], self.opt.loadSize[1]], Image.BICUBIC) img_target_2 = img_target_2.resize( [self.opt.loadSize[0], self.opt.loadSize[1]], Image.BICUBIC) if self.opt.isTrain: lab_source_path = self.lab_source_paths[item % self.lab_source_size] # print(lab_source_path) lab_source = read_flo_file(lab_source_path) # flowtool = flow2img.Flow() # flow = flowtool._readFlow(lab_source_path) # print('flow:', flow.shape) # img = flowtool._flowToColor(lab_source.transpose(2,0,1)) # img = Image.fromarray(img).save('/home/lyc/Desktop/flow.png') lab_source_h, lab_source_w = lab_source.shape[0], lab_source.shape[ 1] lab_source = resample(lab_source, [self.opt.loadSize[1], self.opt.loadSize[0]]) # print(lab_source.shape) lab_source = torch.from_numpy(lab_source.transpose(2, 0, 1)) lab_source[0] = lab_source[0] * self.opt.loadSize[0] / lab_source_w lab_source[1] = lab_source[1] * self.opt.loadSize[1] / lab_source_h lab_target_path = self.lab_target_paths[item % self.lab_target_size] lab_target = read_flo_file(lab_target_path) lab_target_h, lab_target_w = lab_target.shape[0], lab_target.shape[ 1] lab_target = resample(lab_target, [self.opt.loadSize[1], self.opt.loadSize[0]]) lab_target = torch.from_numpy(lab_target.transpose(2, 0, 1)) lab_target[0] = lab_target[0] * self.opt.loadSize[0] / lab_target_w lab_target[1] = lab_target[1] * self.opt.loadSize[1] / lab_target_h # print('lab_source:',lab_source.size()) img_source_1 = self.transform_augment(img_source_1) img_source_2 = self.transform_augment(img_source_2) img_target_1 = self.transform_no_augment(img_target_1) img_target_2 = self.transform_no_augment(img_target_2) # print('img_source:',img_source_1.size()) return { 'img_source': [img_source_1, img_source_2], 'img_target': [img_target_1, img_target_2], 'lab_source': lab_source, 'lab_target': lab_target, 'img_source_paths': [img_source_path_1, img_source_path_2], 'img_target_paths': [img_target_path_1, img_target_path_2], 'lab_source_path': lab_source_path, 'lab_target_path': lab_target_path } else: img_source = self.transform_augment(img_source) img_target = self.transform_no_augment(img_target) return { 'img_source': img_source, 'img_target': img_target, 'img_source_paths': img_source_path, 'img_target_paths': img_target_path, }
img_seq[seq]) + "/frame_" + "0" + str(imgNum).zfill(3) + ".png" img2Path = imgPathPrefix + str( img_seq[seq]) + "/frame_" + "0" + str(imgNum + 1).zfill(3) + ".png" flowPath = flowPathPrefix + "/flow" + str( img_seq[seq]) + "/frame_" + "0" + str(imgNum).zfill(3) + ".flo" flowPath_invalid = flowPathPrefix + "/invalid" + str( img_seq[seq]) + "/frame_" + "0" + str(imgNum).zfill(3) + ".png" flowPath_occ = flowPathPrefix + "/occlusions" + str( img_seq[seq]) + "/frame_" + "0" + str(imgNum).zfill(3) + ".png" img1 = flowlib.read_image(img1Path) img2 = flowlib.read_image(img2Path) height = img1.shape[0] width = img1.shape[1] flow = flowlib.read_flo_file(flowPath) u = flow[:, :, 0] v = flow[:, :, 1] invalid, _ = flowlib.read_image(flowPath_invalid) occ, _ = flowlib.read_image(flowPath_occ) for i in range(halfPatchSize, height - halfPatchSize, 15): for j in range(halfPatchSize, width - halfPatchSize, 15): if invalid[i, j] == 0 and occ[i, j] == 0: if (i + int(v[i, j] - 1) - halfPatchSize < 0 or i + int(v[i, j] + 1) + halfPatchSize >= height): continue