def calc_hand_contact_prob(p_nums,
                           intents,
                           object_names,
                           contact_thresh=0.4,
                           search_r=15e-3,
                           hand_idx=1):
    """
  hand_idx: 0 for left, 1 for right
  """
    contact_probs = []
    for p_num in p_nums:
        for intent in intents:
            if object_names is None:
                object_names = get_object_names(p_num, intent)
            for object_name in object_names:
                print('{:d} : {:s} : {:s}'.format(p_num, intent, object_name))
                cp = ContactPose(p_num, intent, object_name)
                object_mesh = o3dio.read_triangle_mesh(cp.contactmap_filename)
                v = np.array(object_mesh.vertices)
                c = np.array(object_mesh.vertex_colors)[:, 0]
                c = mutils.texture_proc(c)
                idx = c >= contact_thresh
                v = v[idx]

                # read mano
                hand = cp.mano_meshes()[hand_idx]
                if hand is None:
                    continue

                h_pc = o3dg.PointCloud()
                h_pc.points = o3du.Vector3dVector(hand['vertices'])
                tree = o3dg.KDTreeFlann(h_pc)

                contact_prob = np.zeros(len(hand['vertices']))
                for vv in v:
                    k, idx, dist2 = tree.search_hybrid_vector_3d(
                        vv, search_r, 10)
                    for i in range(k):
                        # contact_prob[idx[i]] += (1.0/np.sqrt(dist2[i]))
                        contact_prob[idx[i]] = 1
                contact_probs.append(contact_prob)
    contact_probs = np.mean(contact_probs, axis=0)
    return contact_probs
def apply_semantic_colormap_to_mesh(mesh,
                                    semantic_idx,
                                    sigmoid_a=0.05,
                                    invert=False):
    colors = np.asarray(mesh.vertex_colors)[:, 0]
    colors = mutils.texture_proc(colors, a=sigmoid_a, invert=invert)

    # apply different colormaps based on finger
    mesh_colors = np.zeros((len(colors), 3))
    cmaps = ['Greys', 'Purples', 'Oranges', 'Greens', 'Blues', 'Reds']
    cmaps = [plt.cm.get_cmap(c) for c in cmaps]
    for semantic_id in np.unique(semantic_idx):
        if (len(cmaps) <= semantic_id):
            print('Not enough colormaps, ignoring semantic id {:d}'.format(
                semantic_id))
            continue
        idx = semantic_idx == semantic_id
        mesh_colors[idx] = cmaps[semantic_id](colors[idx])[:, :3]
    mesh.vertex_colors = o3du.Vector3dVector(mesh_colors)
    return mesh
def apply_colormap_to_mesh(mesh, sigmoid_a=0.05, invert=False):
    colors = np.asarray(mesh.vertex_colors)[:, 0]
    colors = mutils.texture_proc(colors, a=sigmoid_a, invert=invert)
    colors = plt.cm.inferno(colors)[:, :3]
    mesh.vertex_colors = o3du.Vector3dVector(colors)
    return mesh
Esempio n. 4
0
def discover_active_areas(finger_idx,
                          part_idx,
                          object_name,
                          intent,
                          p_nums=None,
                          color_thresh=0.4):
    """
  finger_idx: 0->4 : thumb->little
  part_idx: 0->3 : proximal to distal phalanges, 3 = finger tip
  """
    p_nums = p_nums or get_p_nums(object_name, intent)
    shuffle(p_nums)
    data_dir = osp.join('data', 'contactpose_data')

    # read object mesh
    vertices = None
    for p_num in p_nums:
        filename = osp.join(data_dir, f'full{p_num}_{intent}', object_name,
                            f'{object_name}.ply')
        if osp.isfile(filename):
            mesh = o3dio.read_triangle_mesh(filename)
        else:
            print('{:s} does not exist'.format(filename))
            continue
        vertices = np.asarray(mesh.vertices)
        break
    if vertices is None:
        print("no object model found")
        return

    line_ids = mutils.get_hand_line_ids()
    n_lines_per_hand = len(line_ids)
    n_parts_per_finger = 4

    touched_by_part = np.zeros(len(vertices))
    count = 0
    for p_num in p_nums:
        print(f'Processing full{p_num}_{intent} {object_name}')

        # read contact from the mesh
        filename = osp.join(data_dir, f'full{p_num}_{intent}', object_name,
                            f'{object_name}.ply')
        if osp.isfile(filename):
            mesh = o3dio.read_triangle_mesh(filename)
        else:
            print('{:s} does not exist'.format(filename))
            continue
        tex = np.asarray(mesh.vertex_colors)[:, 0]
        tex = mutils.texture_proc(tex)

        # read joints
        filename = osp.join(data_dir, f'full{p_num}_{intent}', object_name,
                            'annotations.json')
        try:
            with open(filename, 'r') as f:
                annotations = json.load(f)
        except FileNotFoundError:
            print('{:s} does not exist'.format(filename))
            continue

        ds = []
        for hand_idx, hand in enumerate(annotations['hands']):
            if hand['valid']:
                joints = np.asarray(hand['joints'])
                l0 = joints[line_ids[:, 0]]
                l1 = joints[line_ids[:, 1]]
                pl = mutils.closest_linesegment_point(l0, l1, vertices)
                d = pl - vertices[:, np.newaxis, :]
                d = np.linalg.norm(d, axis=2)
            else:
                d = np.inf * np.ones((len(vertices), n_lines_per_hand))
            ds.append(d)
        ds = np.hstack(ds)

        hand_idxs, line_idxs = divmod(np.argmin(ds, axis=1), n_lines_per_hand)
        finger_idxs, part_idxs = divmod(line_idxs, n_parts_per_finger)

        this_touched_by_part = np.logical_and(
            tex > color_thresh,
            np.logical_and(
                hand_idxs >= 0,
                np.logical_and(finger_idxs == finger_idx,
                               part_idxs == part_idx)))
        touched_by_part += this_touched_by_part
        count += 1

    touched_by_part /= count
    touched_by_part /= touched_by_part.max()
    filename = osp.join(
        'data',
        f'{object_name}_{intent}_{finger_idx}_{part_idx}_active_areas.npy')
    np.save(filename, touched_by_part)
    print('{:s} saved'.format(filename))