Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
7 changes: 5 additions & 2 deletions roboflow/util/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ def load_labelmap(f):
if f.lower().endswith(".yaml") or f.lower().endswith(".yml"):
with open(f) as file:
data = yaml.safe_load(file)
if "names" in data:
return {i: name for i, name in enumerate(data["names"])}
names = data.get("names", [])
if isinstance(names, dict):
return {int(k): v for k, v in names.items()}
else:
return {i: name for i, name in enumerate(names)}
else:
with open(f) as file:
lines = [line for line in file.readlines() if line.strip()]
Expand Down
5 changes: 5 additions & 0 deletions tests/annotations/dict_names.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
names:
0: cat
1: dog
2: fish
nc: 3
8 changes: 7 additions & 1 deletion tests/util/test_image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import responses

from roboflow.util.image_utils import check_image_path, check_image_url
from roboflow.util.image_utils import check_image_path, check_image_url, load_labelmap


class TestCheckImagePath(unittest.TestCase):
Expand Down Expand Up @@ -33,3 +33,9 @@ def test_url_not_found(self):
url = "https://example.com/notfound.png"
responses.add(responses.HEAD, url, status=404)
self.assertFalse(check_image_url(url))


class TestLoadLabelmap(unittest.TestCase):
def test_yaml_dict_names(self):
labelmap = load_labelmap("tests/annotations/dict_names.yaml")
self.assertEqual(labelmap, {0: "cat", 1: "dog", 2: "fish"})