(source, *, max_images: Optional[int])
| 100 | #---------------------------------------------------------------------------- |
| 101 | |
| 102 | def open_image_zip(source, *, max_images: Optional[int]): |
| 103 | with zipfile.ZipFile(source, mode='r') as z: |
| 104 | input_images = [str(f) for f in sorted(z.namelist()) if is_image_ext(f)] |
| 105 | |
| 106 | # Load labels. |
| 107 | labels = {} |
| 108 | if 'dataset.json' in z.namelist(): |
| 109 | with z.open('dataset.json', 'r') as file: |
| 110 | labels = json.load(file)['labels'] |
| 111 | if labels is not None: |
| 112 | labels = { x[0]: x[1] for x in labels } |
| 113 | else: |
| 114 | labels = {} |
| 115 | |
| 116 | max_idx = maybe_min(len(input_images), max_images) |
| 117 | |
| 118 | def iterate_images(): |
| 119 | with zipfile.ZipFile(source, mode='r') as z: |
| 120 | for idx, fname in enumerate(input_images): |
| 121 | with z.open(fname, 'r') as file: |
| 122 | img = PIL.Image.open(file) # type: ignore |
| 123 | img = np.array(img) |
| 124 | yield dict(img=img, label=labels.get(fname)) |
| 125 | if idx >= max_idx-1: |
| 126 | break |
| 127 | return max_idx, iterate_images() |
| 128 | |
| 129 | #---------------------------------------------------------------------------- |
| 130 |
no test coverage detected