def vis_parsing(path): parsing = cv2.imread(path, 0) parsing_color_list = eval('colormap_utils.{}'.format('CIHP20')) # CIHP20 parsing_color_list = colormap_utils.dict_bgr2rgb(parsing_color_list) colormap = colormap_utils.dict2array(parsing_color_list) parsing_color = colormap[parsing.astype(np.int)] cv2.imwrite('vis_train_{}_{}.png'.format(str(10), str(10)), parsing_color)
def vis_parsing333(path, dir): parsing = cv2.imread(path, 0) parsing_color_list = eval('colormap_utils.{}'.format('MHP59')) # CIHP20 parsing_color_list = colormap_utils.dict_bgr2rgb(parsing_color_list) colormap = colormap_utils.dict2array(parsing_color_list) parsing_color = colormap[parsing.astype(np.int)] cv2.imwrite(os.path.join(dir, os.path.basename(path)), parsing_color)
def vis_parsing(path, dir, colormap, im_ori, draw_contours): parsing = cv2.imread(path, 0) parsing_color_list = eval('colormap_utils.{}'.format(colormap)) # CIHP20 parsing_color_list = colormap_utils.dict_bgr2rgb(parsing_color_list) colormap = colormap_utils.dict2array(parsing_color_list) parsing_color = colormap[parsing.astype(np.int)] parsing_alpha = 0.9 idx = np.nonzero(parsing) im_ori = im_ori.astype(np.float32) im_ori[idx[0], idx[1], :] *= 1.0 - parsing_alpha im_ori += parsing_alpha * parsing_color ###### if draw_contours: contours, _ = cv2.findContours(parsing.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) parsing_color = parsing_color.astype(np.uint8) cv2.drawContours(im_ori, contours, -1, (0, 0, 255), 1) # M = cv2.moments(contours[1]) # 计算第一条轮廓的各阶矩,字典形式 # center_x = int(M["m10"] / M["m00"]) # center_y = int(M["m01"] / M["m00"]) # cv2.circle(parsing_color, (center_x, center_y), 30, 128, -1) # 绘制中心点 # print(center_x, center_y) cv2.imwrite(os.path.join(dir, os.path.basename(path)), im_ori)
def vis_parsing_dir(new_dir, colormap, img_dir, parsing_dir, draw_contours): coco_folder = '/xuhanzhu/mscoco2014' cihp_coco = COCO(coco_folder + '/annotations/densepose_coco_2014_train.json') im_ids = cihp_coco.getImgIds() for im_id in sorted(im_ids): im = cihp_coco.loadImgs(im_id)[0] filename = im['file_name'] ori_path = os.path.join(img_dir, filename) new_path = os.path.join(new_dir, filename) parsing_path = os.path.join(parsing_dir, filename.replace('.jpg', '.png')) parsing = cv2.imread(parsing_path, 0) if os.path.exists(ori_path): im_ori = cv2.imread(ori_path) parsing_color_list = eval( 'colormap_utils.{}'.format('MHP59')) # CIHP20 parsing_color_list = colormap_utils.dict_bgr2rgb( parsing_color_list) colormap = colormap_utils.dict2array(parsing_color_list) parsing_color = colormap[parsing.astype(np.int)] # parsing_color = parsing_color[..., ::-1] parsing_alpha = 0.9 idx = np.nonzero(parsing) im_ori = im_ori.astype(np.float32) im_ori[idx[0], idx[1], :] *= 1.0 - parsing_alpha im_ori += parsing_alpha * parsing_color ##### if draw_contours: contours, _ = cv2.findContours(parsing.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(im_ori, contours, -1, (0, 0, 255), 1) cv2.imwrite(new_path, im_ori) else: continue
im_ids = cihp_coco.getImgIds() Selected_im = im_ids[randint(0, len(im_ids))] Selected_im = 17144 im = cihp_coco.loadImgs(Selected_im)[0] ann_ids = cihp_coco.getAnnIds(imgIds=im['id']) anns = cihp_coco.loadAnns(ann_ids) im_name = os.path.join(coco_folder + 'train_img', im['file_name']) mask_name = os.path.join(coco_folder + 'train_seg', im['file_name'].replace('jpg', 'png')) for i, obj in enumerate(anns): parsing_name = os.path.join(coco_folder + 'train_parsing', obj['parsing']) parsing = cv2.imread(parsing_name, 0) parsing_color_list = eval('colormap_utils.{}'.format('CIHP20')) # CIHP20 parsing_color_list = colormap_utils.dict_bgr2rgb(parsing_color_list) colormap = colormap_utils.dict2array(parsing_color_list) parsing_color = colormap[parsing.astype(np.int)] cv2.imwrite('vis_train_{}_{}.png'.format(str(Selected_im), str(i)), parsing_color) # if os.path.exists(im_name) and os.path.exists(mask_name): # print('Exits', im_name, mask_name) # I = cv2.imread(im_name) # mask = cv2.imread(mask_name, 0) # # ins_pars = mask.numpy() # [instance].parsing # # ins_pars = np.array(F.to_pil_image(mask)) # mask = np.reshape(mask, I.shape[:2]) # # h, w = mask.shape[:2] # parsing_color_list = eval('colormap_utils.{}'.format('CIHP20')) # CIHP20 # parsing_color_list = colormap_utils.dict_bgr2rgb(parsing_color_list) # # I[0:h, 0:w, :] = vis_parsing(I[0:h, 0:w, :], mask, parsing_color_list)