Skip to content

Commit 651ed9d

Browse files
authored
Merge pull request #1636 from patel-zeel/feat/expand-image-formats
Allow "tiff" and more extensions in `DetectionDataset.from_yolo` function
2 parents d4c6fd7 + 6aedf48 commit 651ed9d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

supervision/dataset/formats/yolo.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from pathlib import Path
33
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
44

5-
import cv2
65
import numpy as np
6+
from PIL import Image
77

88
from supervision.config import ORIENTED_BOX_COORDINATES
99
from supervision.dataset.utils import approximate_mask_with_polygons
@@ -153,7 +153,18 @@ def load_yolo_annotations(
153153
image_paths = [
154154
str(path)
155155
for path in list_files_with_extensions(
156-
directory=images_directory_path, extensions=["jpg", "jpeg", "png"]
156+
directory=images_directory_path,
157+
extensions=[
158+
"bmp",
159+
"dng",
160+
"jpg",
161+
"jpeg",
162+
"mpo",
163+
"png",
164+
"tif",
165+
"tiff",
166+
"webp",
167+
],
157168
)
158169
]
159170

@@ -167,10 +178,16 @@ def load_yolo_annotations(
167178
annotations[image_path] = Detections.empty()
168179
continue
169180

170-
image = cv2.imread(image_path)
181+
# PIL is much faster than cv2 for checking image shape and mode: https://github.com/roboflow/supervision/issues/1554
182+
image = Image.open(image_path)
171183
lines = read_txt_file(file_path=annotation_path, skip_empty=True)
172-
h, w, _ = image.shape
184+
w, h = image.size
173185
resolution_wh = (w, h)
186+
if image.mode not in ("RGB", "L"):
187+
raise ValueError(
188+
f"Images must be 'RGB' or 'grayscale', \
189+
but {image_path} mode is '{image.mode}'."
190+
)
174191

175192
with_masks = _with_mask(lines=lines)
176193
with_masks = force_masks if force_masks else with_masks

0 commit comments

Comments
 (0)