def build_multipuzzle_solver(image_filenames, puzzle_type, piece_width): """ Build the multipuzzle solver object. Args: image_filenames (List[str]): List of puzzle file paths. puzzle_type (PuzzleType): Type of the puzzle to solve. piece_width (int): Puzzle piece width in number of pixels Returns (MultiPuzzleSolver): The multipuzzle solver object built from the input image files. """ pieces, puzzles = Puzzle.get_combined_pieces_multiple_images(image_filenames, piece_width) pickle_filename = PickleHelper.build_filename("multipuzzle_distances", image_filenames, puzzle_type) # Initialize the distance information if _FORCE_RECALCULATE_DISTANCES or not os.path.exists(pickle_filename): multipuzzle_solver = MultiPuzzleSolver(image_filenames, pieces, top_level_calculate_asymmetric_distance, puzzle_type) if _POST_INITIAL_CONSTRUCTION_PICKLE_EXPORT: PickleHelper.exporter(multipuzzle_solver, pickle_filename) return multipuzzle_solver # Read the pickle information from the else: multipuzzle_solver = PickleHelper.importer(pickle_filename) multipuzzle_solver.reset_timestamp() return multipuzzle_solver
def _local_pickle_export_helper(self, pickle_file_descriptor): """ Helper function that handles the pickle export for a specific file description. Args: pickle_file_descriptor (str): File descriptor for the pickle file """ pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, self._image_filenames, self._paikin_tal_solver.puzzle_type) PickleHelper.exporter(self, pickle_filename)
def run_paikin_tal_solver(image_filenames, puzzle_type, piece_width): """ Paikin & Tal Solver This function takes a set of inputs and runs the Paikin and Tal solver. It can be sped-up by importing the calculations of distances from existing Pickle files. Args: image_filenames (List[String]): Path to the image files used to create the puzzles puzzle_type (PuzzleType): Type of the puzzle to be solved piece_width (int): Width/length of all puzzle pieces Returns (PaikinTalSolver): Solved Paikin & Tal result. """ pickle_filename = PickleHelper.build_filename( PaikinTalSolver.POST_INITIAL_CONSTRUCTOR_PICKLE_FILE_DESCRIPTOR, image_filenames, puzzle_type ) # Optionally import the images from disk if FORCE_RECALCULATE_DISTANCES or not os.path.exists(pickle_filename): combined_pieces, puzzles = Puzzle.get_combined_pieces_multiple_images(image_filenames, piece_width) # Select whether or not to use fixed puzzle dimensions puzzle_dimensions = puzzles[0].grid_size if USE_KNOWN_PUZZLE_DIMENSIONS and len(images) == 1 else None # Create the Paikin Tal Solver logging.info("Beginning calculating of Paikin & Tal inter-piece distance information") start_time = time.time() paikin_tal_solver = PaikinTalSolver( combined_pieces, top_level_calculate_asymmetric_distance, len(image_filenames), puzzle_type, fixed_puzzle_dimensions=puzzle_dimensions, image_filenames=image_filenames, ) print_elapsed_time(start_time, "Paikin & Tal inter-piece distance calculations") else: paikin_tal_solver = PaikinTalSolver.pickle_import_after_initial_construction(image_filenames, puzzle_type) if config.IS_SOLVER_COMPARISON_RUNNING: paikin_tal_solver.reset_actual_puzzle_count() # Run the Solver paikin_tal_solver.run_standard() # Export the solved results return paikin_tal_solver
def run_imported_stitching_piece_solving(image_filenames, puzzle_type): """ Debug method that is used to test the stitching piece solving. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type """ pickle_filename = PickleHelper.build_filename(MultiPuzzleSolver._POST_SEGMENTATION_COMPLETED_PICKLE_FILE_DESCRIPTOR, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) # noinspection PyProtectedMember solver.reset_timestamp() # noinspection PyProtectedMember solver._perform_stitching_piece_solving()
def run_imported_similarity_matrix_calculation(image_filenames, puzzle_type): """ Debug method that imports a pickle file for the specified image files, puzzle type, and segmentation round and then runs the initial segmentation starting after the specified round. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type """ pickle_file_descriptor = MultiPuzzleSolver._POST_STITCHING_PIECE_SOLVING_PICKLE_FILE_DESCRIPTOR pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) # noinspection PyProtectedMember solver.reset_timestamp() # noinspection PyProtectedMember solver._build_similarity_matrix()
def run_imported_select_starting_pieces(image_filenames, puzzle_type): """ Debug method that imports a pickle file after hierarchical clustering was completed. It then selects the seed pieces as inputs to the solver.. Note: The pickle file name is built from the image file names and the puzzle type. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type """ pickle_file_descriptor = MultiPuzzleSolver._POST_HIERARCHICAL_CLUSTERING_PICKLE_FILE_DESCRIPTOR pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) solver.reset_timestamp() # noinspection PyProtectedMember solver._select_starting_pieces_from_clusters()
def run_imported_hierarchical_clustering(image_filenames, puzzle_type): """ Debug method that imports a pickle file after the similarity matrix was built. It then runs hierarchical clustering on that imported pickle file. Note: The pickle file name is built from the image file names and the puzzle type. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type """ pickle_file_descriptor = MultiPuzzleSolver._POST_SIMILARITY_MATRIX_CALCULATION_PICKLE_FILE_DESCRIPTOR pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) solver.reset_timestamp() # noinspection PyProtectedMember solver._perform_hierarchical_clustering()
def run_imported_segmentation_experiment(image_filenames, puzzle_type, segmentation_round_numb): """ Debug method that imports a pickle file for the specified image files, puzzle type, and segmentation round and then runs the initial segmentation starting after the specified round. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type segmentation_round_numb (int): Segmentation round number """ file_descriptor = MultiPuzzleSolver._POST_SEGMENTATION_PUZZLE_PLACEMENT_FILE_DESCRIPTOR % segmentation_round_numb pickle_filename = PickleHelper.build_filename(file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) # noinspection PyProtectedMember solver.reset_timestamp() # noinspection PyProtectedMember solver._find_initial_segments(go_directly_to_segmentation=True)
def run_imported_segmentation_round(image_filenames, puzzle_type, segmentation_round_numb): """ Debug method that imports a pickle file for the specified image files, puzzle type, and segmentation round and then runs the initial segmentation starting after the specified round. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type segmentation_round_numb (int): Segmentation round number """ pickle_file_descriptor = "segment_round_%d" % segmentation_round_numb pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) # noinspection PyProtectedMember solver.reset_timestamp() # noinspection PyProtectedMember solver._find_initial_segments(skip_initialization=True)
def run_imported_final_puzzle_solving(image_filenames, puzzle_type): """ Debug method that imports a pickle file after hierarchical clustering was completed. It then selects the seed pieces as inputs to the solver.. Note: The pickle file name is built from the image file names and the puzzle type. Args: image_filenames (List[str]): List of paths to image file names puzzle_type (PuzzleType): Solver puzzle type """ pickle_file_descriptor = MultiPuzzleSolver._POST_SELECT_STARTING_PIECES_PICKLE_FILE_DESCRIPTOR pickle_filename = PickleHelper.build_filename(pickle_file_descriptor, image_filenames, puzzle_type) solver = PickleHelper.importer(pickle_filename) solver.reset_timestamp() # noinspection PyProtectedMember solver._perform_placement_with_final_seed_pieces() # noinspection PyProtectedMember solver._final_puzzles = solver._build_output_puzzles()
def run_best_buddies_analyzer(image_file, piece_width, puzzle_type): """ Runs the Best Buddy Analyzer on an image and outputs the best buddy results and generates a results image. Args: image_file (str): Path to an image file piece_width (int): Width of a puzzle piece in number of pixels puzzle_type (PuzzleType): Type of the puzzle to solve """ pickle_file = PickleHelper.build_filename("bb_accuracy", [image_file], puzzle_type) filename_with_image_folder = config.add_image_folder_path([image_file]) bb_results = ImageBestBuddyStatistics(filename_with_image_folder[0], piece_width, puzzle_type, top_level_calculate_asymmetric_distance) PickleHelper.exporter(bb_results, pickle_file) # Calculate the print the results. bb_results = PickleHelper.importer(pickle_file) bb_results.calculate_results() bb_results.print_results()