示例#1
0
def main():
    """Run the masking."""
    # Initialize
    start_datetime = datetime.now()
    args, tree_walker, image_processor, dataset_iterator = initialize()
    n_imgs = "?" if config.lazy_paths else (tree_walker.n_valid_images +
                                            tree_walker.n_skipped_images)

    # Mask images
    time_at_iter_start = time.time()
    for i, paths in enumerate(tree_walker.walk()):
        count_str = f"{tree_walker.n_skipped_images + i + 1} of {n_imgs}"
        start_time = time.time()
        LOGGER.set_state(paths)
        LOGGER.info(__name__, LOG_SEP)
        LOGGER.info(__name__, f"Iteration: {count_str}.")

        # Catch potential exceptions raised while processing the image
        try:
            # Get the image
            img = next(dataset_iterator)
            # Do the processing
            image_processor.process_image(img, paths)
        except PROCESSING_EXCEPTIONS as err:
            error_msg = f"'{str(err)}'. File: {paths.input_file}"
            LOGGER.error(__name__,
                         error_msg,
                         save=True,
                         email=True,
                         email_mode="error")
            continue

        est_done = get_estimated_done(time_at_iter_start, n_imgs, i + 1)
        iter_time_delta = "{:.3f}".format(time.time() - start_time)
        LOGGER.info(__name__, f"Iteration finished in {iter_time_delta} s.")
        LOGGER.info(__name__, f"Estimated completion: {est_done}")

    # Close the image_processor. This will make sure that all exports are finished before we continue.
    LOGGER.info(__name__, LOG_SEP)
    LOGGER.info(__name__, f"Writing output files for the remaining images.")
    image_processor.close()

    # Summary
    summary_str = get_summary(tree_walker, image_processor, start_datetime)
    LOGGER.info(__name__, LOG_SEP)
    LOGGER.info(__name__, summary_str, email=True, email_mode="finished")
示例#2
0
def _handle_missing_files(paths, missing_files):
    """
    Handle any missing files identified for a given image. This will log an error, which saves the error image, and
    sends an error-email, if email sending is enabled.

    :param paths: Paths object representing the input image
    :type paths: src.io.TreeWalker.Paths
    :param missing_files: List of missing files
    :type missing_files: list of str
    """
    current_logger_state = LOGGER.get_state()
    LOGGER.set_state(paths)
    LOGGER.error(
        __name__,
        f"Missing output files {missing_files} for image: {paths.input_file}",
        save=True,
        email=True,
        email_mode="error")
    LOGGER.set_state(current_logger_state)
示例#3
0
    def handle_error(self, err):
        """
        Handle an exception raised by an async worker.

        :param err: Exception raised by the worker
        :type err: BaseException
        """
        # Get the current state of the logger
        current_logger_state = LOGGER.get_state()

        # Set the state of the logger to reflect the failed image.
        LOGGER.set_state(self.paths)
        # Log the error
        LOGGER.error(__name__,
                     self.error_message.format(
                         image_path=self.paths.input_file, err=str(err)),
                     save=False,
                     email=False)
        # Reset the state
        LOGGER.set_state(current_logger_state)