Beispiel #1
0
 def test_import_openmvg(self) -> None:
     """
     Test the import_openmvg function on a small JSON file while linking the images
     """
     self.assertTrue(path.isdir(self._openmvg_sample_path))
     self.assertTrue(path.exists(self._kapture_path), "Kapture directory exists")
     sfm_file = path.join(self._openmvg_sample_path, 'sfm_data_small.json')
     # on windows, without admin rights, fails with OSError: symbolic link privilege not held
     # see https://docs.python.org/3.6/library/os.html#os.symlink
     logger.info(f'Running on "{sys.platform}" which is {"" if self.isWindows else "not"} a Windows platform')
     file_operation = TransferAction.skip if self.isWindows else TransferAction.link_relative
     import_openmvg(sfm_file, self._kapture_path, file_operation, True)
     #  test presence or absence of kapture files
     cameras_file_path = path.join(self._kapture_path, kcsv.CSV_FILENAMES[kapture.Sensors])
     self.assertTrue(path.isfile(cameras_file_path), "Camera file written")
     rigs_file_path = path.join(self._kapture_path, kcsv.CSV_FILENAMES[kapture.Rigs])
     self.assertFalse(path.isfile(rigs_file_path), "Rigs file should be missing")
     records_file_path = path.join(self._kapture_path, kcsv.CSV_FILENAMES[kapture.RecordsCamera])
     self.assertTrue(path.isfile(records_file_path), "Camera Records file written")
     lidars_file_path = path.join(self._kapture_path, kcsv.CSV_FILENAMES[kapture.RecordsLidar])
     self.assertFalse(path.isfile(lidars_file_path), "Lidar Records file should be missing")
     trajectories_file_path = path.join(self._kapture_path, kcsv.CSV_FILENAMES[kapture.Trajectories])
     self.assertTrue(path.isfile(trajectories_file_path), "Trajectories file written")
     # Reload data and verify
     kapture_data = kcsv.kapture_from_dir(self._kapture_path)
     self._verify_data(kapture_data)
     if not self.isWindows:
         # Test images path
         all_records_camera = list(kapture.flatten(kapture_data.records_camera))
         for _, _, name in all_records_camera:
             img_path = get_image_fullpath(self._kapture_path, name)
             self.assertTrue(path.islink(img_path), f"image link {img_path}")
def import_openmvg_command_line() -> None:
    """
    Do the openmvg to kapture import using the parameters given on the command line.
    """
    parser = argparse.ArgumentParser(
        description='Imports from openMVG JSON file to Kapture format.')
    parser_verbosity = parser.add_mutually_exclusive_group()
    parser_verbosity.add_argument(
        '-v',
        '--verbose',
        nargs='?',
        default=logging.WARNING,
        const=logging.INFO,
        action=kapture.utils.logging.VerbosityParser,
        help=
        'verbosity level (debug, info, warning, critical, ... or int value) [warning]'
    )
    parser_verbosity.add_argument('-q',
                                  '--silent',
                                  '--quiet',
                                  action='store_const',
                                  dest='verbose',
                                  const=logging.CRITICAL)
    parser.add_argument('-y',
                        '--force',
                        action='store_true',
                        default=False,
                        help='silently delete kapture data if already exists.')
    # create the parser for the import command #########################################################################
    parser.add_argument('-o',
                        '--openmvg',
                        required=True,
                        help='path to openMVG JSON file.')
    parser.add_argument('-k',
                        '--kapture',
                        required=True,
                        help='top directory where to save Kapture files.')
    parser.add_argument('--image_action',
                        default='root_link',
                        type=TransferAction,
                        help=f'''what to do with images:
        {TransferAction.root_link.name}: link to the root of the images directory (default) ;
        {TransferAction.link_absolute.name}: absolute individual file link ;
        {TransferAction.link_relative.name}: relative individual file link ;
        {TransferAction.copy.name}: copy file instead of creating link ;
        {TransferAction.move.name}: move file instead of creating link ;
        {TransferAction.skip.name}: do not create links
                                      ''')
    ####################################################################################################################
    args = parser.parse_args()

    logger.setLevel(args.verbose)
    if args.verbose <= logging.DEBUG:
        # for debug, let kapture express itself.
        kapture.utils.logging.getLogger().setLevel(args.verbose)
    logger.debug(f'{sys.argv[0]} \\\n' +
                 '  \\\n'.join('--{:20} {:100}'.format(k, str(v))
                               for k, v in vars(args).items()))

    # Check that we will not try to do symbolics links on Windows
    # because this is not possible today if the user is not admin
    if sys.platform.startswith("win") and \
            (args.image_link == TransferAction.link_relative or
             args.image_link == TransferAction.link_absolute or
             args.image_link == TransferAction.root_link):
        logger.fatal(
            "It is currently impossible to link files on a Windows platform")
        logger.fatal(
            f"Please try another option for the image: either do nothing ({TransferAction.skip}),"
            f" or {TransferAction.copy}")
        raise OSError("Image file linking not possible on Windows")
    else:
        import_openmvg(args.openmvg, args.kapture, args.image_action,
                       args.force)