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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import sys
|
| 5 |
+
import numpy as np
|
| 6 |
+
from scipy.spatial import ConvexHull
|
| 7 |
+
import os
|
| 8 |
+
import argparse
|
| 9 |
+
|
| 10 |
+
def load_face_segmentation(path: str):
|
| 11 |
+
'''
|
| 12 |
+
Load face segmentation which defines for each body model part
|
| 13 |
+
the faces that belong to it.
|
| 14 |
+
:param path: str - path to json file with defined face segmentation
|
| 15 |
+
'''
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
with open(path, 'r') as f:
|
| 19 |
+
face_segmentation = json.load(f)
|
| 20 |
+
except FileNotFoundError:
|
| 21 |
+
sys.exit(f"No such file - {path}")
|
| 22 |
+
|
| 23 |
+
return face_segmentation
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def convex_hull_from_3D_points(slice_segments: np.ndarray):
|
| 27 |
+
'''
|
| 28 |
+
Cretes convex hull from 3D points
|
| 29 |
+
:param slice_segments: np.ndarray, dim N x 2 x 3 representing N 3D segments
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
:param slice_segments_hull: np.ndarray, dim N x 2 x 3 representing N 3D segments
|
| 33 |
+
that form the convex hull
|
| 34 |
+
'''
|
| 35 |
+
|
| 36 |
+
# stack all points in N x 3 array
|
| 37 |
+
merged_segment_points = np.concatenate(slice_segments)
|
| 38 |
+
unique_segment_points = np.unique(merged_segment_points,
|
| 39 |
+
axis=0)
|
| 40 |
+
|
| 41 |
+
# points lie in plane -- find which ax of x,y,z is redundant
|
| 42 |
+
redundant_plane_coord = np.argmin(np.max(unique_segment_points,axis=0) -
|
| 43 |
+
np.min(unique_segment_points,axis=0) )
|
| 44 |
+
non_redundant_coords = [x for x in range(3) if x!=redundant_plane_coord]
|
| 45 |
+
|
| 46 |
+
# create convex hull
|
| 47 |
+
hull = ConvexHull(unique_segment_points[:,non_redundant_coords])
|
| 48 |
+
segment_point_hull_inds = hull.simplices.reshape(-1)
|
| 49 |
+
|
| 50 |
+
slice_segments_hull = unique_segment_points[segment_point_hull_inds]
|
| 51 |
+
slice_segments_hull = slice_segments_hull.reshape(-1,2,3)
|
| 52 |
+
|
| 53 |
+
return slice_segments_hull
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def filter_body_part_slices(slice_segments:np.ndarray,
|
| 57 |
+
sliced_faces:np.ndarray,
|
| 58 |
+
measurement_name: str,
|
| 59 |
+
circumf_2_bodypart: dict,
|
| 60 |
+
face_segmentation: dict
|
| 61 |
+
):
|
| 62 |
+
'''
|
| 63 |
+
Remove segments that are not in the appropriate body part
|
| 64 |
+
for the given measurement.
|
| 65 |
+
:param slice_segments: np.ndarray - (N,2,3) for N segments
|
| 66 |
+
represented as two 3D points
|
| 67 |
+
:param sliced_faces: np.ndarray - (N,) representing the indices of the
|
| 68 |
+
faces
|
| 69 |
+
:param measurement_name: str - name of the measurement
|
| 70 |
+
:param circumf_2_bodypart: dict - dict mapping measurement to body part
|
| 71 |
+
:param face_segmentation: dict - dict mapping body part to all faces belonging
|
| 72 |
+
to it
|
| 73 |
+
|
| 74 |
+
Return:
|
| 75 |
+
:param slice_segments: np.ndarray (K,2,3) where K < N, for K segments
|
| 76 |
+
represented as two 3D points that are in the
|
| 77 |
+
appropriate body part
|
| 78 |
+
'''
|
| 79 |
+
|
| 80 |
+
if measurement_name in circumf_2_bodypart.keys():
|
| 81 |
+
|
| 82 |
+
body_parts = circumf_2_bodypart[measurement_name]
|
| 83 |
+
|
| 84 |
+
if isinstance(body_parts,list):
|
| 85 |
+
body_part_faces = [face_index for body_part in body_parts
|
| 86 |
+
for face_index in face_segmentation[body_part]]
|
| 87 |
+
else:
|
| 88 |
+
body_part_faces = face_segmentation[body_parts]
|
| 89 |
+
|
| 90 |
+
N_sliced_faces = sliced_faces.shape[0]
|
| 91 |
+
|
| 92 |
+
keep_segments = []
|
| 93 |
+
for i in range(N_sliced_faces):
|
| 94 |
+
if sliced_faces[i] in body_part_faces:
|
| 95 |
+
keep_segments.append(i)
|
| 96 |
+
|
| 97 |
+
return slice_segments[keep_segments]
|
| 98 |
+
|
| 99 |
+
else:
|
| 100 |
+
return slice_segments
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def point_segmentation_to_face_segmentation(
|
| 104 |
+
point_segmentation: dict,
|
| 105 |
+
faces: np.ndarray,
|
| 106 |
+
save_as: str = None):
|
| 107 |
+
"""
|
| 108 |
+
:param point_segmentation: dict - dict mapping body part to
|
| 109 |
+
all points belonging to it
|
| 110 |
+
:param faces: np.ndarray - (N,3) representing the indices of the faces
|
| 111 |
+
:param save_as: str - optional path to save face segmentation as json
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
import json
|
| 115 |
+
from tqdm import tqdm
|
| 116 |
+
from collections import Counter
|
| 117 |
+
|
| 118 |
+
# create body parts to index mapping
|
| 119 |
+
mapping_bp2ind = dict(zip(point_segmentation.keys(),
|
| 120 |
+
range(len(point_segmentation.keys()))))
|
| 121 |
+
mapping_ind2bp = {v:k for k,v in mapping_bp2ind.items()}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# assign each face to body part index
|
| 125 |
+
faces_segmentation = np.zeros_like(faces)
|
| 126 |
+
for i,face in tqdm(enumerate(faces)):
|
| 127 |
+
for bp_name, bp_indices in point_segmentation.items():
|
| 128 |
+
bp_label = mapping_bp2ind[bp_name]
|
| 129 |
+
|
| 130 |
+
for k in range(3):
|
| 131 |
+
if face[k] in bp_indices:
|
| 132 |
+
faces_segmentation[i,k] = bp_label
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# for each face, assign the most common body part
|
| 136 |
+
face_segmentation_final = np.zeros(faces_segmentation.shape[0])
|
| 137 |
+
for i,f in enumerate(faces_segmentation):
|
| 138 |
+
c = Counter(list(f))
|
| 139 |
+
face_segmentation_final[i] = c.most_common()[0][0]
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# create dict with body part as key and faces as values
|
| 143 |
+
face_segmentation_dict = {k:[] for k in mapping_bp2ind.keys()}
|
| 144 |
+
for i,fff in enumerate(face_segmentation_final):
|
| 145 |
+
face_segmentation_dict[mapping_ind2bp[int(fff)]].append(i)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# save face segmentation
|
| 149 |
+
if save_as:
|
| 150 |
+
with open(save_as, 'w') as f:
|
| 151 |
+
json.dump(face_segmentation_dict, f)
|
| 152 |
+
|
| 153 |
+
return face_segmentation_dict
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
|
| 158 |
+
parser = argparse.ArgumentParser(description='Create face segmentation from \
|
| 159 |
+
point segmentation of smpl/smplx models.')
|
| 160 |
+
parser.add_argument('--create_face_segmentation', action='store_true')
|
| 161 |
+
args = parser.parse_args()
|
| 162 |
+
|
| 163 |
+
if args.create_face_segmentation:
|
| 164 |
+
|
| 165 |
+
import smplx
|
| 166 |
+
|
| 167 |
+
segm_path = "data/smplx/point_segmentation_meshcapade.json"
|
| 168 |
+
with open(segm_path,"r") as f:
|
| 169 |
+
point_segmentation = json.load(f)
|
| 170 |
+
|
| 171 |
+
model_path = "data/smplx"
|
| 172 |
+
smplx_faces = smplx.SMPLX(model_path,ext="pkl").faces
|
| 173 |
+
|
| 174 |
+
save_as = "data/smplx/smplx_body_parts_2_faces.json"
|
| 175 |
+
|
| 176 |
+
_ = point_segmentation_to_face_segmentation(point_segmentation,
|
| 177 |
+
smplx_faces,
|
| 178 |
+
save_as)
|