MCPcopy
hub / github.com/SizheAn/PanoHead / open_image_zip

Function open_image_zip

dataset_tool.py:102–127  ·  view source on GitHub ↗
(source, *, max_images: Optional[int])

Source from the content-addressed store, hash-verified

100#----------------------------------------------------------------------------
101
102def 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

Callers 1

open_datasetFunction · 0.85

Calls 4

loadMethod · 0.80
is_image_extFunction · 0.70
maybe_minFunction · 0.70
iterate_imagesFunction · 0.70

Tested by

no test coverage detected