|
| 1 | +"""Handle similar segments.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from PIL import Image |
| 6 | +from loguru import logger |
| 7 | + |
| 8 | +from openadapt import adapters, cache, config, plotting, utils, vision |
| 9 | + |
| 10 | + |
| 11 | +DEBUG = True |
| 12 | +MIN_SEGMENT_SSIM = 0.95 # threshold for considering segments structurally similar |
| 13 | +MIN_SEGMENT_SIZE_SIM = 0.95 # threshold for considering segment sizes similar |
| 14 | + |
| 15 | + |
| 16 | +# TODO: consolidate with strategies.visual.get_window_segmentation |
| 17 | +@cache.cache(enabled=not DEBUG) |
| 18 | +def get_similar_segment_groups( |
| 19 | + image_file_path: str, |
| 20 | + min_segment_ssim: float = MIN_SEGMENT_SSIM, |
| 21 | + min_segment_size_sim: float = MIN_SEGMENT_SIZE_SIM, |
| 22 | + show_images: bool = DEBUG, |
| 23 | + contrast_factor: int = 10000, |
| 24 | +) -> tuple: |
| 25 | + """Get similar segment groups.""" |
| 26 | + image = Image.open(image_file_path) |
| 27 | + image.show() |
| 28 | + |
| 29 | + if contrast_factor: |
| 30 | + image = utils.increase_contrast(image, contrast_factor) |
| 31 | + image.show() |
| 32 | + |
| 33 | + segmentation_adapter = adapters.get_default_segmentation_adapter() |
| 34 | + segmented_image = segmentation_adapter.fetch_segmented_image(image) |
| 35 | + if show_images: |
| 36 | + segmented_image.show() |
| 37 | + |
| 38 | + import ipdb |
| 39 | + |
| 40 | + ipdb.set_trace() |
| 41 | + |
| 42 | + masks = vision.get_masks_from_segmented_image(segmented_image) |
| 43 | + logger.info(f"{len(masks)=}") |
| 44 | + if show_images: |
| 45 | + plotting.display_binary_images_grid(masks) |
| 46 | + |
| 47 | + refined_masks = vision.refine_masks(masks) |
| 48 | + logger.info(f"{len(refined_masks)=}") |
| 49 | + if show_images: |
| 50 | + plotting.display_binary_images_grid(refined_masks) |
| 51 | + |
| 52 | + masked_images = vision.extract_masked_images(image, refined_masks) |
| 53 | + descriptions = ["" for _ in masked_images] |
| 54 | + if show_images: |
| 55 | + plotting.display_images_table_with_titles(masked_images, descriptions) |
| 56 | + |
| 57 | + similar_idx_groups, ungrouped_idxs, ssim_matrix, _ = vision.get_similar_image_idxs( |
| 58 | + masked_images, |
| 59 | + min_segment_ssim, |
| 60 | + min_segment_size_sim, |
| 61 | + ) |
| 62 | + logger.info(f"{len(similar_idx_groups)=}") |
| 63 | + |
| 64 | + return ( |
| 65 | + image, |
| 66 | + masked_images, |
| 67 | + refined_masks, |
| 68 | + similar_idx_groups, |
| 69 | + ungrouped_idxs, |
| 70 | + ssim_matrix, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def main() -> None: |
| 75 | + """Main.""" |
| 76 | + image_file_path = os.path.join(config.ROOT_DIR_PATH, "../tests/assets/excel.png") |
| 77 | + |
| 78 | + MAX_GROUPS = 2 |
| 79 | + |
| 80 | + for min_segment_ssim in (MIN_SEGMENT_SSIM, MIN_SEGMENT_SSIM // 3): |
| 81 | + logger.info(f"{min_segment_ssim=}") |
| 82 | + image, masked_images, masks, similar_idx_groups, ungrouped_idxs, ssim_matrix = ( |
| 83 | + get_similar_segment_groups(image_file_path) |
| 84 | + ) |
| 85 | + similar_idx_groups = sorted( |
| 86 | + similar_idx_groups, |
| 87 | + key=lambda group: len(group), |
| 88 | + reverse=True, |
| 89 | + ) |
| 90 | + if MAX_GROUPS: |
| 91 | + similar_idx_groups = similar_idx_groups[:MAX_GROUPS] |
| 92 | + plotting.plot_similar_image_groups( |
| 93 | + masked_images, |
| 94 | + similar_idx_groups, |
| 95 | + ssim_matrix, |
| 96 | + [ |
| 97 | + f"min_ssim={MIN_SEGMENT_SSIM}", |
| 98 | + f"min_size_sim={MIN_SEGMENT_SIZE_SIM}", |
| 99 | + ], |
| 100 | + ) |
| 101 | + |
| 102 | + """ |
| 103 | + - images: |
| 104 | + - original |
| 105 | + - one segment mask |
| 106 | + - multiple segment masks |
| 107 | + - original with one segment highlighted |
| 108 | + - original with multiple segments highlighted |
| 109 | + - original with one segment labelled |
| 110 | + - original with multiple segments labelled |
| 111 | + - original with one segment highlighted+labelled |
| 112 | + - original with multiple segments highlighted+labelled |
| 113 | + - individual segment |
| 114 | + - individual segment labelled |
| 115 | + - one or multiple segments per prompt |
| 116 | + """ |
| 117 | + for similar_idx_group in similar_idx_groups: |
| 118 | + similar_masks = [masks[idx] for idx in similar_idx_group] |
| 119 | + highlighted_image = plotting.highlight_masks(image, similar_masks) |
| 120 | + highlighted_image.show() |
| 121 | + |
| 122 | + import ipdb |
| 123 | + |
| 124 | + ipdb.set_trace() |
| 125 | + foo = 1 # noqa |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments