def _create_renderer( # pylint: disable=too-many-arguments w=640, h=480, rt=_np.zeros(3), t=_np.zeros(3), f=None, c=None, k=None, near=1., far=10., texture=None): """Create a renderer for the specified parameters.""" f = _np.array([w, w]) / 2. if f is None else f c = _np.array([w, h]) / 2. if c is None else c k = _np.zeros(5) if k is None else k if texture is not None: rn = _odr_r.TexturedRenderer() else: rn = _odr_r.ColoredRenderer() # pylint: disable=redefined-variable-type rn.camera = _odr_c.ProjectPoints(rt=rt, t=t, f=f, c=c, k=k) rn.frustum = {'near': near, 'far': far, 'height': h, 'width': w} if texture is not None: rn.texture_image = _np.asarray(_cv2.imread(texture), _np.float64) / 255. return rn
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 get_landmark_positions( stored_parameters, # pylint: disable=too-many-locals, too-many-arguments resolution, landmarks): """Get landmark positions for a given image.""" model = _MODEL_NEUTRAL model.betas[:len(stored_parameters['betas'])] = stored_parameters['betas'] mesh = _TEMPLATE_MESH # Get the full rendered mesh. model.pose[:] = stored_parameters['pose'] model.trans[:] = stored_parameters['trans'] mesh.v = model.r mesh_points = mesh.v[tuple(landmarks.values()), ] # Get the skeleton joints. J_onbetas = model.J_regressor.dot(mesh.v) skeleton_points = J_onbetas[(8, 5, 2, 1, 4, 7, 21, 19, 17, 16, 18, 20), ] camera = _odr_c.ProjectPoints(rt=stored_parameters['rt'], t=stored_parameters['t'], f=(stored_parameters['f'], stored_parameters['f']), c=_np.array(resolution) / 2., k=_np.zeros(5)) camera.v = _np.vstack((skeleton_points, mesh_points)) landmark_positions = camera.r.T.copy() return landmark_positions
def get_landmark_positions(betas, pose, trans, resolution, landmarks, rt, t, f): """Get landmark positions for a given image.""" # Pose the model. model = MODEL_NEUTRAL model.betas[:len(betas)] = betas model.pose[:] = pose model.trans[:] = trans mesh = _copy(_TEMPLATE_MESH) mesh.v = model.r mesh_points = mesh.v[tuple(landmarks.values()), ] J_onbetas = model.J_regressor.dot(model.r) skeleton_points = J_onbetas[(8, 5, 2, 1, 4, 7, 21, 19, 17, 16, 18, 20), ] # Do the projection. camera = _odr_c.ProjectPoints(rt=rt, t=t, f=(f, f), c=np.array(resolution) / 2., k=np.zeros(5)) camera.v = np.vstack((skeleton_points, mesh_points)) return camera.r.T.copy()
def get_landmark_positions(stored_parameter_fp, resolution, landmarks): """Get landmark positions for a given image.""" with open(stored_parameter_fp, 'rb') as inf: stored_parameters = pickle.load(inf) # Pose the model. model = MODEL_NEUTRAL model.betas[:len(stored_parameters['betas'])] = stored_parameters['betas'] model.pose[:] = stored_parameters['pose'] model.trans[:] = stored_parameters['trans'] mesh = _copy(_TEMPLATE_MESH) mesh.v = model.r mesh_points = mesh.v[tuple(landmarks.values()), ] J_onbetas = model.J_regressor.dot(model.r) skeleton_points = J_onbetas[(8, 5, 2, 1, 4, 7, 21, 19, 17, 16, 18, 20), ] # Do the projection. camera = _odr_c.ProjectPoints(rt=stored_parameters['rt'], t=stored_parameters['t'], f=(stored_parameters['f'], stored_parameters['f']), c=np.array(resolution) / 2., k=np.zeros(5)) camera.v = np.vstack((skeleton_points, mesh_points)) return camera.r.T.copy()
def get_landmark_positions( stored_parameter_fp, # pylint: disable=too-many-locals, too-many-arguments resolution, resolution_orig, # pylint: disable=unused-argument landmarks, trans=(0, 0), # pylint: disable=unused-argument scale=1., steps_x=3, steps_y=12): """Get landmark positions for a given image.""" with open(stored_parameter_fp, 'rb') as inf: stored_parameters = pickle.load(inf) orig_pose = np.array(stored_parameters['pose']).copy() orig_rt = np.array(stored_parameters['rt']).copy() orig_trans = np.array(stored_parameters['trans']).copy() orig_t = np.array(stored_parameters['t']).copy() model = MODEL_NEUTRAL model.betas[:len(stored_parameters['betas'])] = stored_parameters['betas'] mesh = _TEMPLATE_MESH # Use always the image center for rendering. orig_t[0] = 0. orig_t[1] = 0. orig_t[2] /= scale # Prepare for rendering. angles_y = np.linspace(0., 2. * (1. - 1. / steps_y) * np.pi, steps_y) elevation_maxextent = (steps_x - 1) // 2 * 0.2 * np.pi angles_x = np.linspace(-elevation_maxextent, elevation_maxextent, steps_x) if steps_x == 1: # Assume plain layout. angles_x = (0., ) angles = itertools.product(angles_y, angles_x) landmark_positions = [] full_parameters = [] for angle_y, angle_x in angles: stored_parameters['rt'] = orig_rt.copy() stored_parameters['rt'][0] += angle_x stored_parameters['rt'][1] += angle_y ####################################################################### # Zero out camera translation and rotation and move this information # to the body root joint rotations and 'trans' parameter. #print orig_pose[:3] cam_rdg, _ = cv2.Rodrigues(np.array(stored_parameters['rt'])) per_rdg, _ = cv2.Rodrigues(np.array(orig_pose)[:3]) resrot, _ = cv2.Rodrigues(np.dot(per_rdg, cam_rdg.T)) restrans = np.dot(-np.array(orig_trans), cam_rdg.T) + np.array(orig_t) stored_parameters['pose'][:3] = (-resrot).flat stored_parameters['trans'][:] = restrans stored_parameters['rt'][:] = [0, 0, 0] stored_parameters['t'][:] = [0, 0, 0] ####################################################################### # Get the full rendered mesh. model.pose[:] = stored_parameters['pose'] model.trans[:] = stored_parameters['trans'] mesh.v = model.r mesh_points = mesh.v[tuple(landmarks.values()), ] # Get the skeleton joints. J_onbetas = model.J_regressor.dot(mesh.v) skeleton_points = J_onbetas[(8, 5, 2, 1, 4, 7, 21, 19, 17, 16, 18, 20), ] # Do the projection. camera = _odr_c.ProjectPoints(rt=stored_parameters['rt'], t=stored_parameters['t'], f=(stored_parameters['f'], stored_parameters['f']), c=np.array(resolution) / 2., k=np.zeros(5)) camera.v = np.vstack((skeleton_points, mesh_points)) full_parameters.append( list(stored_parameters['betas']) + list(stored_parameters['pose']) + list(stored_parameters['trans']) + list(stored_parameters['rt']) + list(stored_parameters['t']) + [stored_parameters['f']]) landmark_positions.append(list(camera.r.T.copy().flat)) return landmark_positions, full_parameters