def _simple_renderer(rn, meshes, yrot=0, texture=None): mesh = meshes[0] if texture is not None: if not hasattr(mesh, 'ft'): mesh.ft = copy(mesh.f) vt = copy(mesh.v[:, :2]) vt -= np.min(vt, axis=0).reshape((1, -1)) vt /= np.max(vt, axis=0).reshape((1, -1)) mesh.vt = vt mesh.texture_filepath = rn.texture_image # Set camers parameters if texture is not None: rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, ft=mesh.ft, vt=mesh.vt, bgcolor=np.ones(3)) else: rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, bgcolor=np.ones(3)) for next_mesh in meshes[1:]: _stack_with(rn, next_mesh, texture) # Construct Back Light (on back right corner) albedo = rn.vc rn.vc = LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( np.array([-200, -100, -100]), yrot), vc=albedo, light_color=np.array([1, 1, 1])) # Construct Left Light rn.vc += LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY(np.array([800, 10, 300]), yrot), vc=albedo, light_color=np.array([1, 1, 1])) # Construct Right Light rn.vc += LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( np.array([-500, 500, 1000]), yrot), vc=albedo, light_color=np.array([.7, .7, .7])) return rn.r
def simple_renderer(rn, meshes, yrot=0): """Create a renderer, optionally with texture.""" mesh = meshes[0] if hasattr(rn, 'texture_image'): if not hasattr(mesh, 'ft'): mesh.ft = _copy(mesh.f) vt = _copy(mesh.v[:, :2]) vt -= _np.min(vt, axis=0).reshape((1, -1)) vt /= _np.max(vt, axis=0).reshape((1, -1)) mesh.vt = vt mesh.texture_filepath = rn.texture_image rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, ft=mesh.ft, vt=mesh.vt, bgcolor=_np.ones(3)) else: rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, bgcolor=_np.ones(3)) for next_mesh in meshes[1:]: _stack_with(rn, next_mesh) # pylint: disable=undefined-variable albedo = rn.vc # Construct Back Light (on back right corner) rn.vc = _odr_l.LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( _np.array([-200, -100, -100]), yrot), vc=albedo, light_color=_np.array([1, 1, 1])) # Construct Left Light rn.vc += _odr_l.LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( _np.array([800, 10, 300]), yrot), vc=albedo, light_color=_np.array([1, 1, 1])) # Construct Right Light rn.vc += _odr_l.LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( _np.array([-500, 500, 1000]), yrot), vc=albedo, light_color=_np.array([.7, .7, .7])) return rn.r
def render(model, image, cam, steps, segmented=False, scale=1.): """Render a sequence of views from a fitted body model.""" assert steps >= 1 if segmented: texture = None mesh = copy(_TEMPLATE_MESH_SEGMENTED) else: texture = None mesh = copy(_TEMPLATE_MESH) mesh.vc = _COLORS['pink'] #cmesh = Mesh(_os.path.join(_os.path.dirname(__file__), # 'template-bodyparts-corrected-labeled-split5.ply')) #mesh.vc = cmesh.vc.copy() # render ply model.betas[:len(cam['betas'])] = cam['betas'] model.pose[:] = cam['pose'] model.trans[:] = cam['trans'] mesh.v = model.r w, h = (image.shape[1], image.shape[0]) dist = np.abs(cam['t'][2] - np.mean(mesh.v, axis=0)[2]) rn = _create_renderer( w=int(w * scale), h=int(h * scale), near=4., far=30. + dist, rt=np.array(cam['rt']), t=np.array(cam['t']), f=np.array([cam['f'], cam['f']]) * scale, # c=np.array(cam['cam_c']), texture=texture) light_yrot = np.radians(120) base_mesh = copy(mesh) renderings = [] for angle in np.linspace(0., 2. * np.pi, num=steps, endpoint=False): mesh.v = rotateY(base_mesh.v, angle) imtmp = _simple_renderer(rn=rn, meshes=[mesh], yrot=light_yrot, texture=texture) if segmented: imtmp = imtmp[:, :, ::-1] renderings.append(imtmp * 255.) return renderings
def _simple_renderer(rn, meshes, yrot=0, texture=None, out_mesh_name=None, out_texture_name=None): mesh = meshes[0] if texture is not None: if not hasattr(mesh, 'ft'): """mesh.ft = copy(mesh.f) vt = copy(mesh.v[:, :2]) vt -= np.min(vt, axis=0).reshape((1, -1)) vt /= np.max(vt, axis=0).reshape((1, -1)) mesh.vt = vt""" mesh.vt, mesh.ft = Template_tex() mesh.texture_filepath = rn.texture_image # Set camers parameters if texture is not None: rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, ft=mesh.ft, vt=mesh.vt, bgcolor=np.ones(3)) else: rn.set(v=mesh.v, f=mesh.f, vc=mesh.vc, bgcolor=np.ones(3)) for next_mesh in meshes[1:]: _stack_with(rn, next_mesh, texture) if out_mesh_name is not None: write_obj(mesh, out_mesh_name) if (texture is not None) and (out_texture_name is not None): cv2.imwrite(out_texture_name, texture) with open(out_mesh_name[:-3] + "mtl", 'w') as my_file: my_file.write("newmtl Material.001" + "\n" + "Ns 96.078431" + "\n" + "Ka 1.000000 1.000000 1.000000" + "\n" + "Kd 0.640000 0.640000 0.640000" + "\n" + "Ks 0.000000 0.000000 0.000000" + "\n" + "Ke 0.412673 0.432000 0.226290" + "\n" + "Ni 1.000000" + "\n" + "d 1.000000" + "\n" + "illum 1" + "\n" + "map_Kd " + out_texture_name) # Construct Back Light (on back right corner) if texture is not None: rn.vc = ch.ones(rn.v.shape) else: albedo = rn.vc # Construct Back Light (on back right corner) rn.vc = LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( np.array([-200, -100, -100]), yrot), vc=albedo, light_color=np.array([1, 1, 1])) # Construct Left Light rn.vc += LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( np.array([800, 10, 300]), yrot), vc=albedo, light_color=np.array([1, 1, 1])) # Construct Right Light rn.vc += LambertianPointLight(f=rn.f, v=rn.v, num_verts=len(rn.v), light_pos=rotateY( np.array([-500, 500, 1000]), yrot), vc=albedo, light_color=np.array([.7, .7, .7])) return rn.r
def render(filename, model, image, cam, segmented=False, scale=1.): """Render a sequence of views from a fitted body model.""" if segmented: texture = filename + '_tex.png' else: texture = None mesh = copy(_TEMPLATE_MESH) #cmesh = Mesh(_os.path.join(_os.path.dirname(__file__), # 'template-bodyparts-corrected-labeled-split5.ply')) #mesh.vc = cmesh.vc.copy() mesh.vc = _COLORS['pink'] # render ply model.betas[:len(cam['betas'])] = cam['betas'] model.pose[:] = cam['pose'] #cam['trans'][1] = cam['trans'][1] - 0.08 #olhar essa translacao, error nos diferentes tipos de camera no SPIN a camera nao esta na mesma direcao .. model.trans[:] = cam['trans'] #model.trans[:] = [cam['trans'][0]*-1,cam['trans'][1],cam['trans'][2]] print cam['trans'] mesh.v = model.r w, h = (image.shape[1], image.shape[0]) dist = np.abs(cam['t'][2] - np.mean(mesh.v, axis=0)[2]) #pdb.set_trace() if not ('cam_c' is cam.keys()): cam['cam_c'] = [image.shape[1] / 2, image.shape[0] / 2] print 'Vai criar' rn = _create_renderer(w=int(w * scale), h=int(h * scale), near=0.1, far=30. + dist, rt=np.array(cam['rt']), t=np.array(cam['t']), f=np.array([cam['f'], cam['f']]) * scale, c=np.array(cam['cam_c']), texture=texture) print 'Criou' print cam['cam_c'] light_yrot = np.radians(120) base_mesh = copy(mesh) renderings = [] for angle in np.linspace(0., 2. * np.pi, num=1, endpoint=False): mesh.v = rotateY(mesh.v, angle) imtmp = _simple_renderer(rn=rn, meshes=[mesh], yrot=light_yrot, texture=texture) renderings.append(imtmp * 255.) return renderings