if input_type == 'UVV': cfg.MODEL.WEIGHTS = 'out_training/KAIST_flow_scale_UVV_0414/out_model_iter_11000.pth' else: cfg.MODEL.WEIGHTS = 'out_training/KAIST_flow_UVM_scale_0412/out_model_iter_22000.pth' #cfg.MODEL.ROI_HEADS.NUM_CLASSES = 17 folder = '../../../Datasets/KAIST/test/KAIST_flow_test_sanitized/' file_list = glob.glob(os.path.join(folder, '*.flo')) img_folder = '../../../Datasets/KAIST/test/' # Create predictor predictor = DefaultPredictor(cfg) scale = 0 for i in range(len(file_list)): fpath = file_list[i] flow = readFlow(fpath) image = np.zeros((flow.shape[0], flow.shape[1], 3)) image[:, :, 0] = flow[:, :, 0] image[:, :, 1] = flow[:, :, 1] # UVV if input_type == 'UVV': image[:, :, 2] = flow[:, :, 1] if scale == 1: image *= 3.0 image += 128.0 image[image > 255] = 255.0 else: image = np.abs(image) / 40.0 * 255.0 image[image > 255] = 255.0 else:
continue # rescale score 0~100 box[4] *= 100 f.write(str(i+1)+',') f.write(','.join(str(c) for c in box)) f.write('\n') """ scale = 1 for i in range(len(contents)): fpath = contents[i].split('\n')[0] set_num = fpath.split('/')[0] V_num = fpath.split('/')[1] img_num = fpath.split('/')[2] flow_path = folder + set_num + '_' + V_num + '_' + img_num + '.flo' flow = readFlow(flow_path) image = np.zeros((flow.shape[0], flow.shape[1], 3)) image[:,:,0] = flow[:,:,0] image[:,:,1] = flow[:,:,1] # UVV if input_type == 'UVV': image[:,:,2] = flow[:,:,1] if scale == 1: image *= 3.0 image += 128.0 image[image>255] = 255.0 else: image = np.abs(image) / 40.0 * 255.0 image[image>255] = 255.0 else:
def read_image(file_name, format=None): """ Read an image into the given format. Will apply rotation and flipping if the image has such exif information. Args: file_name (str): image file path format (str): one of the supported image modes in PIL, or "BGR" Returns: image (np.ndarray): an HWC image """ with PathManager.open(file_name, "rb") as f: if format == "BGRT": """ # KAIST folder = file_name.split('visible')[0] img_name = file_name.split('visible/')[1] path_rgb = file_name path_thermal = folder + 'lwir/' + img_name img_rgb = cv2.imread(path_rgb) img_thermal = cv2.imread(path_thermal) image = np.zeros((img_thermal.shape[0], img_thermal.shape[1], 4)) image [:,:,0:3] = img_rgb image [:,:,3] = img_thermal[:,:,0] """ # FLIR folder = file_name.split('thermal_8_bit/')[0] img_name = file_name.split('thermal_8_bit/')[1] img_name = img_name.split('.')[0] + '.jpg' rgb_path = folder + 'RGB/' + img_name #print(rgb_path) rgb_img = cv2.imread(rgb_path) thermal_img = cv2.imread(file_name) #import pdb; pdb.set_trace() rgb_img = cv2.resize(rgb_img, (thermal_img.shape[1], thermal_img.shape[0])) image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 4)) image[:, :, 0:3] = rgb_img image[:, :, 3] = thermal_img[:, :, 0] #""" elif format == 'BGR_only': folder = file_name.split('thermal_8_bit/')[0] img_name = file_name.split('thermal_8_bit/')[1] img_name = img_name.split('.')[0] + '.jpg' rgb_path = folder + 'resized_RGB/' + img_name image = cv2.imread(rgb_path) elif format == 'BGRTTT': # middle fusion """ # KAIST folder = file_name.split('visible')[0] img_name = file_name.split('visible/')[1] path_rgb = file_name path_thermal = folder + 'lwir/' + img_name img_rgb = cv2.imread(path_rgb) img_thermal = cv2.imread(path_thermal) image = np.zeros((img_thermal.shape[0], img_thermal.shape[1], 6)) image [:,:,0:3] = img_rgb image [:,:,3:] = img_thermal """ # FLIR folder = file_name.split('thermal_8_bit/')[0] img_name = file_name.split('thermal_8_bit/')[-1] img_name = img_name.split('.')[0] + '.jpg' rgb_path = folder + 'RGB/' + img_name rgb_img = cv2.imread(rgb_path) thermal_img = cv2.imread(file_name) rgb_img = cv2.resize(rgb_img, (thermal_img.shape[1], thermal_img.shape[0])) image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 6)) image[:, :, 0:3] = rgb_img image[:, :, 3:6] = thermal_img #""" elif format == 'BGRTTT_perturb': folder = file_name.split('thermal_8_bit/')[0] img_name = file_name.split('thermal_8_bit/')[1] img_name = img_name.split('.')[0] + '.jpg' rgb_path = folder + 'RGB/' + img_name rgb_img = cv2.imread(rgb_path) import os number = int(file_name.split('video_')[-1].split('.')[0]) #number = int(file_name.split('FLIR')[-1].split('_')[1].split('.')[0]) number -= 1 number_str = '{:05d}'.format(number) new_file_name = file_name.split('thermal')[ 0] + 'thermal_8_bit/FLIR_video_' + number_str + '.jpeg' if os.path.exists(new_file_name): thermal_img = cv2.imread(new_file_name) print(new_file_name, ' RGB: ', rgb_path) else: thermal_img = cv2.imread(file_name) print(file_name, ' RGB: ', rgb_path) rgb_img = cv2.resize(rgb_img, (thermal_img.shape[1], thermal_img.shape[0])) """ # Random resize import random ratio = random.randrange(100,121) / 100 width_new = int(640*ratio+0.5) height_new = int(512*ratio+0.5) rgb_img = cv2.resize(rgb_img, (width_new, height_new)) # Random crop [height, width, _] = thermal_img.shape diff_w = width_new - width diff_h = height_new - height if diff_w > 0: shift_x = random.randrange(0, diff_w) else: shift_x = 0 if diff_h > 0: shift_y = random.randrange(0, diff_h) else: shift_y = 0 rgb_img = rgb_img[shift_y:shift_y+height, shift_x:shift_x+width, :] """ #import pdb; pdb.set_trace() image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 6)) image[:, :, 0:3] = rgb_img image[:, :, 3:6] = thermal_img elif format == "mid_RGB_out": thermal_img = cv2.imread(file_name) image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 6)) image[:, :, 3:6] = thermal_img elif format == 'T_TCONV': #import pdb;pdb.set_trace() folder = file_name.split('thermal_8_bit/')[0] img_name = file_name.split('thermal_8_bit/')[1] img_name = img_name.split('.')[0] + '.jpeg' t_conv_path = folder + 'thermal_convert/' + img_name t_conv_img = cv2.imread(t_conv_path) thermal_img = cv2.imread(file_name) image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 2)) image[:, :, 0] = t_conv_img[:, :, 0] image[:, :, 1] = thermal_img[:, :, 0] elif format == 'T_TCONV_MASK': folder = file_name.split('thermal_convert/')[0] img_name = file_name.split('thermal_convert/')[1] #img_name = img_name.split('.')[0] + '.jpeg' t_conv_path = folder + 'thermal_convert/' + img_name t_conv_img = cv2.imread(t_conv_path) t_mask_path = folder + 'thermal_analysis/' + file_name.split( 'thermal_convert/')[1].split(".")[0] + '_mask.jpg' mask_img = cv2.imread(t_mask_path) thermal_img = cv2.imread(file_name) image = np.zeros((thermal_img.shape[0], thermal_img.shape[1], 3)) image[:, :, 0] = t_conv_img[:, :, 0] image[:, :, 1] = thermal_img[:, :, 0] image[:, :, 2] = mask_img[:, :, 0] elif format == 'UVV': # UV in first two channel, 0 in third channel if 'train' in file_name: folder = '../../../Datasets/KAIST/train/KAIST_flow_train_sanitized/' else: folder = '../../../Datasets/KAIST/test/KAIST_flow_test_sanitized/' fname = file_name.split('/')[-1].split('.')[0] + '.flo' fpath = folder + fname flow = readFlow(fpath) image = np.zeros((flow.shape[0], flow.shape[1], 3)) image[:, :, 0] = flow[:, :, 0] image[:, :, 1] = flow[:, :, 1] image[:, :, 2] = flow[:, :, 1] image *= 4.0 #image += 128.0 image[image > 255] = 255.0 #pdb.set_trace() """ image = np.abs(image) / 40.0 * 255.0 image[image>255] = 255.0 """ elif format == 'UVM': # UV + magnitude(uv) if 'train' in file_name: folder = '../../../Datasets/KAIST/train/KAIST_flow_train_sanitized/' else: folder = '../../../Datasets/KAIST/test/KAIST_flow_test_sanitized/' fname = file_name.split('/')[-1].split('.')[0] + '.flo' fpath = folder + fname flow = readFlow(fpath) flow_s = flow * flow magnitude = np.sqrt(flow_s[:, :, 0] + flow_s[:, :, 1]) image = np.zeros((flow.shape[0], flow.shape[1], 3)) image[:, :, 0] = flow[:, :, 0] image[:, :, 1] = flow[:, :, 1] image[:, :, 2] = magnitude image *= 4.0 #image += 128.0 image[image > 255] = 255.0 """ image = np.abs(image) / 40.0 * 255.0 image[image>255] = 255.0 """ elif format == 'BGRTUV': if 'train' in file_name: flow_folder = '../../../Datasets/KAIST/train/KAIST_flow_train_sanitized/' img_folder = '../../../Datasets/KAIST/train/' else: flow_folder = '../../../Datasets/KAIST/test/KAIST_flow_test_sanitized/' img_folder = '../../../Datasets/KAIST/test/' fname = file_name.split('/')[-1].split('.')[0] + '.flo' fpath = flow_folder + fname flow = readFlow(fpath) image = np.zeros((flow.shape[0], flow.shape[1], 6)) image[:, :, 4] = flow[:, :, 0] image[:, :, 5] = flow[:, :, 1] image *= 3 image += 128.0 image[image > 255] = 255.0 set_name = file_name.split('/')[-1].split('_')[0] V_name = file_name.split('/')[-1].split('_')[1] img_name = file_name.split('/')[-1].split('_')[2] fname_bgr = img_folder + set_name + '/' + V_name + '/visible/' + img_name fname_thr = img_folder + set_name + '/' + V_name + '/lwir/' + img_name bgr = cv2.imread(fname_bgr) thr = cv2.imread(fname_thr) image[:, :, 0:3] = bgr image[:, :, 3] = thr[:, :, 0] else: #import pdb; pdb.set_trace() image = Image.open(f) # capture and ignore this bug: https://github.com/python-pillow/Pillow/issues/3973 try: image = ImageOps.exif_transpose(image) except Exception: pass if format is not None: # PIL only supports RGB, so convert to RGB and flip channels over below conversion_format = format if format == "BGR": conversion_format = "RGB" image = image.convert(conversion_format) image = np.asarray(image) if format == "BGR": # flip channels if needed image = image[:, :, ::-1] # PIL squeezes out the channel dimension for "L", so make it HWC if format == "L": image = np.expand_dims(image, -1) return image