|
6 | 6 | from io import BytesIO |
7 | 7 | from typing import Optional |
8 | 8 |
|
| 9 | +from PIL import Image |
| 10 | + |
9 | 11 | from simpletuner.helpers.data_backend.base import BaseDataBackend |
10 | 12 | from simpletuner.helpers.data_backend.dataset_types import DatasetType |
11 | 13 | from simpletuner.helpers.image_manipulation.load import load_image, load_video |
@@ -94,16 +96,58 @@ def __init__( |
94 | 96 | max_num_samples=max_num_samples, |
95 | 97 | ) |
96 | 98 |
|
97 | | - def _should_use_metadata_only_for_video(self) -> bool: |
98 | | - if not _is_ffprobe_available(): |
99 | | - return False |
100 | | - if self.dataset_type is not DatasetType.VIDEO: |
101 | | - return False |
| 99 | + def _should_use_metadata_only(self, is_video_file: bool) -> bool: |
| 100 | + """Whether this sample can be bucketed from dimensions alone. |
| 101 | +
|
| 102 | + Bucketing needs only the sample dimensions: calculate_target_size() and |
| 103 | + the crop coordinate maths derive everything from original_size, and no |
| 104 | + pixel-derived metadata is stored. Videos already take this path via |
| 105 | + ffprobe; still images can do the same from the file header. |
| 106 | +
|
| 107 | + Face cropping is the one crop style whose coordinates depend on pixel |
| 108 | + content, so it always requires a full decode. |
| 109 | + """ |
102 | 110 | crop_enabled = bool(self.dataset_config.get("crop", False)) |
103 | 111 | crop_style = str(self.dataset_config.get("crop_style") or "random").lower() |
104 | 112 | if crop_enabled and crop_style == "face": |
105 | 113 | return False |
106 | | - return True |
| 114 | + |
| 115 | + if is_video_file: |
| 116 | + return self.dataset_type is DatasetType.VIDEO and _is_ffprobe_available() |
| 117 | + if self.dataset_type in (DatasetType.IMAGE, DatasetType.CONDITIONING): |
| 118 | + # Header reads need a real path; remote backends stay on full reads. |
| 119 | + return getattr(self.data_backend, "type", None) == "local" |
| 120 | + return False |
| 121 | + |
| 122 | + def _probe_image_dimensions(self, image_path_str: str) -> Optional[dict]: |
| 123 | + """Read image dimensions from the file header, without decoding pixels. |
| 124 | +
|
| 125 | + PIL parses only the header on Image.open(); pixel data is loaded lazily |
| 126 | + and never requested here. Returns None to signal that the caller should |
| 127 | + fall back to a full decode. |
| 128 | +
|
| 129 | + EXIF orientations 5-8 transpose the image, so the stored dimensions are |
| 130 | + swapped relative to what exif_transpose() produces on the full-decode |
| 131 | + path. The orientation tag lives in the header too, so it is applied here |
| 132 | + to keep both paths in agreement. |
| 133 | + """ |
| 134 | + try: |
| 135 | + with Image.open(image_path_str) as image: |
| 136 | + width, height = image.size |
| 137 | + orientation = image.getexif().get(0x0112) |
| 138 | + if orientation in (5, 6, 7, 8): |
| 139 | + width, height = height, width |
| 140 | + except Exception as e: |
| 141 | + logger.debug( |
| 142 | + "(id=%s) Could not read image header for %s (%s); falling back to full decode.", |
| 143 | + self.id, |
| 144 | + image_path_str, |
| 145 | + e, |
| 146 | + ) |
| 147 | + return None |
| 148 | + if not width or not height: |
| 149 | + return None |
| 150 | + return {"original_size": (width, height)} |
107 | 151 |
|
108 | 152 | def _needs_video_frame_count(self) -> bool: |
109 | 153 | if self.bucket_strategy == "resolution_frames": |
@@ -382,7 +426,7 @@ def _process_for_bucket( |
382 | 426 | is_video_file = file_extension.strip(".") in video_file_extensions |
383 | 427 |
|
384 | 428 | use_metadata_only = False |
385 | | - if is_video_file and self._should_use_metadata_only_for_video(): |
| 429 | + if is_video_file and self._should_use_metadata_only(is_video_file): |
386 | 430 | if getattr(self.data_backend, "type", None) == "local": |
387 | 431 | video_metadata = self._probe_video_metadata(image_path_str, None) |
388 | 432 | else: |
@@ -429,6 +473,13 @@ def _process_for_bucket( |
429 | 473 |
|
430 | 474 | logger.debug("(id=%s) Using ffprobe metadata-only scan for %s", self.id, image_path_str) |
431 | 475 |
|
| 476 | + elif not is_video_file and self._should_use_metadata_only(is_video_file): |
| 477 | + image_dimensions = self._probe_image_dimensions(image_path_str) |
| 478 | + if image_dimensions: |
| 479 | + use_metadata_only = True |
| 480 | + image_metadata.update(image_dimensions) |
| 481 | + logger.debug("(id=%s) Using header-only metadata scan for %s", self.id, image_path_str) |
| 482 | + |
432 | 483 | if use_metadata_only: |
433 | 484 | if not self.meets_resolution_requirements(image_metadata=image_metadata): |
434 | 485 | if not self.delete_unwanted_images: |
|
0 commit comments