def vis_mask(sub_dir, split, num=10, save_dir='', num_parts=14): im_files = get_im_names(osp.join(root_dir, 'images', split), pattern='*.jpg', return_np=False, return_path=True) mask_files = get_im_names(osp.join(root_dir, sub_dir, split), pattern='*.png', return_np=False, return_path=True) im_files.sort() mask_files.sort() assert len(im_files) == len(mask_files) assert all([ osp.basename(im_f)[:-4] == osp.basename(mask_f)[:-4] for im_f, mask_f in zip(im_files, mask_files) ]) for im_f, mask_f in list(zip(im_files, mask_files))[:num]: for vis_func, save_sub_dir in zip( [vis_mask_, vis_mask_each_part_], ['all_on_one_im', 'one_im_per_mask']): vis_func( read_im(im_f, convert_rgb=True, resize_h_w=None, transpose=False), read_im(mask_f, convert_rgb=False, resize_h_w=None, transpose=False), osp.join(save_dir, save_sub_dir, osp.basename(im_f)), num_parts)
def vis_parts(im_dir, ann): # All keys: # 'category_id', 'id', 'image_id', # 'iscrowd', 'area', # 'num_keypoints', 'keypoints', # 'bbox', 'segmentation', 'dp_masks', # 'dp_x', 'dp_y', 'dp_I', 'dp_U', 'dp_V' # print(ann.keys()) part_mask = GetDensePoseMask(ann['dp_masks']) bbr = np.array(ann['bbox']).astype(int) # the box. ################ im = read_im(osp.join(im_dir, im_id_to_name[ann['image_id']]), resize_h_w=None, transpose=False) x1, y1, x2, y2 = bbr[0], bbr[1], bbr[0] + bbr[2], bbr[1] + bbr[3] x2 = min([x2, im.shape[1]]) y2 = min([y2, im.shape[0]]) ################ MaskIm = cv2.resize(part_mask, (int(x2 - x1), int(y2 - y1)), interpolation=cv2.INTER_NEAREST) MaskBool = np.tile((MaskIm == 0)[:, :, np.newaxis], [1, 1, 3]) # Replace the visualized mask image with I_vis. Mask_vis = cv2.applyColorMap((MaskIm * 15).astype(np.uint8), cv2.COLORMAP_PARULA)[:, :, :] Mask_vis[MaskBool] = im[y1:y2, x1:x2, :][MaskBool] im = im.copy().astype(np.float) im[y1:y2, x1:x2, :] = im[y1:y2, x1:x2, :] * 0.3 + Mask_vis * 0.7 return im
def fuse_parts_(mask_f, new_mask_f): ori_mask = read_im(mask_f, convert_rgb=False, resize_h_w=None, transpose=False) mask = ori_mask.copy() for m in mapping: mask[ori_mask == m[0]] = m[1] save_im(mask, new_mask_f, transpose=False, check_bound=False)
def check_mask_value_(mask_file): mask = read_im(mask_file, convert_rgb=False, resize_h_w=None, transpose=False) assert len(mask.shape) == 2, "{} mask.shape is {}".format( mask_file, mask.shape) assert mask.min() >= 0, "{} mask.min is {}".format(mask_file, mask.min()) assert mask.max() <= 14, "{} mask.max is {}".format(mask_file, mask.max())
def check_mask_value(split): """To make sure saving and loading mask images are correct.""" mask_files = get_im_names(osp.join(save_root_dir, mask_dir, split), pattern='*.png', return_np=False, return_path=True) for mf in mask_files: mask = read_im(mf, convert_rgb=False, resize_h_w=None, transpose=False) check_mask_value_(mask)
def vis_mask(split, num=10, save_dir=''): im_files = get_im_names(osp.join(save_root_dir, 'images', split), pattern='*.jpg', return_np=False, return_path=True) mask_files = get_im_names(osp.join(save_root_dir, mask_dir, split), pattern='*.png', return_np=False, return_path=True) im_files.sort() mask_files.sort() assert len(im_files) == len(mask_files) assert all([ osp.basename(im_f)[:-4] == osp.basename(mask_f)[:-4] for im_f, mask_f in zip(im_files, mask_files) ]) for im_f, mask_f in list(zip(im_files, mask_files))[:num]: vis_mask_( read_im(im_f, convert_rgb=True, resize_h_w=None, transpose=False), read_im(mask_f, convert_rgb=False, resize_h_w=None, transpose=False), osp.join(save_dir, osp.basename(im_f)))
def vis_fg(im_dir, ann): # All keys: # 'category_id', 'id', 'image_id', # 'iscrowd', 'area', # 'num_keypoints', 'keypoints', # 'bbox', 'segmentation', 'dp_masks', # 'dp_x', 'dp_y', 'dp_I', 'dp_U', 'dp_V' # print(ann.keys()) bbox = np.array(ann['bbox']).astype(int) # the box. ################ im = read_im(osp.join(im_dir, im_id_to_name[ann['image_id']]), resize_h_w=None, transpose=False) x1, y1, x2, y2 = bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3] x2 = min([x2, im.shape[1]]) y2 = min([y2, im.shape[0]]) ################ # Example: # => im.shape (428, 640, 3) # => MaskIm.shape (428, 640) # => MaskIm.dtype uint8 # => np.unique(MaskIm) [0 1] # # MaskIm.shape (428, 640) # # MaskIm.dtype uint8 # # np.unique(MaskIm) [0 1] # The same resolution as whole image. MaskIm = annToMask(ann, im.shape[0], im.shape[1]) print('=> im.shape', im.shape) print('=> MaskIm.shape', MaskIm.shape) print('=> MaskIm.dtype', MaskIm.dtype) print('=> np.unique(MaskIm)', np.unique(MaskIm)) if MaskIm.shape != im.shape[:2]: MaskIm = cv2.resize(MaskIm, (im.shape[1], im.shape[0]), interpolation=cv2.INTER_NEAREST) print('# MaskIm.shape', MaskIm.shape) print('# MaskIm.dtype', MaskIm.dtype) print('# np.unique(MaskIm)', np.unique(MaskIm)) ################ MaskBool = np.tile((MaskIm == 0)[:, :, np.newaxis], [1, 1, 3]) # Replace the visualized mask image with I_vis. Mask_vis = MaskIm[..., np.newaxis] * np.array([[[255, 0, 0]]]) Mask_vis[MaskBool] = im[MaskBool] im = im.copy().astype(np.float) im = im * 0.3 + Mask_vis * 0.7 return im
def transform(ann, im_dir, save_im_dir, save_mask_dir, split_file): """input ann: loaded json""" may_make_dir(osp.dirname(split_file)) fid = open(split_file, 'w') im_id_to_name = get_im_id_to_name(ann) anns = ann['annotations'] i = 0 for ann in anns: if not 'dp_masks' in ann: continue bbox = np.array(ann['bbox']).astype(int) im = read_im(osp.join(im_dir, im_id_to_name[ann['image_id']]), resize_h_w=None, transpose=False) x1, y1, x2, y2 = bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3] x1 = max([x1, 0]) y1 = max([y1, 0]) x2 = min([x2, im.shape[1]]) y2 = min([y2, im.shape[0]]) mask = GetDensePoseMask(ann['dp_masks']).astype(np.uint8) mask = cv2.resize(mask, (x2 - x1, y2 - y1), interpolation=cv2.INTER_NEAREST) assert mask.min() >= 0, "mask.min is {}".format(mask.min()) assert mask.max() <= 14, "mask.max is {}".format(mask.max()) im = im[y1:y2, x1:x2, :] save_im_name = im_id_to_name[ann['image_id']][:-4] + '_' + str( ann['id']) + '.jpg' save_im_path = osp.join(save_im_dir, save_im_name) save_im(im, save_im_path, transpose=False, check_bound=False) save_mask_name = im_id_to_name[ann['image_id']][:-4] + '_' + str( ann['id']) + '.png' save_mask_path = osp.join(save_mask_dir, save_mask_name) save_im(mask, save_mask_path, transpose=False, check_bound=False) rel_im_path = '/'.join(save_im_path.split('/')[-3:]) rel_mask_path = '/'.join(save_mask_path.split('/')[-3:]) fid.write('{}\t{}\n'.format(rel_im_path, rel_mask_path)) i += 1 fid.close() return len(anns), i
def transform(ann, im_dir, save_im_dir, save_mask_dir, split_file, to_save_im=False): """ Args: ann: loaded Densepose annotation json to_save_im: If your cropped images have been saved when preparing part parsing, this can be False """ may_make_dir(osp.dirname(split_file)) fid = open(split_file, 'w') im_id_to_name = get_im_id_to_name(ann) anns = ann['annotations'] i = 0 n_dp = 0 n_fg = 0 for ann in anns: i += 1 if not 'dp_masks' in ann: continue n_dp += 1 if not 'segmentation' in ann: continue n_fg += 1 bbox = np.array(ann['bbox']).astype(int) im = read_im(osp.join(im_dir, im_id_to_name[ann['image_id']]), resize_h_w=None, transpose=False) x1, y1, x2, y2 = bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3] x1 = max([x1, 0]) y1 = max([y1, 0]) x2 = min([x2, im.shape[1]]) y2 = min([y2, im.shape[0]]) # The same resolution as whole image. MaskIm = annToMask(ann, im.shape[0], im.shape[1]) assert len(MaskIm.shape) == 2, "len(MaskIm.shape) {}".format( len(MaskIm.shape)) assert MaskIm.shape == im.shape[: 2], "MaskIm.shape {}, im.shape {}".format( MaskIm.shape, im.shape) # print('=> im.shape', im.shape) # print('=> MaskIm.shape', MaskIm.shape) # print('=> MaskIm.dtype', MaskIm.dtype) # print('=> np.unique(MaskIm)', np.unique(MaskIm)) # The same resolution as bbox MaskIm = MaskIm[y1:y2, x1:x2] # print('=> MaskIm.shape', MaskIm.shape) check_mask_value_(MaskIm) save_im_name = im_id_to_name[ann['image_id']][:-4] + '_' + str( ann['id']) + '.jpg' save_im_path = osp.join(save_im_dir, save_im_name) if to_save_im: im = im[y1:y2, x1:x2, :] save_im(im, save_im_path, transpose=False, check_bound=False) save_mask_name = im_id_to_name[ann['image_id']][:-4] + '_' + str( ann['id']) + '_fg' + '.png' save_mask_path = osp.join(save_mask_dir, save_mask_name) save_im(MaskIm, save_mask_path, transpose=False, check_bound=False) rel_im_path = '/'.join(save_im_path.split('/')[-3:]) rel_mask_path = '/'.join(save_mask_path.split('/')[-3:]) fid.write('{}\t{}\n'.format(rel_im_path, rel_mask_path)) if i % 200 == 0: print('{}/{} Done'.format(i, len(anns))) fid.close() return len(anns), n_dp, n_fg
# Including Background class nclass = 8 # infer_im_path_mask_path_file = 'datasets/cuhk03/detected/infer_im_path_mask_path.txt' infer_im_path_mask_path_file = 'datasets/duke/infer_im_path_mask_path.txt' lines = read_lines(infer_im_path_mask_path_file, strip=True) im_paths, mask_paths = zip(*[l.split('\t') for l in lines]) rand_inds = np.random.permutation(range(len(im_paths))) im_paths = np.array(im_paths)[rand_inds] mask_paths = np.array(mask_paths)[rand_inds] num_vis_ims = 64 im_grid_list = [] for i in range(num_vis_ims): im = read_im(im_paths[i], convert_rgb=True, resize_h_w=None, transpose=False) mask = read_im(mask_paths[i], convert_rgb=False, resize_h_w=None, transpose=False) vis_one_im(im, mask, nclass, resize_h_w=(128, 64), im_grid_list=im_grid_list) n_cols = 8 n_rows = int(np.ceil(len(im_grid_list) / n_cols)) vis_im = make_im_grid(im_grid_list, n_rows, n_cols, 8, 255) save_im(vis_im, 'exp/vis_coco_part_pred/cuhk03_detected/vis_im.jpg',