A class to extract images information, including caption, size, and appearance times in a presentation.
| 9 | |
| 10 | |
| 11 | class ImageLabler: |
| 12 | """ |
| 13 | A class to extract images information, including caption, size, and appearance times in a presentation. |
| 14 | """ |
| 15 | |
| 16 | def __init__(self, presentation: Presentation, config: Config): |
| 17 | """ |
| 18 | Initialize the ImageLabler. |
| 19 | |
| 20 | Args: |
| 21 | presentation (Presentation): The presentation object. |
| 22 | config (Config): The configuration object. |
| 23 | """ |
| 24 | self.presentation = presentation |
| 25 | self.slide_area = presentation.slide_width.pt * presentation.slide_height.pt |
| 26 | self.image_stats = {} |
| 27 | self.stats_file = pjoin(config.RUN_DIR, "image_stats.json") |
| 28 | self.config = config |
| 29 | self.collect_images() |
| 30 | if pexists(self.stats_file): |
| 31 | image_stats: dict[str, dict] = json.load(open(self.stats_file, "r")) |
| 32 | for name, stat in image_stats.items(): |
| 33 | if pbasename(name) in self.image_stats: |
| 34 | self.image_stats[pbasename(name)] = stat |
| 35 | |
| 36 | def apply_stats(self): |
| 37 | """ |
| 38 | Apply image captions to the presentation. |
| 39 | """ |
| 40 | for slide in self.presentation.slides: |
| 41 | for shape in slide.shape_filter(Picture): |
| 42 | stats = self.image_stats[pbasename(shape.img_path)] |
| 43 | shape.caption = stats["caption"] |
| 44 | |
| 45 | def caption_images(self): |
| 46 | """ |
| 47 | Generate captions for images in the presentation. |
| 48 | """ |
| 49 | caption_prompt = open("prompts/caption.txt").read() |
| 50 | for image, stats in self.image_stats.items(): |
| 51 | if "caption" not in stats: |
| 52 | stats["caption"] = llms.vision_model( |
| 53 | caption_prompt, pjoin(self.config.IMAGE_DIR, image) |
| 54 | ) |
| 55 | print("captioned", image, ": ", stats["caption"]) |
| 56 | json.dump( |
| 57 | self.image_stats, |
| 58 | open(self.stats_file, "w"), |
| 59 | indent=4, |
| 60 | ensure_ascii=False, |
| 61 | ) |
| 62 | self.apply_stats() |
| 63 | return self.image_stats |
| 64 | |
| 65 | def collect_images(self): |
| 66 | """ |
| 67 | Collect images from the presentation and gather other information. |
| 68 | """ |
no outgoing calls
no test coverage detected