Esempio n. 1
0
 def j2d_processing(self, kp, center, scale, r, f, is_train):
     """Process gt 2D keypoints and apply all augmentation transforms."""
     nparts = kp.shape[0]
     for i in range(nparts):
         kp[i,0:2] = transform(kp[i,0:2]+1, center, scale, 
                               [constants.IMG_RES, constants.IMG_RES], rot=r)
     # convert to normalized coordinates
     kp[:,:-1] = 2.*kp[:,:-1]/constants.IMG_RES - 1.
     # flip the x coordinates
     if is_train and f:
          kp = flip_kp(kp)
     kp = kp.astype('float32')
     return kp
Esempio n. 2
0
 def j2d_processing(self, kp, center, scale, r, f):
     """Process gt 2D keypoints and apply all augmentation transforms."""
     nparts = kp.shape[0]
     for i in range(nparts):
         kp[i,0:2] = transform(kp[i,0:2]+1, center, scale, 
                               [self.options.img_res, self.options.img_res], rot=r)
     # convert to normalized coordinates
     kp[:,:-1] = 2.*kp[:,:-1]/self.options.img_res - 1.
     # flip the x coordinates
     if f:
          kp = flip_kp(kp)
     kp = kp.astype('float32')
     return kp
Esempio n. 3
0
    def j2d_heatmap_processing(self, kp, center, scale, r, f):
        # first process the keypoints
        nparts = kp.shape[0]
        for i in range(nparts):
            kp[i, 0:2] = transform(kp[i, 0:2] + 1, center, scale,
                                   [constants.IMG_RES, constants.IMG_RES], rot=r)
        # flip the x coordinates
        if f:
            kp = flip_kp(kp)

        # generate guaissan heatmap
        target_weight = np.ones((self.num_joints, 1), dtype=np.float32)
        target_weight[:, 0] = kp[:, -1]
        target = np.zeros((self.num_joints, self.heatmap_size[1], self.heatmap_size[0]), dtype=np.float32)
        tmp_size = self.sigma * 3

        for joint_id in range(self.num_joints):
            feat_stride = self.options.img_res / self.heatmap_size
            mu_x = int(kp[joint_id][0] / feat_stride[0] + 0.5)
            mu_y = int(kp[joint_id][1] / feat_stride[1] + 0.5)
            # Check that any part of the gaussian is in-bounds
            ul = [int(mu_x - tmp_size), int(mu_y - tmp_size)]
            br = [int(mu_x + tmp_size + 1), int(mu_y + tmp_size + 1)]
            if ul[0] >= self.heatmap_size[0] or ul[1] >= self.heatmap_size[1] \
                    or br[0] < 0 or br[1] < 0:
                # If not, just return the image as is
                target_weight[joint_id] = 0
                continue

            # # Generate gaussian
            size = 2 * tmp_size + 1
            x = np.arange(0, size, 1, np.float32)
            y = x[:, np.newaxis]
            x0 = y0 = size // 2
            # The gaussian is not normalized, we want the center value to equal 1
            g = np.exp(- ((x - x0) ** 2 + (y - y0) ** 2) / (2 * self.sigma ** 2))

            # Usable gaussian range
            g_x = max(0, -ul[0]), min(br[0], self.heatmap_size[0]) - ul[0]
            g_y = max(0, -ul[1]), min(br[1], self.heatmap_size[1]) - ul[1]
            # Image range
            img_x = max(0, ul[0]), min(br[0], self.heatmap_size[0])
            img_y = max(0, ul[1]), min(br[1], self.heatmap_size[1])

            v = target_weight[joint_id]
            if v > 0.5:
                target[joint_id][img_y[0]:img_y[1], img_x[0]:img_x[1]] = \
                    g[g_y[0]:g_y[1], g_x[0]:g_x[1]]

        return target, target_weight
Esempio n. 4
0
    def j2d_heatmap_location_processing(self, kp, center, scale, r, f):
        # first process the keypoints
        nparts = kp.shape[0]
        for i in range(nparts):
            kp[i, 0:2] = transform(kp[i, 0:2] + 1, center, scale,
                                   [constants.IMG_RES, constants.IMG_RES], rot=r)
        # flip the x coordinates
        if f:
            kp = flip_kp(kp)

        kp_heatmaps = kp.copy().astype(np.uint8)
        feat_stride = constants.IMG_RES / self.heatmap_size
        kp_heatmaps[:, :-1] = kp_heatmaps[:, :-1] / feat_stride
        kp_heatmaps = kp_heatmaps.astype('float32')
        return kp_heatmaps
