def init(self, InputArgs=None): self.Parser = argparse.ArgumentParser(description='NOCSMapModule to visualize NOCS maps and camera poses.', fromfile_prefix_chars='@') ArgGroup = self.Parser.add_argument_group() ArgGroup.add_argument('--models', nargs='+', help='Specify input OBJ model paths. * globbing is supported.', required=True) ArgGroup.add_argument('--normalize', help='Choose to normalize models to lie within NOCS.', action='store_true') self.Parser.set_defaults(normalize=False) self.Args, _ = self.Parser.parse_known_args(InputArgs) if len(sys.argv) <= 1: self.Parser.print_help() exit() self.Models = [] self.OBJLoaders = [] for m in self.Args.models: self.Models.append(datastructures.PointSet3D()) if self.Args.normalize == True: print('[ INFO ]: Normalizing models to lie within NOCS.') self.OBJLoaders.append(obj_loader.Loader(m, isNormalize=True)) else: self.OBJLoaders.append(obj_loader.Loader(m, isNormalize=False)) if len(self.OBJLoaders[-1].vertices) > 0: self.Models[-1].Points = np.array(self.OBJLoaders[-1].vertices) if len(self.OBJLoaders[-1].vertcolors) > 0: print('[ INFO ]: Found vertex colors. Will be used for rendering.') self.Models[-1].Colors = np.asarray(self.OBJLoaders[-1].vertcolors) else: self.Models[-1].Colors = self.Models[-1].Points self.Models[-1].update() self.isDrawNOCSCube = True self.isDrawPoints = False self.isDrawMesh = True self.isDrawBB = False # self.RotateAngle = -90 # self.RotateAxis = np.array([1, 0, 0]) self.RotateAngle = 0 self.RotateAxis = np.array([1, 0, 0]) self.PointSize = 10.0 self.nModels = len(self.Args.models) self.activeModelIdx = self.nModels # nModels will show all
def init(self, argv=None): # print('Using arguments: ', argv) self.argv = argv if (len(argv) is not 1 + 1): print('[ USAGE ]:', argv[0], '<ModelFileOBJ>') exit() self.ModelPoints = datastructures.PointSet3D() self.OBJLoader = obj_loader.Loader(argv[1], isNormalize=True) if len(self.OBJLoader.vertices) > 0: self.vertices = self.OBJLoader.vertices self.ModelPoints.Points = np.array(self.vertices) self.ModelPoints.Colors = self.ModelPoints.Points self.ModelPoints.update() self.isDrawNOCSCube = True self.isDrawPoints = False self.isDrawMesh = True self.RotateAngle = 0 self.PointSize = 10.0
def init(self, InputArgs=None): self.Parser = argparse.ArgumentParser(description='NOCSMapModule to visualize NOCS maps and camera poses.', fromfile_prefix_chars='@') ArgGroup = self.Parser.add_argument_group() ArgGroup.add_argument('--models', nargs='+', help='Specify input OBJ model paths. * globbing is supported.', required=True) ArgGroup.add_argument('--normalize', help='Choose to normalize models to lie within NOCS.', action='store_true') self.Parser.set_defaults(normalize=False) ArgGroup.add_argument('--color-order', help='Choose to color models with point color order.', action='store_true') self.Parser.set_defaults(color_order=False) ArgGroup.add_argument('--half-offset', help='Offset all points by +0.5.', action='store_true') self.Parser.set_defaults(half_offset=False) ArgGroup.add_argument('--color-file', help='Choose a color from file. Should match number of points in model.', required=False) self.Args, _ = self.Parser.parse_known_args(InputArgs) if len(sys.argv) <= 1: self.Parser.print_help() exit() self.Models = [] self.OBJLoaders = [] for m in self.Args.models: self.Models.append(datastructures.PointSet3D()) if self.Args.normalize == True: print('[ INFO ]: Normalizing models to lie within NOCS.') self.OBJLoaders.append(obj_loader.Loader(m, isNormalize=True)) else: self.OBJLoaders.append(obj_loader.Loader(m, isNormalize=False)) if len(self.OBJLoaders[-1].vertices) > 0: self.Models[-1].Points = np.array(self.OBJLoaders[-1].vertices) if len(self.OBJLoaders[-1].vertcolors) > 0: print('[ INFO ]: Found vertex colors. Will be used for rendering.') self.Models[-1].Colors = np.asarray(self.OBJLoaders[-1].vertcolors) else: self.Models[-1].Colors = self.Models[-1].Points if self.Args.color_file is not None: print('[ INFO ]: Coloring models with file.') Colors = np.load(self.Args.color_file) # print(self.Models[-1].Colors.shape) # print(Colors[:, :3].shape) self.Models[-1].Colors = np.asarray(Colors[:, :3]) + 0.5 if self.Args.color_order == True: print('[ INFO ]: Coloring models with point order color.') Steps = np.linspace(0.0, 1.0, num=len(self.Models[-1])) Colors = ColorPalette.mpl_colormap(Steps) # print(self.Models[-1].Colors.shape) # print(Colors[:, :3].shape) self.Models[-1].Colors = np.asarray(Colors[:, :3]) # # TEMP # np.save('colors', self.Models[-1].Points) if self.Args.half_offset == True: self.Models[-1].Points += 0.5 self.Models[-1].update() self.isDrawNOCSCube = True self.isDrawPoints = False self.isDrawMesh = True self.isDrawBB = False self.isColorByOrder = False # self.RotateAngle = -90 # self.RotateAxis = np.array([1, 0, 0]) self.RotateAngle = 0 self.RotateAxis = np.array([1, 0, 0]) self.PointSize = 10.0 self.nModels = len(self.Args.models) self.activeModelIdx = self.nModels # nModels will show all
def loadData(self): Palette = ColorBlind_10 NMFiles = self.getFileNames(self.Args.nocs_maps) ColorFiles = [None] * len(NMFiles) PoseFiles = [None] * len(NMFiles) if self.Args.colors is not None: ColorFiles = self.getFileNames(self.Args.colors) if self.Args.poses is not None: PoseFiles = self.getFileNames(self.Args.poses) for (NMF, CF, PF) in zip(NMFiles, ColorFiles, PoseFiles): NOCSMap = cv2.imread(NMF, -1) NOCSMap = NOCSMap[:, :, :3] # Ignore alpha if present NOCSMap = cv2.cvtColor(NOCSMap, cv2.COLOR_BGR2RGB) # IMPORTANT: OpenCV loads as BGR, so convert to RGB if self.Intrinsics is None: self.ImageSize = (NOCSMap.shape[1], NOCSMap.shape[0]) else: NOCSMap = self.resizeAndPad(NOCSMap) CFIm = None if CF is not None: CFIm = cv2.imread(CF) CFIm = cv2.cvtColor(CFIm, cv2.COLOR_BGR2RGB) # IMPORTANT: OpenCV loads as BGR, so convert to RGB if CFIm.shape != NOCSMap.shape: # Re-size only if not the same size as NOCSMap CFIm = cv2.resize(CFIm, (NOCSMap.shape[1], NOCSMap.shape[0]), interpolation=cv2.INTER_CUBIC) # Ok to use cubic interpolation for RGB NOCS = ds.NOCSMap(NOCSMap, RGB=CFIm) self.NOCSMaps.append(NOCSMap) self.NOCS.append(NOCS) if self.Args.est_pose == True: _, K, R, C, Flip = self.estimateCameraPoseFromNM(NOCSMap, NOCS, N=self.Args.num_points, Intrinsics=self.Intrinsics) # The rotation and translation are about the NOCS origin self.CamIntrinsics.append(K) self.CamRots.append(R) self.CamPos.append(C) self.CamFlip.append(Flip) self.Cameras.append(ds.Camera(ds.CameraExtrinsics(self.CamRots[-1], self.CamPos[-1]), ds.CameraIntrinsics(self.CamIntrinsics[-1]))) if PF is not None: with open(PF) as f: data = json.load(f) # Loading convention: Flip sign of x postiion, flip signs of quaternion z, w P = np.array([data['position']['x'], data['position']['y'], data['position']['z']]) / self.Args.pose_scale Quat = np.array([data['rotation']['w'], data['rotation']['x'], data['rotation']['y'], data['rotation']['z']]) # NOTE: order is w, x, y, z # Cajole transforms to work P[0] *= -1 P += 0.5 Quat = np.array([Quat[0], Quat[1], -Quat[2], -Quat[3]]) self.PosesPos.append(P) R = quaternions.quat2mat(Quat).T self.PosesRots.append(R) else: self.PosesPos.append(None) self.PosesRots.append(None) self.nNM = len(NMFiles) self.activeNMIdx = self.nNM # len(NMFiles) will show all # Load OBJ models ModelFiles = self.getFileNames(self.Args.models) for MF in ModelFiles: self.OBJModels.append(obj_loader.Loader(MF, isNormalize=True))