Exemple #1
0
def adjustment_sample(filepath):
    """A sample function that illustrates how to use adjusters.

    :param filepath: The absolute filepath of an input image.
    :type filepath: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath), adjusters=[STRONG_DARK_ADJUSTER])
    return evolver.evolve()
Exemple #2
0
def draw_shape_sample(filepath):
    """A sample function that illustrates how to use custom shape drawing methods.

    :param filepath: The absolute filepath of an input image.
    :type filepath: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath), draw=add_square)
    return evolver.evolve()
Exemple #3
0
def standard_sample(filepath):
    """A standard sample function that illustrates how to use the module.

    :param filepath: The absolute filepath of an input image.
    :type filepath: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath))
    return evolver.evolve()
Exemple #4
0
def error_metric_sample(filepath):
    """A sample function that illustrates how to use custom error metrics.

    :param filepath: The absolute filepath of an input image.
    :type filepath: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath),
                      calculate_error=structural_similarity_error)
    return evolver.evolve()
Exemple #5
0
def load_genome_sample(filepath_source, filepath_genome):
    """A sample function that illustrates how to load previously saved genomes.

    :param filepath_source: The absolute filepath of an input image.
    :type filepath_source: str
    :param filepath_genome: The filepath to load the pre-evolved genome.
    :type filepath_genome: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath_source),
                      saved_genome=load_genome(filepath_genome))
    return evolver.evolve()
Exemple #6
0
def save_genome_sample(filepath_source, filepath_to_save_to):
    """A sample function that illustrates how to save genomes.

    :param filepath_source: The absolute filepath of an input image.
    :type filepath_source: str
    :param filepath_to_save_to: The filepath to save the genome.
    :type filepath_to_save_to: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath_source))
    genome = evolver.evolve()
    genome.save_genome(filepath_to_save_to)
    return genome
Exemple #7
0
def preprocess_sample(filepath):
    """A sample function that illustrates how to use preprocesses.

    :param filepath: The absolute filepath of an input image.
    :type filepath: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath),
                      preprocesses=[
                          saturate_preprocess, contrast_preprocess,
                          smooth_preprocess
                      ])
    return evolver.evolve()
Exemple #8
0
def callback_sample(filepath_source, filepath_csv):
    """A sample function that illustrates how to add callbacks to the evolve method.

    :param filepath_source: The absolute filepath of an input image.
    :type filepath_source: str
    :param filepath_csv: The filepath to save CSV statistics to.
    :type filepath_csv: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath_source))
    logger = callbacks.CSVLogger(filepath_csv)
    return evolver.evolve(
        callbacks=[callbacks.default_callback, logger.callback])
Exemple #9
0
def save_image_sample(filepath_source, filepath_to_save_to):
    """A sample function that illustrates how to save images from genomes.

    :param filepath_source: The absolute filepath of an input image.
    :type filepath_source: str
    :param filepath_to_save_to: The filepath to save the evolved image to.
    :type filepath_to_save_to: str
    :return: The evolved genome.
    :rtype: Genome
    """
    evolver = Evolver(get_image(filepath_source))
    genome = evolver.evolve()
    image = genome.render_scaled_image()
    cv2.imwrite(filepath_to_save_to, image)
    return genome
Exemple #10
0
def main():
    """Run the application."""
    parser = ArgumentParser(
        description=f"Circlevolve CLI, version {__version__}")
    parser.add_argument("image",
                        type=str,
                        help="Path to base image for evolution")
    parser.add_argument("-square",
                        dest="square",
                        action='store_true',
                        help="Runs with squares instead of circles")
    parser.add_argument("-debug",
                        dest="debug",
                        action='store_true',
                        help="Print statistics in console")
    parser.add_argument(
        "-ssim",
        dest="ssim",
        action='store_true',
        help="Use structural-similarity instead of mean-squared-error")
    parser.add_argument("-dark",
                        dest="dark",
                        action='store_true',
                        help="Optimize the algorithm for darker images")

    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "-silent",
        dest="output",
        action='store_const',
        const=2,
        help="Disables viewing updates and statistics during evolution")
    group.add_argument("-quiet",
                       dest="output",
                       action='store_const',
                       const=1,
                       help="Updates more rarely during evolution")

    parser.add_argument("--saved-genome",
                        default=None,
                        type=str,
                        help="Path to saved genome for continued evolution")
    parser.add_argument("--num-shapes",
                        default=1000,
                        type=int,
                        help="Number of shapes to draw with")
    parser.add_argument("--num-generations",
                        default=5000,
                        type=int,
                        help="Number of generations to train")
    args = parser.parse_args()

    if args.saved_genome is not None:
        saved_genome = load_genome(args.saved_genome)
    else:
        saved_genome = None

    image_path = args.image + "_result.png"
    genome_path = args.image + "_genome.pkl"

    if args.square:
        draw = drawers.add_square
    else:
        draw = drawers.add_circle

    if args.ssim:
        error = error_metrics.structural_similarity_error
    else:
        error = error_metrics.mean_squared_error

    if args.dark:
        adjustments = [adjusters.STRONG_DARK_ADJUSTER]
    else:
        adjustments = []

    if args.output == 2:
        silent = True
    else:
        silent = False

    if args.output == 1:
        callback_list = [callbacks.quiet_visual_callback]
        if args.debug:
            callback_list.append(callbacks.quiet_verbose_callback)
    else:
        callback_list = [callbacks.visual_callback]
        if args.debug:
            callback_list.append(callbacks.verbose_callback)

    evolver = Evolver(get_image(args.image),
                      saved_genome=saved_genome,
                      num_shapes=args.num_shapes,
                      draw=draw,
                      calculate_error=error,
                      adjusters=adjustments)
    print("evolve!")
    genome = evolver.evolve(num_generations=args.num_generations,
                            silent=silent,
                            callbacks=callback_list)
    image = genome.render_scaled_image()

    imwrite(image_path, image)
    genome.save_genome(genome_path)
    print(f"Image saved at {image_path}\nGenome saved at {genome_path}")

    show()  # Keep matplotlib window open.