Example #1
0
    def embedding(self, im, bbox):
        im_in = im
        cords = bbox[0:4]
        center, scale = self.x1y1x2y2_to_cs(cords)
        r = 0
        trans = get_affine_transform(center[0], scale[0], r, (288, 384))
        input_image = cv2.warpAffine(im_in,
                                     trans, (288, 384),
                                     flags=cv2.INTER_LINEAR)
        with torch.no_grad():
            adj = self.reset_adj_no1()
            adj = adj.unsqueeze(0)
            adj = adj.type(torch.cuda.FloatTensor)
            input = self.transform(input_image)
            input = input.unsqueeze(0)
            with torch.cuda.device(self.gpu_id):
                input = input.cuda()
                feature = self.model(input, adj, flag=1)
                if cfg.TEST.FLIP_TEST:
                    input_flipped = np.flip(input.cpu().numpy(), 3).copy()
                    input_flipped = torch.from_numpy(input_flipped).cuda()
                    feature_flipped = self.model(input_flipped, adj, flag=1)
                    feature = (feature + feature_flipped) * 0.5

        feature = feature.cpu().numpy().squeeze()
        return feature.tolist()
    def extract_feature(self, im, bbox):
        im_in = im
        cords = bbox[0:4]
        center, scale = self.x1y1x2y2_to_cs(cords)
        r = 0

        trans = get_affine_transform(center[0], scale[0], r, self.image_size)
        input_image = cv2.warpAffine(
            im_in,
            trans, (int(self.image_width), int(self.image_height)),
            flags=cv2.INTER_LINEAR)
        with torch.no_grad():
            input = self.transform(input_image)
            input = input.unsqueeze(0)
            with torch.cuda.device(self.gpu_id):
                input = input.cuda()
                feature = self.model(input, flag=1)
                if cfg.TEST.FLIP_TEST:
                    input_flipped = np.flip(input.cpu().numpy(), 3).copy()
                    input_flipped = torch.from_numpy(input_flipped).cuda()
                    feature_flipped = self.model(input_flipped, flag=1)
                    feature = (feature + feature_flipped) * 0.5

        feature = feature.cpu().numpy().squeeze()
        return feature
	def detect_pose(self, im, bbox):
		im_in = im
		cords = bbox[0:4]
		center, scale = self.x1y1x2y2_to_cs(cords)
		r=0
		
		trans = get_affine_transform(center[0], scale[0], r, self.image_size)
		input_image = cv2.warpAffine(im_in, trans, (int(self.image_width), int(self.image_height)), flags=cv2.INTER_LINEAR)
		with torch.no_grad():
			input = self.transform(input_image)
			input = input.unsqueeze(0)
			with torch.cuda.device(self.gpu_id):
				input = input.cuda()
				output = self.model(input)
				if cfg.TEST.FLIP_TEST:
					input_flipped = np.flip(input.cpu().numpy(),3).copy()
					input_flipped = torch.from_numpy(input_flipped).cuda()
					output_flipped = self.model(input_flipped)
					output_flipped = flip_back(output_flipped.cpu().numpy(), self.flip_pairs)
					output_flipped = torch.from_numpy(output_flipped.copy()).cuda()
					if cfg.TEST.SHIFT_HEATMAP:
						output_flipped[:, :, :, 1:] = output_flipped.clone()[:, :, :, 0:-1]
					output = (output + output_flipped) *0.5
		
		output = output.cpu().numpy()
		preds, maxvals = self.get_final_preds(output, center, scale)
		preds, maxvals = preds.squeeze(), maxvals.squeeze()
		heatmaps = output.squeeze()
		#For posetrack dataset, the 3th and 4th channel is None
		preds = np.delete(preds, [3,4], axis=0)
		maxvals = np.delete(maxvals, [3,4], axis=0)
		heatmaps = np.delete(heatmaps, [3,4], axis=0)
		#print(heatmaps.shape,preds,maxvals)
		return preds, maxvals, heatmaps
Example #4
0
    def __getitem__(self, idx):
        db_rec = copy.deepcopy(self.db[idx])

        image_file = db_rec['image']
        im_id = db_rec['image_id'] if 'image_id' in db_rec else 0
        im_bbox = np.array(db_rec['bbox']) if 'bbox' in db_rec else np.array(
            [0, 0, 0, 0])
        #print(im_bbox)
        filename = db_rec['filename'] if 'filename' in db_rec else ''
        imgnum = db_rec['imgnum'] if 'imgnum' in db_rec else ''

        if self.data_format == 'zip':
            from utils import zipreader
            data_numpy = zipreader.imread(
                image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION)
        else:
            data_numpy = cv2.imread(
                image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION)

        joints = db_rec['joints_3d']
        joints_vis = db_rec['joints_3d_vis']

        c = db_rec['center']
        s = db_rec['scale']
        score = db_rec['score'] if 'score' in db_rec else 1
        r = 0

        if self.is_train:
            sf = self.scale_factor
            rf = self.rotation_factor
            s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)
            r = np.clip(np.random.randn()*rf, -rf*2, rf*2) \
                if random.random() <= 0.6 else 0

            if self.flip and random.random() <= 0.5:
                data_numpy = data_numpy[:, ::-1, :]
                joints, joints_vis = fliplr_joints(joints, joints_vis,
                                                   data_numpy.shape[1],
                                                   self.flip_pairs)
                c[0] = data_numpy.shape[1] - c[0] - 1

        trans = get_affine_transform(c, s, r, self.image_size)
        input = cv2.warpAffine(
            data_numpy,
            trans, (int(self.image_size[0]), int(self.image_size[1])),
            flags=cv2.INTER_LINEAR)

        if self.transform:
            input = self.transform(input)

        for i in range(self.num_joints):
            if joints_vis[i, 0] > 0.0:
                joints[i, 0:2] = affine_transform(joints[i, 0:2], trans)

        target, target_weight = self.generate_target(joints, joints_vis)

        target = torch.from_numpy(target)
        target_weight = torch.from_numpy(target_weight)

        meta = {
            'image': image_file,
            'image_id': im_id,
            'im_bbox': im_bbox,
            'filename': filename,
            'imgnum': imgnum,
            'joints': joints,
            'joints_vis': joints_vis,
            'center': c,
            'scale': s,
            'rotation': r,
            'score': score
        }
        # print(meta)

        return input, target, target_weight, meta