carsten12 commited on
Commit
d1434a9
·
verified ·
1 Parent(s): 50b0701

Update utils.py

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