def __init__(self, transform, test_dir, batch_size=32): self.test_dir = test_dir self.batch_size = batch_size self.filenames = os.listdir(self.test_dir) self.batch_index = 0 self.transform = transform self.skeleton = load_skeleton(osp.join('/home/ubuntu/3d-testing/InterHand2.6M/data/InterHand2.6M/annotations', 'skeleton.txt'), 21*2) self.bboxs = open("bbs.txt", "r") self.files = open("my_hand_files.txt", "r") self.index = -1 self.fs = self.files.readlines() self.bs = self.bboxs.readlines()
def __init__(self, transform, mode, annot_subset): self.mode = mode # train, test, val self.annot_subset = annot_subset # all, human_annot, machine_annot self.img_path = '../data/InterHand2.6M/images' self.annot_path = '../data/InterHand2.6M/annotations' if self.annot_subset == 'machine_annot' and self.mode == 'val': self.rootnet_output_path = '../data/InterHand2.6M/rootnet_output/rootnet_interhand2.6m_output_machine_annot_val.json' else: self.rootnet_output_path = '../data/InterHand2.6M/rootnet_output/rootnet_interhand2.6m_output_all_test.json' self.transform = transform self.joint_num = 21 # single hand self.root_joint_idx = {'right': 20, 'left': 41} self.joint_type = {'right': np.arange(0,self.joint_num), 'left': np.arange(self.joint_num,self.joint_num*2)} self.skeleton = load_skeleton(osp.join(self.annot_path, 'skeleton.txt'), self.joint_num*2) self.datalist = [] self.datalist_sh = [] self.datalist_ih = [] self.sequence_names = [] # load annotation print("Load annotation from " + osp.join(self.annot_path, self.annot_subset)) db = COCO(osp.join(self.annot_path, self.annot_subset, 'InterHand2.6M_' + self.mode + '_data.json')) with open(osp.join(self.annot_path, self.annot_subset, 'InterHand2.6M_' + self.mode + '_camera.json')) as f: cameras = json.load(f) with open(osp.join(self.annot_path, self.annot_subset, 'InterHand2.6M_' + self.mode + '_joint_3d.json')) as f: joints = json.load(f) if (self.mode == 'val' or self.mode == 'test') and cfg.trans_test == 'rootnet': print("Get bbox and root depth from " + self.rootnet_output_path) rootnet_result = {} with open(self.rootnet_output_path) as f: annot = json.load(f) for i in range(len(annot)): rootnet_result[str(annot[i]['annot_id'])] = annot[i] else: print("Get bbox and root depth from groundtruth annotation") for aid in db.anns.keys(): ann = db.anns[aid] image_id = ann['image_id'] img = db.loadImgs(image_id)[0] capture_id = img['capture'] seq_name = img['seq_name'] cam = img['camera'] frame_idx = img['frame_idx'] img_path = osp.join(self.img_path, self.mode, img['file_name']) campos, camrot = np.array(cameras[str(capture_id)]['campos'][str(cam)], dtype=np.float32), np.array(cameras[str(capture_id)]['camrot'][str(cam)], dtype=np.float32) focal, princpt = np.array(cameras[str(capture_id)]['focal'][str(cam)], dtype=np.float32), np.array(cameras[str(capture_id)]['princpt'][str(cam)], dtype=np.float32) joint_world = np.array(joints[str(capture_id)][str(frame_idx)], dtype=np.float32) joint_cam = world2cam(joint_world.transpose(1,0), camrot, campos.reshape(3,1)).transpose(1,0) joint_img = cam2pixel(joint_cam, focal, princpt)[:,:2] joint_valid = np.array(ann['joint_valid'],dtype=np.float32).reshape(self.joint_num*2) # if root is not valid -> root-relative 3D pose is also not valid. Therefore, mark all joints as invalid joint_valid[self.joint_type['right']] *= joint_valid[self.root_joint_idx['right']] joint_valid[self.joint_type['left']] *= joint_valid[self.root_joint_idx['left']] hand_type = ann['hand_type'] hand_type_valid = np.array((ann['hand_type_valid']), dtype=np.float32) if (self.mode == 'val' or self.mode == 'test') and cfg.trans_test == 'rootnet': bbox = np.array(rootnet_result[str(aid)]['bbox'],dtype=np.float32) abs_depth = {'right': rootnet_result[str(aid)]['abs_depth'][0], 'left': rootnet_result[str(aid)]['abs_depth'][1]} else: img_width, img_height = img['width'], img['height'] bbox = np.array(ann['bbox'],dtype=np.float32) # x,y,w,h bbox = process_bbox(bbox, (img_height, img_width)) abs_depth = {'right': joint_cam[self.root_joint_idx['right'],2], 'left': joint_cam[self.root_joint_idx['left'],2]} cam_param = {'focal': focal, 'princpt': princpt} joint = {'cam_coord': joint_cam, 'img_coord': joint_img, 'valid': joint_valid} data = {'img_path': img_path, 'seq_name': seq_name, 'cam_param': cam_param, 'bbox': bbox, 'joint': joint, 'hand_type': hand_type, 'hand_type_valid': hand_type_valid, 'abs_depth': abs_depth, 'file_name': img['file_name'], 'capture': capture_id, 'cam': cam, 'frame': frame_idx} if hand_type == 'right' or hand_type == 'left': self.datalist_sh.append(data) else: self.datalist_ih.append(data) if seq_name not in self.sequence_names: self.sequence_names.append(seq_name) self.datalist = self.datalist_sh + self.datalist_ih print('Number of annotations in single hand sequences: ' + str(len(self.datalist_sh))) print('Number of annotations in interacting hand sequences: ' + str(len(self.datalist_ih)))
return args # argument parsing args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True # joint set information is in annotations/skeleton.txt joint_num = 21 # single hand root_joint_idx = {'right': 20, 'left': 41} joint_type = { 'right': np.arange(0, joint_num), 'left': np.arange(joint_num, joint_num * 2) } skeleton = load_skeleton( osp.join('../data/InterHand2.6M/annotations/skeleton.txt'), joint_num * 2) # snapshot load model_path = './snapshot_%d.pth.tar' % int(args.test_epoch) assert osp.exists(model_path), 'Cannot find model at ' + model_path print('Load checkpoint from {}'.format(model_path)) model = get_model('test', joint_num) model = DataParallel(model).cuda() ckpt = torch.load(model_path) model.load_state_dict(ckpt['network'], strict=False) model.eval() # prepare input image transform = transforms.ToTensor() img_path = 'input.jpg' original_img = cv2.imread(img_path)
gpus[1] = int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) assert args.test_epoch, 'Test epoch is required.' return args # argument parsing args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True # joint set information is in annotations/skeleton.txt joint_num = 21 # single hand root_joint_idx = {'right': 20, 'left': 41} joint_type = {'right': np.arange(0,joint_num), 'left': np.arange(joint_num,joint_num*2)} skeleton = load_skeleton(osp.join('/media/lwk/T7/datasets/InterHand2.6M/annotations/skeleton.txt'), joint_num*2) # snapshot load model_path = './snapshot_%d.pth.tar' % int(args.test_epoch) assert osp.exists(model_path), 'Cannot find model at ' + model_path print('Load checkpoint from {}'.format(model_path)) model = get_model('test', joint_num) model = DataParallel(model).cuda() ckpt = torch.load(model_path) model.load_state_dict(ckpt['network'], strict=False) model.eval() # prepare input image transform = transforms.ToTensor() img_path = 'input.jpg' original_img = cv2.imread(img_path)
def __init__(self, transform, mode, annot_subset): self.mode = mode self.root_path = '../data/RHD/data' self.rootnet_output_path = '../data/RHD/rootnet_output/rootnet_rhd_output.json' self.original_img_shape = (320, 320) # height, width self.transform = transform self.joint_num = 21 # single hand self.joint_type = {'right': np.arange(self.joint_num,self.joint_num*2), 'left': np.arange(0,self.joint_num)} self.root_joint_idx = {'right': 21, 'left': 0} self.skeleton = load_skeleton(osp.join(self.root_path, 'skeleton.txt'), self.joint_num*2) self.datalist = []; if self.mode == 'train': set = 'training' else: set = 'evaluation' self.annot_path = osp.join(self.root_path, 'RHD_' + set + '.json') db = COCO(self.annot_path) if self.mode == 'test' and cfg.trans_test == 'rootnet': print("Get bbox and root depth from " + self.rootnet_output_path) rootnet_result = {} with open(self.rootnet_output_path) as f: annot = json.load(f) for i in range(len(annot)): rootnet_result[str(annot[i]['annot_id'])] = annot[i] else: print("Get bbox and root depth from groundtruth annotation") for aid in db.anns.keys(): ann = db.anns[aid] image_id = ann['image_id'] img = db.loadImgs(image_id)[0] img_path = osp.join(self.root_path, set, 'color', img['file_name']) img_width, img_height = img['width'], img['height'] cam_param = img['cam_param'] focal, princpt = np.array(cam_param['focal'],dtype=np.float32), np.array(cam_param['princpt'],dtype=np.float32) joint_img = np.array(ann['joint_img'],dtype=np.float32) joint_cam = np.array(ann['joint_cam'],dtype=np.float32) joint_valid = np.array(ann['joint_valid'],dtype=np.float32) # transform single hand data to double hand data structure hand_type = ann['hand_type'] joint_img_dh = np.zeros((self.joint_num*2,2),dtype=np.float32) joint_cam_dh = np.zeros((self.joint_num*2,3),dtype=np.float32) joint_valid_dh = np.zeros((self.joint_num*2),dtype=np.float32) joint_img_dh[self.joint_type[hand_type]] = joint_img joint_cam_dh[self.joint_type[hand_type]] = joint_cam joint_valid_dh[self.joint_type[hand_type]] = joint_valid joint_img = joint_img_dh; joint_cam = joint_cam_dh; joint_valid = joint_valid_dh; if self.mode == 'test' and cfg.trans_test == 'rootnet': bbox = np.array(rootnet_result[str(aid)]['bbox'],dtype=np.float32) abs_depth = rootnet_result[str(aid)]['abs_depth'] else: bbox = np.array(ann['bbox'],dtype=np.float32) # x,y,w,h bbox = process_bbox(bbox, (img_height, img_width)) abs_depth = joint_cam[self.root_joint_idx[hand_type],2] # single hand abs depth cam_param = {'focal': focal, 'princpt': princpt} joint = {'cam_coord': joint_cam, 'img_coord': joint_img, 'valid': joint_valid} data = {'img_path': img_path, 'bbox': bbox, 'cam_param': cam_param, 'joint': joint, 'hand_type': hand_type, 'abs_depth': abs_depth} self.datalist.append(data)