Skip to content

Commit 97ae155

Browse files
authored
Merge pull request #226 from hardikdava/fix/yolo_loading_bug
Fix yolo dataset background instance loading
2 parents 24f0137 + 864764e commit 97ae155

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

supervision/dataset/formats/yolo.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ def _polygons_to_masks(
5252
)
5353

5454

55-
def _pair_image_with_annotation(
56-
image_paths: List[Union[str, Path]], annotation_paths: List[Union[str, Path]]
57-
) -> List[Tuple[Union[str, Path], Union[str, Path]]]:
58-
image_dict = {p.stem: p for p in image_paths}
59-
return [
60-
(image_dict[annotation_path.stem], annotation_path)
61-
for annotation_path in annotation_paths
62-
if annotation_path.stem in image_dict
63-
]
64-
65-
6655
def _with_mask(lines: List[str]) -> bool:
6756
return any([len(line.split()) > 5 for line in lines])
6857

@@ -137,14 +126,21 @@ def load_yolo_annotations(
137126
annotation_paths = list_files_with_extensions(
138127
directory=annotations_directory_path, extensions=["txt"]
139128
)
140-
path_pairs = _pair_image_with_annotation(
141-
image_paths=image_paths, annotation_paths=annotation_paths
142-
)
129+
143130
classes = _extract_class_names(file_path=data_yaml_path)
144131
images = {}
145132
annotations = {}
146-
for image_path, annotation_path in path_pairs:
133+
134+
for image_path in image_paths:
135+
image_name = Path(image_path).stem
147136
image = cv2.imread(str(image_path))
137+
138+
annotation_path = os.path.join(annotations_directory_path, f"{image_name}.txt")
139+
if not os.path.exists(annotation_path):
140+
images[image_path.name] = image
141+
annotations[image_path.name] = Detections.empty()
142+
continue
143+
148144
lines = read_txt_file(str(annotation_path))
149145
h, w, _ = image.shape
150146
resolution_wh = (w, h)

0 commit comments

Comments
 (0)