def dp_annot_process(ann, heatmap_size, crop_res, center, scale, IsFlipped):
    bb_xywh = np.array(ann['bbox'])

    bbox_gt = [bb_xywh[0], bb_xywh[1], bb_xywh[0] + bb_xywh[2], bb_xywh[1] + bb_xywh[3]]
    # Cropped Upper left point
    crop_ul = np.array(transform([1, 1], center, scale, [crop_res] * 2, invert=1)) - 1
    # Cropped Bottom right point
    crop_br = np.array(
        transform([crop_res + 1] * 2, center, scale, [crop_res] * 2, invert=1)) - 1
    bbox_crop = np.concatenate([crop_ul, crop_br])

    dp_dict = {}
    M = heatmap_size

    # Create blobs for densepose supervision.
    ################################################## The mask
    All_labels = blob_utils.zeros(M ** 2, int32=True)
    All_Weights = blob_utils.zeros(M ** 2, int32=True)
    ################################################# The points
    X_points = blob_utils.zeros(196, int32=False)
    Y_points = blob_utils.zeros(196, int32=False)
    Ind_points = blob_utils.zeros(196, int32=True)
    I_points = blob_utils.zeros(196, int32=True)
    U_points = blob_utils.zeros(196, int32=False)
    V_points = blob_utils.zeros(196, int32=False)
    Uv_point_weights = blob_utils.zeros(196, int32=False)
    #################################################

    Ilabel = segm_utils.GetDensePoseMask(ann['dp_masks'])
    #
    GT_I = np.array(ann['dp_I'])
    GT_U = np.array(ann['dp_U'])
    GT_V = np.array(ann['dp_V'])
    GT_x = np.array(ann['dp_x'])
    GT_y = np.array(ann['dp_y'])
    GT_weights = np.ones(GT_I.shape).astype(np.float32)
    #
    ## Do the flipping of the densepose annotation !
    if IsFlipped:
        GT_I, GT_U, GT_V, GT_x, GT_y, Ilabel = DP.get_symmetric_densepose(GT_I, GT_U, GT_V, GT_x, GT_y,
                                                                          Ilabel)
    #
    roi_fg = bbox_crop
    roi_gt = bbox_gt
    #
    x1 = roi_fg[0];
    x2 = roi_fg[2]
    y1 = roi_fg[1];
    y2 = roi_fg[3]
    #
    x1_source = roi_gt[0];
    x2_source = roi_gt[2]
    y1_source = roi_gt[1];
    y2_source = roi_gt[3]
    #
    x_targets = (np.arange(x1, x2, (x2 - x1) / float(M)) - x1_source) * (255. / (x2_source - x1_source))
    y_targets = (np.arange(y1, y2, (y2 - y1) / float(M)) - y1_source) * (255. / (y2_source - y1_source))
    #
    x_targets = x_targets[0:M]  ## Strangely sometimes it can be M+1, so make sure size is OK!
    y_targets = y_targets[0:M]
    #
    [X_targets, Y_targets] = np.meshgrid(x_targets, y_targets)
    New_Index = cv2.remap(Ilabel, X_targets.astype(np.float32), Y_targets.astype(np.float32),
                          interpolation=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0))
    # #
    All_L = np.zeros(New_Index.shape)
    All_W = np.ones(New_Index.shape)
    #
    All_L = New_Index
    #
    gt_length_x = x2_source - x1_source
    gt_length_y = y2_source - y1_source
    #
    GT_y = ((GT_y / 255. * gt_length_y) + y1_source - y1) * (float(M) / (y2 - y1))
    GT_x = ((GT_x / 255. * gt_length_x) + x1_source - x1) * (float(M) / (x2 - x1))
    #
    GT_I[GT_y < 0] = 0
    GT_I[GT_y > (M - 1)] = 0
    GT_I[GT_x < 0] = 0
    GT_I[GT_x > (M - 1)] = 0
    #
    points_inside = GT_I > 0
    GT_U = GT_U[points_inside]
    GT_V = GT_V[points_inside]
    GT_x = GT_x[points_inside]
    GT_y = GT_y[points_inside]
    GT_weights = GT_weights[points_inside]
    GT_I = GT_I[points_inside]
    #
    X_points[0:len(GT_x)] = GT_x
    Y_points[0:len(GT_y)] = GT_y
    # Ind_points[i, 0:len(GT_I)] = i
    I_points[0:len(GT_I)] = GT_I
    U_points[0:len(GT_U)] = GT_U
    V_points[0:len(GT_V)] = GT_V
    Uv_point_weights[0:len(GT_weights)] = GT_weights

    All_labels[:] = np.reshape(All_L.astype(np.int32), M ** 2)
    All_Weights[:] = np.reshape(All_W.astype(np.int32), M ** 2)

    # K = cfg.BODY_UV_RCNN.NUM_PATCHES
    K = 24
    # print(K)
    #
    U_points = np.tile(U_points, [K + 1])
    V_points = np.tile(V_points, [K + 1])
    Uv_Weight_Points = np.zeros(U_points.shape)
    #
    for jjj in range(1, K + 1):
        Uv_Weight_Points[jjj * I_points.shape[0]: (jjj + 1) * I_points.shape[0]] = (I_points == jjj).astype(
            np.float32)
        # Uv_Weight_Points[:, jjj * I_points.shape[1]: (jjj + 1) * I_points.shape[1]] = (I_points == jjj).astype(
        #     np.float32)

    ##
    dp_dict['body_uv_ann_labels'] = np.array(All_labels).astype(np.int32)
    dp_dict['body_uv_ann_weights'] = np.array(All_Weights).astype(np.float32)
    #
    ##########################
    dp_dict['body_uv_X_points'] = X_points.astype(np.float32)
    dp_dict['body_uv_Y_points'] = Y_points.astype(np.float32)
    dp_dict['body_uv_Ind_points'] = Ind_points.astype(np.float32)
    dp_dict['body_uv_I_points'] = I_points.astype(np.float32)
    dp_dict['body_uv_U_points'] = U_points.astype(
        np.float32)  #### VERY IMPORTANT :   These are switched here :
    dp_dict['body_uv_V_points'] = V_points.astype(np.float32)
    dp_dict['body_uv_point_weights'] = Uv_Weight_Points.astype(np.float32)

    return dp_dict