コード例 #1
0
ファイル: visualizeNOCSMap.py プロジェクト: wx-b/tk3dv
    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('--nocs-maps', nargs='+', help='Specify input NOCS maps. * globbing is supported.', required=True)
        ArgGroup.add_argument('--colors', nargs='+', help='Specify RGB images corresponding to the input NOCS maps. * globbing is supported.', required=False)
        ArgGroup.add_argument('--intrinsics', help='Specify the intrinsics file to estimate camera pose.', required=False, default=None)
        ArgGroup.add_argument('--poses', nargs='+',
                              help='Specify the camera extrinsics corresponding to the input NOCS maps. * globbing is supported.',
                              required=False)
        ArgGroup.add_argument('--models', nargs='+',
                              help='Specify OBJ models to load additionally. * globbing is supported.',
                              required=False)
        ArgGroup.add_argument('--num-points', help='Specify the number of pixels to use for camera pose registration.', default=1000, type=int, required=False)
        ArgGroup.add_argument('--error-viz', help='Specify error wrto Nth NOCS map. If multiple NOCS maps are provided. Will compute the L2 errors between the Nth NOCS map and the rest. Will render this instead of RGB or colors.', default=-1, type=int, required=False)

        ArgGroup.add_argument('--est-pose', help='Choose to estimate pose.', action='store_true')
        self.Parser.set_defaults(est_pose=False)
        ArgGroup.add_argument('--pose-scale', help='Specify the (inverse) scale of the camera positions in the ground truth pose files.', default=1.0, type=float, required=False)

        self.Args, _ = self.Parser.parse_known_args(InputArgs)
        if len(sys.argv) <= 1:
            self.Parser.print_help()
            exit()

        self.NOCSMaps = []
        self.NOCS = []
        self.Cameras = []
        self.CamRots = []
        self.CamPos = []
        self.CamIntrinsics = []
        self.CamFlip = [] # This is a hack
        # Extrinsics, if provided
        self.PosesRots = []
        self.PosesPos = []
        self.OBJModels = []
        self.PointSize = 3
        self.Intrinsics = None
        if self.Args.intrinsics is not None:
            self.Intrinsics = ds.CameraIntrinsics()
            self.Intrinsics.init_with_file(self.Args.intrinsics)
            self.ImageSize = (self.Intrinsics.Width, self.Intrinsics.Height)
            print('[ INFO ]: Intrinsics provided. Will re-size all images to', self.ImageSize)
        if self.Intrinsics is None:
            self.ImageSize = None
            print('[ INFO ]: No intrinsics provided. Will use NOCS map size.')

        sys.stdout.flush()
        self.nNM = 0
        self.SSCtr = 0
        self.takeSS = False
        self.showNOCS = True
        self.showBB = False
        self.showPoints = False
        self.showWireFrame = False
        self.isVizError = False
        self.showOBJModels = True
        self.loadData()
        self.generateDiffMap()
コード例 #2
0
    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, Color, CF, PF) in zip(NMFiles, Palette.colors, 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.no_pose == False:
                _, 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']])
                        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