save progress

This commit is contained in:
2023-02-08 16:24:05 +00:00
parent 66e4212eab
commit ccec55d91f
6 changed files with 86602 additions and 45 deletions
+4 -1
View File
@@ -223,9 +223,12 @@ def _todict(matobj):
# can i cache map?
def read_posed_garment_frame(folder, id, garment, frame, map, mask=None, shape=None, shapedirs=None, pose=None, unshape=False, deorient=False):
def read_posed_garment_frame(folder, id, garment, frame, map=None, mask=None, shape=None, shapedirs=None, pose=None, unshape=False, deorient=False):
pc16_path = os.path.join(folder, id, garment + '.pc16')
verts = readPC2Frame(pc16_path, frame, True)
if map is None:
map = sio.loadmat(os.path.join(folder, id, garment + '_map.mat'))['map'].reshape((-1))
verts = verts[map, :]
if unshape:
+86 -43
View File
@@ -5,6 +5,7 @@ import argparse
import random
import string
import time
import networkx as nx
import tables as tb
import numpy as np
import trimesh as tm
@@ -18,22 +19,18 @@ from scipy.sparse.csgraph import floyd_warshall, dijkstra
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
from data.cloth3d_io import readPC2, readPC2Frame, writePC2, readOBJ
from data.cloth3d_io import readPC2, readPC2Frame, writePC2, readOBJ, read_garment_mask, read_posed_garment_frame
from custom_utils.custom_utils import _maskFaces
# from FeatureLines.superSMPL.smpl import SMPL
def get_body():
smpl = SMPL('FeatureLines/superSMPL')
pose = np.zeros((1, 72), dtype=np.float32)
beta = np.zeros((1, 11), dtype=np.float32)
R = smpl.run_rodrigues(pose)
T, _, _ = smpl.run(R, beta, True)
return np.squeeze(T)
body = readOBJ('custom_data/superSMPL.obj')
return body[0]
def get_skirt_body():
body = readOBJ('FeatureLines/superSMPL_skirt.obj')
body = readOBJ('custom_data/superSMPL_skirt.obj')
return body[0]
@@ -44,8 +41,6 @@ def export_to_ply(mesh, file):
output_file.close()
def get_closest_n_points(verts, idx, n):
dist = sp.spatial.distance.cdist(verts[idx, :], verts)
dist[dist == 0] = 1e9
@@ -61,9 +56,11 @@ def histedges_equalN(x, nbin):
np.sort(x))
def k_neighbours(k):
body = get_skirt_body()
if is_skirt:
body = get_skirt_body()
else:
body = get_body()
adj = np.ones((14475, 14475)) * 9999 # ???
diagonal = np.linspace(0, 14474, 14475, dtype=np.int32)
m = tm.Trimesh(body, F, process=False, validate=False)
@@ -73,10 +70,13 @@ def k_neighbours(k):
adj[edges[:, 1], edges[:, 0]] = lengths
adj[diagonal, diagonal] = 0
adj = csr_matrix(adj)
dist = dijkstra(adj, directed=False, unweighted=False, limit=0.5)
# G = nx.convert_matrix.from_numpy_array(adj)
np.save('MeshUtils/dijkstra_skirt.npy', dist)
adj = csr_matrix(adj)
dist = dijkstra(adj, directed=False, unweighted=False, limit=0.1)
skirt_modifier = '_skirt' if is_skirt else ''
np.save('custom_data/dijkstra' + skirt_modifier + '.npy', dist)
# dist = floydWarshall(adj, 14475)
@@ -86,20 +86,68 @@ def k_neighbours(k):
pass
class Data:
def __init__(self):
self.dist = None
self.mask = None
self.faces = None
class Cache:
def __init__(self, folder, neighbourhood, frames):
self.cache = dict()
for frame in tqdm.tqdm(frames):
id = frame.split()[0]
frame_nr = int(frame.split()[1])
garment = frame.split()[2]
if id in self.cache.keys():
continue
self.cache[id] = Data()
mask = read_garment_mask(folder, id, garment) #np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool)
k_neighbors = np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood)
_, faces = _maskFaces(np.zeros((14475,3)), F, mask) #np.load(os.path.join(folder, 'faces', f'{id}.npy'))
v = read_posed_garment_frame(folder, id, garment, frame_nr)[mask,:]
m = tm.Trimesh(v, faces, process=False, validate=False)
outliers = np.array([len(v) for v in m.vertex_neighbors]) == 0
outliers = np.nonzero(outliers)[0]
outliers_n_neighbours = get_closest_n_points(v, outliers, 2)
outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1)
faces = np.concatenate((faces, outliers_faces), axis=0)
G = tm.graph.vertex_adjacency_graph(m)
for i in range(v.shape[0]):
for k in range(25):
assert nx.has_path(G, i, k_neighbors[i, k]), f'No path between {i} and {k_neighbors[i, k]}'
self.cache[id].k_neighbors = k_neighbors
self.cache[id].mask = mask
self.cache[id].faces = faces
def __getitem__(self, id):
return self.cache[id]
def calculate_dist(folder, neighbourhood, frames):
out = dict()
dist = dict()
# frames = frames[int(args.start):int(args.end)]
for frame in tqdm.tqdm(frames):
id = frame.split('_')[0]
if id in out.keys():
id = frame.split()[0]
garment = frame.split()[2]
if id in dist.keys():
continue
mask = np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool)
mask = read_garment_mask(folder, id, garment) #np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool)
k_neighbors = np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood)
out[id] = k_neighbors
dist[id] = k_neighbors
return dist
return out
def generate_curvatures(folder, neighbourhood, frames):
@@ -107,36 +155,26 @@ def generate_curvatures(folder, neighbourhood, frames):
# frames = frames[int(args.start):int(args.end)]
# os.makedirs(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}'), exist_ok=True)
k_neighbors_cache = calculate_dist(folder, neighbourhood, frames)
cache = Cache(folder, neighbourhood, frames)
for frame in tqdm.tqdm(frames):
id = frame.split('_')[0]
frame_nr = frame.split('_')[1]
garment = frame.split('_')[2]
id = frame.split()[0]
frame_nr = int(frame.split()[1])
garment = frame.split()[2]
# bodies = readPC2(os.path.join(folder, id, 'Body_posed.pc16'), True)['V']
# offsetss = readPC2(os.path.join(folder, id, garment + '_registered.pc16'), True)['V']
mask = np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool)
_, faces = _maskFaces(np.zeros((14475,3)), F, mask) #np.load(os.path.join(folder, 'faces', f'{id}.npy'))
v = np.load(os.path.join(folder, 'mesh_posed', f'{id}_{frame_nr}.npz'))['mesh_posed'][mask,:]
# mask = np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool)
mask = cache[id].mask
faces = cache[id].faces
v = read_posed_garment_frame(folder, id, garment, frame_nr)[mask,:]
m = tm.Trimesh(v, faces, process=False, validate=False)
outliers = np.array([len(v) for v in m.vertex_neighbors]) == 0
outliers = np.nonzero(outliers)[0]
outliers_n_neighbours = get_closest_n_points(v, outliers, 2)
outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1)
faces = np.concatenate((faces, outliers_faces), axis=0)
verts = v
m = tm.Trimesh(verts, faces, process=False, validate=False)
export_to_ply(m, temp_file)
cloud = PyntCloud.from_file(temp_file)
k_neighbors = k_neighbors_cache[id] # np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood)
k_neighbors = cache[id].k_neighbors
ev = cloud.add_scalar_field("eigen_values", k_neighbors=k_neighbors)
cloud.add_scalar_field("curvature", ev=ev)
curvature = np.array(cloud.points["curvature(" + str(neighbourhood+1) + ")"])
@@ -145,7 +183,11 @@ def generate_curvatures(folder, neighbourhood, frames):
# np.savez_compressed(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}', f'{id}_{frame_nr}.npz'), mesh_curvature_raw=out)
group = h5file.get_node(f'/{id}/{frame_nr}/{garment}')
h5file.create_array(group, "curvature_raw", out)
if f'curvature_raw' in group:
array = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_raw')
array[:] = out
else:
h5file.create_array(group, "curvature_raw", out)
return None
@@ -259,7 +301,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", help = "process the given folder", default=r'/home/workspace/data/dataset/cloth3d/train')
parser.add_argument("-s", "--start", help = "start of range", default=0)
parser.add_argument("-e", "--end", help = "end of range", default=10)
parser.add_argument("-e", "--end", help = "end of range", default=1000)
parser.add_argument("-n", "--name", help = "end of range", default="lhunor")
args = parser.parse_args()
@@ -268,6 +310,7 @@ if __name__ == '__main__':
F = np.load("custom_utils/smpl_faces" + skirt_modifier + ".npy").astype(np.int32)
# k_neighbours(k=25)
folder = args.folder
frames_file = 'frames_all.txt'
+1 -1
View File
@@ -119,7 +119,7 @@ def create_extras_file(folder):
lines = [line.strip() for line in lines]
frames = lines
for frame in tqdm(frames):
for frame in tqdm(frames[:1000]):
# add a new empty group to the root group for each frame
id, frame_nr, garment = frame.split()
if not id in h5file.root: