Skip to content

Commit 27ef38e

Browse files
Handle dict style names in load_labelmap
1 parent 53c41ba commit 27ef38e

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

roboflow/util/image_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ def load_labelmap(f):
102102
if f.lower().endswith(".yaml") or f.lower().endswith(".yml"):
103103
with open(f) as file:
104104
data = yaml.safe_load(file)
105-
if "names" in data:
106-
return {i: name for i, name in enumerate(data["names"])}
105+
names = data.get("names", [])
106+
if isinstance(names, dict):
107+
return {int(k): v for k, v in names.items()}
108+
else:
109+
return {i: name for i, name in enumerate(names)}
107110
else:
108111
with open(f) as file:
109112
lines = [line for line in file.readlines() if line.strip()]

tests/annotations/dict_names.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
names:
2+
0: cat
3+
1: dog
4+
2: fish
5+
nc: 3

tests/util/test_image_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import responses
44

5-
from roboflow.util.image_utils import check_image_path, check_image_url
5+
from roboflow.util.image_utils import check_image_path, check_image_url, load_labelmap
66

77

88
class TestCheckImagePath(unittest.TestCase):
@@ -33,3 +33,9 @@ def test_url_not_found(self):
3333
url = "https://example.com/notfound.png"
3434
responses.add(responses.HEAD, url, status=404)
3535
self.assertFalse(check_image_url(url))
36+
37+
38+
class TestLoadLabelmap(unittest.TestCase):
39+
def test_yaml_dict_names(self):
40+
labelmap = load_labelmap("tests/annotations/dict_names.yaml")
41+
self.assertEqual(labelmap, {0: "cat", 1: "dog", 2: "fish"})

0 commit comments

Comments
 (0)