def vis_mask_(im, mask, save_path): assert im.shape[:2] == mask.shape ori_mask = mask mask = mask[..., np.newaxis] * np.array([[[255, 0, 0]]]) mask[ori_mask == 0] = im[ori_mask == 0] im = im * 0.3 + mask * 0.7 save_im(im, save_path, transpose=False, check_bound=True)
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 vis_mask_each_part_no_superimpose_(im, mask, save_path, num_parts): """ im: [H, W, 3] mask: [H, W] """ assert im.shape[:2] == mask.shape ims = [im.transpose(2, 0, 1)] for i in range(num_parts + 1): m = np.zeros_like(mask) m[mask == i] = 255 m = np.repeat(m[np.newaxis, ...], 3, 0) ims.append(m) im = make_im_grid(ims, 1, len(ims), 8, 128) save_im(im, save_path, transpose=True, check_bound=True)
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 vis_mask_each_part_(im, mask, save_path, num_parts): """ im: [H, W, 3] mask: [H, W] """ assert im.shape[:2] == mask.shape H, W = mask.shape # [3, H, W] im = im.transpose(2, 0, 1) # dim the image dim_im = im * 0.5 ims = [im] for i in range(num_parts + 1): # [3, H, W] m = np.array([0, 255, 0])[:, np.newaxis, np.newaxis].repeat(H, 1).repeat(W, 2) dim_im_copy = dim_im.copy() dim_im_copy[:, mask == i] = dim_im_copy[:, mask == i] * 0.4 + m[:, mask == i] * 0.6 ims.append(dim_im_copy) im = make_im_grid(ims, 1, len(ims), 8, 128) save_im(im, save_path, transpose=True, check_bound=True)
def vis_mask_(im, mask, save_path, num_parts): assert im.shape[:2] == mask.shape mask = cv2.applyColorMap((mask * (255 // num_parts)).astype(np.uint8), cv2.COLORMAP_PARULA) im = im * 0.2 + mask * 0.8 save_im(im, save_path, transpose=False, check_bound=True)
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
# 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', transpose=True, check_bound=True)
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 im_dir = '/home/users/houjing.huang/Dataset/coco/val2014/' ann_json = '/home/users/houjing.huang/Dataset/coco/annotations/densepose_coco_2014_minival.json' ann = load_json(ann_json) im_id_to_name = {rec['id']: rec['file_name'] for rec in ann['images']} save_dir = 'exp/vis_dense_pose/val_set' i = 0 for ann_ in ann['annotations']: if not 'dp_masks' in ann_: continue i += 1 if i == 11: break im = vis_fg(im_dir, ann_) save_name = im_id_to_name[ann_['image_id']][:-4] + '_' + str( ann_['id']) + '_fg' + '.jpg' save_im(im, osp.join(save_dir, save_name), transpose=False, check_bound=True)