Skip to content

Commit 363b116

Browse files
Merge pull request #395 from roboflow/codex/modify-load_labelmap-for-names-handling
Support YAML dict labelmaps
2 parents 53c41ba + 0c9b7c5 commit 363b116

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

roboflow/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from roboflow.models import CLIPModel, GazeModel # noqa: F401
1616
from roboflow.util.general import write_line
1717

18-
__version__ = "1.2.0"
18+
__version__ = "1.2.1"
1919

2020

2121
def check_key(api_key, model, notebook, num_retries=0):

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)