(source_dir, *, max_images: Optional[int])
| 67 | #---------------------------------------------------------------------------- |
| 68 | |
| 69 | def open_image_folder(source_dir, *, max_images: Optional[int]): |
| 70 | input_images = [str(f) for f in sorted(Path(source_dir).rglob('*')) if is_image_ext(f) and os.path.isfile(f)] |
| 71 | |
| 72 | # Load labels. |
| 73 | labels = {} |
| 74 | meta_fname = os.path.join(source_dir, 'dataset.json') |
| 75 | if os.path.isfile(meta_fname): |
| 76 | with open(meta_fname, 'r') as file: |
| 77 | labels = json.load(file)['labels'] |
| 78 | if labels is not None: |
| 79 | labels = { x[0]: x[1] for x in labels } |
| 80 | else: |
| 81 | labels = {} |
| 82 | print("original labels:", len(labels)) |
| 83 | |
| 84 | max_idx = maybe_min(len(input_images), max_images) |
| 85 | |
| 86 | def iterate_images(): |
| 87 | for idx, fname in enumerate(input_images): |
| 88 | arch_fname = os.path.relpath(fname, source_dir) |
| 89 | arch_fname = arch_fname.replace('\\', '/') |
| 90 | arch_fname = os.path.basename(arch_fname) # basename as identifier |
| 91 | img = np.array(PIL.Image.open(fname)) |
| 92 | if len(labels) > 0 and not arch_fname in labels: |
| 93 | print("Label nout found:", arch_fname, labels.get(arch_fname)) |
| 94 | continue # Ignore images without label |
| 95 | yield dict(img=img, label=labels.get(arch_fname)) |
| 96 | if idx >= max_idx-1: |
| 97 | break |
| 98 | return max_idx, iterate_images() |
| 99 | |
| 100 | #---------------------------------------------------------------------------- |
| 101 |
no test coverage detected