Skip to content

Commit fa9eff0

Browse files
committed
fix: close Image.open with context manager and clean up ValueError message in YOLO loader
1 parent 42b0bc3 commit fa9eff0

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/supervision/dataset/formats/yolo.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,15 @@ def load_yolo_annotations(
228228
continue
229229

230230
# PIL is much faster than cv2 for checking image shape and mode: https://github.com/roboflow/supervision/issues/1554
231-
image = Image.open(image_path)
231+
with Image.open(image_path) as image:
232+
w, h = image.size
233+
mode = image.mode
232234
lines = read_txt_file(file_path=annotation_path, skip_empty=True)
233-
w, h = image.size
234235
resolution_wh = (w, h)
235-
if image.mode not in ("RGB", "L"):
236+
if mode not in ("RGB", "L"):
236237
raise ValueError(
237-
f"Images must be 'RGB' or 'grayscale', \
238-
but {image_path} mode is '{image.mode}'."
238+
f"Images must be 'RGB' or 'grayscale',"
239+
f" but {image_path} mode is '{mode}'."
239240
)
240241

241242
with_masks = force_masks or _with_seg_mask(lines=lines)

tests/dataset/test_show_progress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ def test_load_coco_show_progress(coco_dataset: dict, show_progress: bool) -> Non
166166

167167

168168
def test_load_coco_show_progress_consistent(coco_dataset: dict) -> None:
169-
classes_off, paths_off, ann_off = load_coco_annotations(
169+
classes_off, paths_off, _ann_off = load_coco_annotations(
170170
images_directory_path=coco_dataset["images_dir"],
171171
annotations_path=coco_dataset["annotations_path"],
172172
show_progress=False,
173173
)
174-
classes_on, paths_on, ann_on = load_coco_annotations(
174+
classes_on, paths_on, _ann_on = load_coco_annotations(
175175
images_directory_path=coco_dataset["images_dir"],
176176
annotations_path=coco_dataset["annotations_path"],
177177
show_progress=True,

0 commit comments

Comments
 (0)