def load_meshes_sixd(obj_files, vertex_tmp_store_folder, recalculate_normals=False): import inout hashed_file_name = hashlib.md5( ''.join(obj_files) + 'load_meshes_sixd' + str(recalculate_normals)).hexdigest() + '.npy' out_file = os.path.join(vertex_tmp_store_folder, hashed_file_name) if os.path.exists(out_file): return np.load(out_file) else: bar = progressbar.ProgressBar() attributes = [] for model_path in bar(obj_files): model = inout.load_ply(model_path) vertices = np.array(model['pts']).astype(np.float32) if recalculate_normals: normals = calc_normals(vertices) else: normals = np.array(model['normals']).astype(np.float32) faces = np.array(model['faces']).astype(np.uint32) if 'colors' in model: colors = np.array(model['colors']).astype(np.uint32) attributes.append((vertices, normals, colors, faces)) else: attributes.append((vertices, normals, faces)) np.save(out_file, attributes) return attributes
import opengl_utils import inout import matplotlib.pyplot as plt import numpy as np obj_path = 'cat.ply' model = inout.load_ply(obj_path) model['pts'] = model['pts'] * 1000. im_size = (640, 480) opengl = opengl_utils.NormalRender(model, im_size) K = np.array([[572.4114, 0., 325.2611], [0., 573.57043, 242.04899], [0., 0., 1.]]) pose = np.array([[0.0950661, 0.983309, -0.155129, 0.0717302], [0.741596, -0.173913, -0.647911, -0.14907229], [-0.664076, -0.0534489, -0.745752, 1.0606388]], dtype=np.float32) depth = opengl.render(im_size, 100, 10000, K, pose[:, :3], pose[:, 3:] * 1000) plt.imshow(depth) plt.show()
def add_object(self, obj_id, model_path, **kwargs): """See base class.""" # Color of the object model (the original color saved with the object model # will be used if None). surf_color = None if 'surf_color' in kwargs: surf_color = kwargs['surf_color'] # Load the object model. model = inout.load_ply(model_path) self.models[obj_id] = model # Calculate the 3D bounding box of the model (will be used to set the near # and far clipping plane). bb = misc.calc_3d_bbox( model['pts'][:, 0], model['pts'][:, 1], model['pts'][:, 2]) self.model_bbox_corners[obj_id] = np.array([ [bb[0], bb[1], bb[2]], [bb[0], bb[1], bb[2] + bb[5]], [bb[0], bb[1] + bb[4], bb[2]], [bb[0], bb[1] + bb[4], bb[2] + bb[5]], [bb[0] + bb[3], bb[1], bb[2]], [bb[0] + bb[3], bb[1], bb[2] + bb[5]], [bb[0] + bb[3], bb[1] + bb[4], bb[2]], [bb[0] + bb[3], bb[1] + bb[4], bb[2] + bb[5]], ]) # Set texture/color of vertices. self.model_textures[obj_id] = None # Use the specified uniform surface color. if surf_color is not None: colors = np.tile(list(surf_color) + [1.0], [model['pts'].shape[0], 1]) # Set UV texture coordinates to dummy values. texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) # Use the model texture. elif 'texture_file' in self.models[obj_id].keys(): model_texture_path = os.path.join( os.path.dirname(model_path), self.models[obj_id]['texture_file']) model_texture = inout.load_im(model_texture_path) # Normalize the texture image. if model_texture.max() > 1.0: model_texture = model_texture.astype(np.float32) / 255.0 model_texture = np.flipud(model_texture) self.model_textures[obj_id] = model_texture # UV texture coordinates. texture_uv = model['texture_uv'] # Set the per-vertex color to dummy values. colors = np.zeros((model['pts'].shape[0], 3), np.float32) # Use the original model color. elif 'colors' in model.keys(): assert (model['pts'].shape[0] == model['colors'].shape[0]) colors = model['colors'] if colors.max() > 1.0: colors /= 255.0 # Color values are expected in range [0, 1]. # Set UV texture coordinates to dummy values. texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) # Set the model color to gray. else: colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 # Set UV texture coordinates to dummy values. texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) # Set the vertex data. if self.mode == 'depth': vertices_type = [ ('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1]) ] vertices = np.array(list(zip(model['pts'], colors)), vertices_type) else: if self.shading == 'flat': vertices_type = [ ('a_position', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2) ] vertices = np.array(list(zip(model['pts'], colors, texture_uv)), vertices_type) elif self.shading == 'phong': vertices_type = [ ('a_position', np.float32, 3), ('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2) ] vertices = np.array(list(zip(model['pts'], model['normals'], colors, texture_uv)), vertices_type) else: raise ValueError('Unknown shading type.') # Create vertex and index buffer for the loaded object model. self.vertex_buffers[obj_id] = vertices.view(gloo.VertexBuffer) self.index_buffers[obj_id] = \ model['faces'].flatten().astype(np.uint32).view(gloo.IndexBuffer) # Set shader for the selected shading. if self.shading == 'flat': rgb_fragment_code = _rgb_fragment_flat_code elif self.shading == 'phong': rgb_fragment_code = _rgb_fragment_phong_code else: raise ValueError('Unknown shading type.') # Prepare the RGB OpenGL program. rgb_program = gloo.Program(_rgb_vertex_code, rgb_fragment_code) rgb_program.bind(self.vertex_buffers[obj_id]) if self.model_textures[obj_id] is not None: rgb_program['u_use_texture'] = int(True) rgb_program['u_texture'] = self.model_textures[obj_id] else: rgb_program['u_use_texture'] = int(False) rgb_program['u_texture'] = np.zeros((1, 1, 4), np.float32) self.rgb_programs[obj_id] = rgb_program # Prepare the depth OpenGL program. depth_program = gloo.Program(_depth_vertex_code,_depth_fragment_code) depth_program.bind(self.vertex_buffers[obj_id]) self.depth_programs[obj_id] = depth_program
split_type_str = ' - ' + split_type if split_type is not None else '' # Load dataset parameters. dp_split = dataset_params.get_split_params(p['datasets_path'], dataset, split, split_type) model_type = 'eval' dp_model = dataset_params.get_model_params(p['datasets_path'], dataset, model_type) # Load object models. models = {} if p['error_type'] in ['ad', 'add', 'adi', 'mssd', 'mspd', 'proj']: misc.log('Loading object models...') for obj_id in dp_model['obj_ids']: models[obj_id] = inout.load_ply( dp_model['model_tpath'].format(obj_id=obj_id)) # Load models info. models_info = None if p['error_type'] in ['ad', 'add', 'adi', 'vsd', 'mssd', 'mspd', 'cus']: models_info = inout.load_json(dp_model['models_info_path'], keys_to_int=True) # Get sets of symmetry transformations for the object models. models_sym = None if p['error_type'] in ['mssd', 'mspd']: models_sym = {} for obj_id in dp_model['obj_ids']: models_sym[obj_id] = misc.get_symmetry_transformations( models_info[obj_id], p['max_sym_disc_step'])
model_mpath = base_path + 'models/obj_{:02d}.ply' # Already transformed objId = 5 scene_id = 5 # bbox_cens_path = 'output/bbox_cens.yml' scene_info_mpath = base_path + 'train/{:02d}/info.yml' scene_gt_mpath = base_path + 'train/{:02d}/gt.yml' w, h = 640, 480 with open(scene_gt_mpath.format(scene_id), 'r') as f: gt_info = yaml.load(f, Loader=yaml.CLoader) model = io.load_ply(model_mpath.format(objId)) numImages = len(gt_info) for im_id in range(0, numImages): # print rgb_in_mpath.format(objId,im_id) maskData = np.zeros((h, w), dtype=np.uint8) # rgb = io.load_im(rgb_in_mpath.format(objId,im_id)) [r, t] = getRotTrans(gt_info, im_id, objId) # print r # print t m_rgb = renderer.render(model, (w, h), cam_mat, r, t,
os.mkdir(seg_path.format(objId)) # bbox_cens_path = 'output/bbox_cens.yml' scene_info_mpath = base_path + 'test/{:02d}/info.yml' scene_gt_mpath = base_path + 'test/{:02d}/gt.yml' w, h = 640, 480 depthMask = np.zeros([480, 640]) with open(scene_gt_mpath.format(scene_id), 'r') as f: gt_info = yaml.load(f, Loader=yaml.CLoader) models = [] for x in range(1, 16): models.append(io.load_ply(model_mpath.format(x))) numImages = len(gt_info) def createMask(x): objId = gt_info_im[x]['obj_id'] [r, t] = getRotTrans(gt_info, im_id, objId) objDepImg = renderer.render(models[objId - 1], (w, h), cam_mat, r, t, shading='phong', mode='depth')
ren_depth.add_object(obj_id, dp_model['model_tpath'].format(obj_id=obj_id)) # Render training images for all object models. for obj_id in obj_ids: # Prepare output folders. misc.ensure_dir( os.path.dirname( out_rgb_tpath.format(out_path=out_path, obj_id=obj_id, im_id=0))) misc.ensure_dir( os.path.dirname( out_depth_tpath.format(out_path=out_path, obj_id=obj_id, im_id=0))) # Load model. model_path = dp_model['model_tpath'].format(obj_id=obj_id) model = inout.load_ply(model_path) # Load model texture. if 'texture_file' in model: model_texture_path =\ os.path.join(os.path.dirname(model_path), model['texture_file']) model_texture = inout.load_im(model_texture_path) else: model_texture = None scene_camera = {} scene_gt = {} im_id = 0 for radius in radii: # Sample viewpoints. view_sampler_mode = 'hinterstoisser' # 'hinterstoisser' or 'fibonacci'.
def __init__(self, model_path): self.model = inout.load_ply(model_path) self.K = np.array([ [679.78228891, 0.0, 316.4596997], [0.0, 678.69975492, 245.26957305], [0.0, 0.0, 1.0]],dtype=np.float32)
rayban_mpath = base_path + 'models/rayban.ply' # Already transformed objId = 9 scene_id = 9 # bbox_cens_path = 'output/bbox_cens.yml' scene_info_mpath = base_path + 'test/{:02d}/info.yml' scene_gt_mpath = base_path + 'test/{:02d}/gt.yml' w, h = 640, 480 with open(scene_gt_mpath.format(scene_id), 'r') as f: gt_info = yaml.load(f, Loader=yaml.CLoader) model = io.load_ply(model_mpath.format(objId)) rotY15 = np.array([0.9848, 0, 0.1736, 0, 1.0000, 0, -0.1736, 0, 0.9848]).reshape([3, 3]) rotZ90 = np.array([0, 1, 0, -1, 0, 0, 0, 0, 1]).reshape([3, 3]) glass = io.load_ply(rayban_mpath.format(objId)) glass['pts'] = glass['pts'] / 14 glass['pts'] = np.dot(np.dot(glass['pts'], rotZ90), rotY15) glass['pts'][:, 0] = glass['pts'][:, 0] / 1.2 glass['pts'][:, 0] = glass['pts'][:, 0] + 13.0 glass['pts'][:, 1] = glass['pts'][:, 1] * 1.2