def get_face_for_pixel(shape, mesh, model, configuration, coords): # pylint: disable=too-many-locals """Get the face index or -1 for the mesh in the given conf at coords.""" assert len(shape) == 2, str(shape) assert np.all(coords >= 0), str(coords) assert coords.ndim == 2, str(coords.ndim) for coord in coords.T: assert coord[0] < shape[1], "%s, %s" % (str(coord), str(shape)) assert coord[1] < shape[0], "%s, %s" % (str(coord), str(shape)) mesh = copy(mesh) # Setup the model. model.betas[:len(configuration['betas'])] = configuration['betas'] model.pose[:] = configuration['pose'] model.trans[:] = configuration['trans'] mesh.v = model.r inmesh = mesh # Assign a different color for each face. faces = np.arange(inmesh.f.size).reshape(-1, 3) vertices = np.empty((len(inmesh.f) * 3, 3)) vc = np.zeros_like(vertices) # pylint: disable=invalid-name for iface, face in enumerate(inmesh.f): vertices[iface * 3 + 0] = inmesh.v[face[0]] vertices[iface * 3 + 1] = inmesh.v[face[1]] vertices[iface * 3 + 2] = inmesh.v[face[2]] vc[iface * 3 + 0] = (float(iface % 255) / 255., float(iface / 255) / 255., 0.) vc[iface * 3 + 1] = (float(iface % 255) / 255., float(iface / 255) / 255., 0.) vc[iface * 3 + 2] = (float(iface % 255) / 255., float(iface / 255) / 255., 0.) fcmesh = Mesh(v=vertices, f=faces, vc=vc) # Render the mesh. dist = np.abs(configuration['t'][2] - np.mean(fcmesh.v, axis=0)[2]) rn = _odr_r.ColoredRenderer() # pylint: disable=redefined-variable-type, invalid-name rn.camera = _odr_c.ProjectPoints( rt=configuration['rt'], t=configuration['t'], f=np.array([configuration['f'], configuration['f']]), c=np.array([shape[1], shape[0]]) / 2., k=np.zeros(5)) rn.frustum = { 'near': 1., 'far': dist + 20., 'height': shape[0], 'width': shape[1] } rn.set(v=fcmesh.v, f=fcmesh.f, vc=fcmesh.vc, bgcolor=np.ones(3)) rendered = rn.r results = [-1 for _ in range(len(coords.T))] for coord_idx, coord in enumerate(coords.T): # Find the face or background. loc_color = (rendered[int(coord[1]), int(coord[0])] * 255.).astype('uint8') if np.all(loc_color == 255): continue else: assert loc_color[2] == 0, str(loc_color) face_idx = loc_color[1] * 255 + loc_color[0] assert face_idx >= 0 and face_idx < len(mesh.f) results[coord_idx] = face_idx return results
def bake_vertex_colors(inmesh): """Bake the vertex colors by superposition.""" faces = np.arange(inmesh.f.size).reshape(-1, 3) vertices = np.empty((len(inmesh.f) * 3, 3)) vc = np.zeros_like(vertices) # pylint: disable=invalid-name tmpar = np.ascontiguousarray(inmesh.vc).view( np.dtype((np.void, inmesh.vc.dtype.itemsize * inmesh.vc.shape[1]))) _, unique_idx = np.unique(tmpar, return_index=True) unique_clrs = inmesh.vc[unique_idx] for iface, face in enumerate(inmesh.f): vertices[iface * 3 + 0] = inmesh.v[face[0]] vertices[iface * 3 + 1] = inmesh.v[face[1]] vertices[iface * 3 + 2] = inmesh.v[face[2]] low_idx = np.argmin([ np.linalg.norm(inmesh.vc[face[0]]), np.linalg.norm(inmesh.vc[face[1]]), np.linalg.norm(inmesh.vc[face[2]]) ]) vc[iface * 3 + 0] = inmesh.vc[face[low_idx]] vc[iface * 3 + 1] = inmesh.vc[face[low_idx]] vc[iface * 3 + 2] = inmesh.vc[face[low_idx]] tmpar = np.ascontiguousarray(vc).view( np.dtype((np.void, vc.dtype.itemsize * vc.shape[1]))) _, unique_idx = np.unique(tmpar, return_index=True) unique_clrs_after = vc[unique_idx] for clr in unique_clrs: assert clr in unique_clrs_after for clr in unique_clrs_after: assert clr in unique_clrs outmesh = Mesh(v=vertices, f=faces, vc=vc) return outmesh
def show_capsules(capsules, mv): from body.mesh import Mesh capsules_v = [cps.v.r for cps in capsules] vs = np.vstack(capsules_v) fs = np.vstack( [cap_f + (i * len(capsules[0].v)) for i in range(len(capsules_v))]) mcap = Mesh(v=vs, f=fs) mv.set_static_meshes([mcap])
from opendr.renderer import ColoredRenderer, TexturedRenderer from opendr.camera import ProjectPoints from opendr.lighting import LambertianPointLight from up_tools.mesh import Mesh from up_tools.camera import rotateY _sys.path.insert(0, _path.join(_path.dirname(__file__), '..')) from config import SMPL_FP _sys.path.insert(0, SMPL_FP) try: from smpl.serialization import load_model as _load_model except: from smpl_webuser.serialization import load_model as _load_model _LOGGER = _logging.getLogger(__name__) _TEMPLATE_MESH = Mesh(filename=_os.path.join(_os.path.dirname(__file__), '..', 'models', '3D', 'template.ply')) _COLORS = { 'pink': [.6, .6, .8], 'cyan': [.7, .75, .5], 'yellow': [.5, .7, .75], 'grey': [.7, .7, .7], } def _create_renderer( # pylint: disable=too-many-arguments w=640, h=480, rt=np.zeros(3), t=np.zeros(3), f=None, c=None,
from opendr.camera import ProjectPoints from opendr.lighting import LambertianPointLight from opendr.renderer import ColoredRenderer, TexturedRenderer from up_tools.camera import rotateY from up_tools.mesh import Mesh _sys.path.insert(0, _path.join(_path.dirname(__file__), '..')) _sys.path.insert(0, SMPL_FP) try: from smpl.serialization import load_model as _load_model except: from smpl_webuser.serialization import load_model as _load_model _LOGGER = _logging.getLogger(__name__) _TEMPLATE_MESH = Mesh(filename=_os.path.join(_os.path.dirname(__file__), '..', 'models', '3D', 'template.ply')) _TEMPLATE_MESH_SEGMENTED = Mesh( filename=_os.path.join(_os.path.dirname(__file__), '..', 'models', '3D', 'template-bodyparts.ply')) _COLORS = { 'pink': [.6, .6, .8], 'cyan': [.7, .75, .5], 'yellow': [.5, .7, .75], 'grey': [.7, .7, .7], } def _create_renderer( # pylint: disable=too-many-arguments w=640, h=480, rt=np.zeros(3),
def cli(inmesh_fp, outmesh_fp): """Bake the vertex colors by superposition.""" inmesh = Mesh(filename=inmesh_fp) outmesh = bake_vertex_colors(inmesh) outmesh.write_ply(outmesh_fp)