def _load_texture_images( material_names: List[str], data_dir: str, material_properties: MaterialProperties, texture_files: TextureFiles, path_manager: PathManager, ) -> Tuple[MaterialProperties, TextureImages]: final_material_properties = {} texture_images = {} # Only keep the materials referenced in the obj. for material_name in material_names: if material_name in texture_files: # Load the texture image. path = os.path.join(data_dir, texture_files[material_name]) if path_manager.exists(path): image = (_read_image( path, path_manager=path_manager, format="RGB") / 255.0) image = torch.from_numpy(image) texture_images[material_name] = image else: msg = f"Texture file does not exist: {path}" warnings.warn(msg) if material_name in material_properties: final_material_properties[material_name] = material_properties[ material_name] return final_material_properties, texture_images
def _load_materials( material_names: List[str], f: Optional[str], *, data_dir: str, load_textures: bool, device: Device, path_manager: PathManager, ): """ Load materials and optionally textures from the specified path. Args: material_names: a list of the material names found in the .obj file. f: path to the material information. data_dir: the directory where the material texture files are located. load_textures: whether textures should be loaded. device: Device (as str or torch.device) on which to return the new tensors. path_manager: PathManager object to interpret paths. Returns: material_colors: dict of properties for each material. texture_images: dict of material names and texture images. """ if not load_textures: return None, None if not material_names or f is None: if material_names: warnings.warn("No mtl file provided") return None, None if not path_manager.exists(f): warnings.warn(f"Mtl file does not exist: {f}") return None, None # Texture mode uv wrap return load_mtl( f, material_names=material_names, data_dir=data_dir, path_manager=path_manager, device=device, )
def _siamese_to_coco(self, siamese_json): assert self._output_dir save_json = os.path.join(self._output_dir, "siamese2coco.json") pm = PathManager() pm.mkdirs(os.path.dirname(save_json)) with file_lock(save_json): if pm.exists(save_json): logger.warning( f"Using previously cached COCO format annotations at '{save_json}'. " "You need to clear the cache file if your dataset has been modified." ) else: logger.info( f"Converting annotations of dataset '{siamese_json}' to COCO format ...)" ) with pm.open(siamese_json, "r") as f: siamese_data = json.load(f) coco_data = {"data": []} exist_imgid = set() for key, datas in siamese_data.items(): # copy 'info', 'categories' if key != "data": coco_data[key] = datas else: for data in datas: for i in range(2): img_data = data[str(i)] if img_data["image_id"] in exist_imgid: continue else: exist_imgid.add(img_data["image_id"]) coco_data[key].append(img_data) self._logger.info( f"Number of unique images: {len(exist_imgid)}.") coco_data = convert_to_coco_dict(coco_data["data"], self._metadata) with pm.open(save_json, "w") as f: json.dump(coco_data, f) return save_json
def _load_texture_images( material_names: List[str], data_dir: str, material_properties: MaterialProperties, texture_files: TextureFiles, path_manager: PathManager, ) -> Tuple[MaterialProperties, TextureImages]: final_material_properties = {} texture_images = {} used_material_names = list(material_names) if not used_material_names and material_properties: if len(material_properties) > 1: raise ValueError( "Multiple materials but no usemtl declarations in the obj file" ) # No materials were specified in obj file and only one is in the # specified .mtl file, so we use it. used_material_names.append(next(iter(material_properties.keys()))) # Only keep the materials referenced in the obj. for material_name in used_material_names: if material_name in texture_files: # Load the texture image. path = os.path.join(data_dir, texture_files[material_name]) if path_manager.exists(path): image = (_read_image( path, path_manager=path_manager, format="RGB") / 255.0) image = torch.from_numpy(image) texture_images[material_name] = image else: msg = f"Texture file does not exist: {path}" warnings.warn(msg) if material_name in material_properties: final_material_properties[material_name] = material_properties[ material_name] return final_material_properties, texture_images