Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow "tiff" and more extensions in DetectionDataset.from_yolo function #1636

Merged
merged 7 commits into from
Jan 8, 2025
25 changes: 21 additions & 4 deletions supervision/dataset/formats/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

import cv2
import numpy as np
from PIL import Image

from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.dataset.utils import approximate_mask_with_polygons
Expand Down Expand Up @@ -153,7 +153,18 @@ def load_yolo_annotations(
image_paths = [
str(path)
for path in list_files_with_extensions(
directory=images_directory_path, extensions=["jpg", "jpeg", "png"]
directory=images_directory_path,
extensions=[
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patel-zeel small request: don't break file extensions onto separate lines; list them all on one line if possible

Copy link
Contributor Author

@patel-zeel patel-zeel Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept them in a single line @SkalskiP. Pre-commit (specifically ruff-format) forced them on separate lines.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eeeh that's what I thought so

"bmp",
"dng",
"jpg",
"jpeg",
"mpo",
"png",
"tif",
"tiff",
"webp",
],
)
]

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

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

with_masks = _with_mask(lines=lines)
with_masks = force_masks if force_masks else with_masks
Expand Down
Loading