Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions supervision/dataset/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ def as_pascal_voc(
in the range [0, 1). Argument is used only for segmentation datasets.
"""
if images_directory_path:
images_path = Path(images_directory_path)
images_path.mkdir(parents=True, exist_ok=True)

save_dataset_images(
images_directory_path=images_directory_path, images=self.images
)
if annotations_directory_path:
annotations_path = Path(annotations_directory_path)
annotations_path.mkdir(parents=True, exist_ok=True)
Path(annotations_directory_path).mkdir(parents=True, exist_ok=True)

for image_name, image in self.images.items():
detections = self.annotations[image_name]

if images_directory_path:
cv2.imwrite(str(images_path / image_name), image)
for image_path, image in self.images.items():
detections = self.annotations[image_path]

if annotations_directory_path:
annotation_name = Path(image_name).stem
annotation_name = Path(image_path).stem
annotations_path = os.path.join(
annotations_directory_path, f"{annotation_name}.xml"
)
image_name = Path(image_path).name
pascal_voc_xml = detections_to_pascal_voc(
detections=detections,
classes=self.classes,
Expand All @@ -206,7 +206,7 @@ def as_pascal_voc(
approximation_percentage=approximation_percentage,
)

with open(annotations_path / f"{annotation_name}.xml", "w") as f:
with open(annotations_path, "w") as f:
f.write(pascal_voc_xml)

@classmethod
Expand Down Expand Up @@ -615,9 +615,10 @@ def as_folder_structure(self, root_directory_path: str) -> None:
for class_name in self.classes:
os.makedirs(os.path.join(root_directory_path, class_name), exist_ok=True)

for image_name in self.images:
classification = self.annotations[image_name]
image = self.images[image_name]
for image_path in self.images:
classification = self.annotations[image_path]
image = self.images[image_path]
image_name = Path(image_path).name
class_id = (
classification.class_id[0]
if classification.confidence is None
Expand Down Expand Up @@ -666,9 +667,9 @@ def from_folder_structure(cls, root_directory_path: str) -> ClassificationDatase
class_id = classes.index(class_name)

for image in os.listdir(os.path.join(root_directory_path, class_name)):
image_dir = os.path.join(root_directory_path, class_name, image)
images[image] = cv2.imread(image_dir)
annotations[image] = Classifications(
image_path = str(os.path.join(root_directory_path, class_name, image))
images[image_path] = cv2.imread(image_path)
annotations[image_path] = Classifications(
class_id=np.array([class_id]),
)

Expand Down
12 changes: 6 additions & 6 deletions supervision/dataset/formats/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def load_coco_annotations(
image_annotations = coco_annotations_groups.get(coco_image["id"], [])
image_path = os.path.join(images_directory_path, image_name)

image = cv2.imread(str(image_path))
image = cv2.imread(image_path)
annotation = coco_annotations_to_detections(
image_annotations=image_annotations,
resolution_wh=(image_width, image_height),
Expand All @@ -170,8 +170,8 @@ def load_coco_annotations(
detections=annotation,
)

images[image_name] = image
annotations[image_name] = annotation
images[image_path] = image
annotations[image_path] = annotation

return classes, images, annotations

Expand Down Expand Up @@ -200,9 +200,9 @@ def save_coco_annotations(
coco_categories = classes_to_coco_categories(classes=classes)

image_id, annotation_id = 1, 1
for image_name, image in images.items():
for image_path, image in images.items():
image_height, image_width, _ = image.shape

image_name = f"{Path(image_path).stem}{Path(image_path).suffix}"
coco_image = {
"id": image_id,
"license": 1,
Expand All @@ -213,7 +213,7 @@ def save_coco_annotations(
}

coco_images.append(coco_image)
detections = annotations[image_name]
detections = annotations[image_path]

coco_annotation, annotation_id = detections_to_coco_annotations(
detections=detections,
Expand Down
11 changes: 6 additions & 5 deletions supervision/dataset/formats/pascal_voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ def load_pascal_voc_annotations(

for image_path in image_paths:
image_name = Path(image_path).stem
image = cv2.imread(str(image_path))
image_path = str(image_path)
image = cv2.imread(image_path)

annotation_path = os.path.join(annotations_directory_path, f"{image_name}.xml")
if not os.path.exists(annotation_path):
images[image_path.name] = image
annotations[image_path.name] = Detections.empty()
images[image_path] = image
annotations[image_path] = Detections.empty()
continue

tree = parse(annotation_path)
Expand All @@ -184,8 +185,8 @@ def load_pascal_voc_annotations(
root, classes, resolution_wh, force_masks
)

images[image_path.name] = image
annotations[image_path.name] = annotation
images[image_path] = image
annotations[image_path] = annotation

return classes, images, annotations

Expand Down
20 changes: 11 additions & 9 deletions supervision/dataset/formats/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ def load_yolo_annotations(
annotations = {}

for image_path in image_paths:
image_name = Path(image_path).stem
image = cv2.imread(str(image_path))
image_stem = Path(image_path).stem
image_path = str(image_path)
image = cv2.imread(image_path)

annotation_path = os.path.join(annotations_directory_path, f"{image_name}.txt")
annotation_path = os.path.join(annotations_directory_path, f"{image_stem}.txt")
if not os.path.exists(annotation_path):
images[image_path.name] = image
annotations[image_path.name] = Detections.empty()
images[image_path] = image
annotations[image_path] = Detections.empty()
continue

lines = read_txt_file(str(annotation_path))
Expand All @@ -158,8 +159,8 @@ def load_yolo_annotations(
lines=lines, resolution_wh=resolution_wh, with_masks=with_masks
)

images[image_path.name] = image
annotations[image_path.name] = annotation
images[image_path] = image
annotations[image_path] = annotation
return classes, images, annotations


Expand Down Expand Up @@ -227,8 +228,9 @@ def save_yolo_annotations(
approximation_percentage: float = 0.75,
) -> None:
Path(annotations_directory_path).mkdir(parents=True, exist_ok=True)
for image_name, image in images.items():
detections = annotations[image_name]
for image_path, image in images.items():
detections = annotations[image_path]
image_name = Path(image_path).name
yolo_annotations_name = _image_name_to_annotation_name(image_name=image_name)
yolo_annotations_path = os.path.join(
annotations_directory_path, yolo_annotations_name
Expand Down
3 changes: 2 additions & 1 deletion supervision/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def save_dataset_images(
) -> None:
Path(images_directory_path).mkdir(parents=True, exist_ok=True)

for image_name, image in images.items():
for image_path, image in images.items():
image_name = Path(image_path).name
target_image_path = os.path.join(images_directory_path, image_name)
cv2.imwrite(target_image_path, image)

Expand Down