From 79a95be0ec92a43a3ced69a8dd689f8e3bc6e532 Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Wed, 9 Jul 2025 16:35:18 +0200 Subject: [PATCH 1/2] fix labelmap parsing for yaml dict --- roboflow/util/image_utils.py | 5 ++++- tests/util/test_image_utils.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/roboflow/util/image_utils.py b/roboflow/util/image_utils.py index 71a32824..6d556ab3 100644 --- a/roboflow/util/image_utils.py +++ b/roboflow/util/image_utils.py @@ -103,7 +103,10 @@ def load_labelmap(f): 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["names"] + if isinstance(names, dict): + return {int(i): name for i, name in names.items()} + 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()] diff --git a/tests/util/test_image_utils.py b/tests/util/test_image_utils.py index 5a17fe37..af97171d 100644 --- a/tests/util/test_image_utils.py +++ b/tests/util/test_image_utils.py @@ -3,6 +3,9 @@ import responses from roboflow.util.image_utils import check_image_path, check_image_url +from roboflow.util.image_utils import load_labelmap +import tempfile +import os class TestCheckImagePath(unittest.TestCase): @@ -33,3 +36,17 @@ 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): + with tempfile.NamedTemporaryFile("w+", suffix=".yaml", delete=False) as tmp: + tmp.write("names:\n 0: abc\n 1: def\n") + tmp.flush() + result = load_labelmap(tmp.name) + os.unlink(tmp.name) + self.assertEqual(result, {0: "abc", 1: "def"}) + + def test_yaml_list_names(self): + result = load_labelmap("tests/datasets/sharks-tiny-yolov9/data.yaml") + self.assertEqual(result, {0: "fish", 1: "primary", 2: "shark"}) From 5cad1fbb744bcb0bff75710af633bb0bbafac473 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Jul 2025 14:35:28 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/util/test_image_utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/util/test_image_utils.py b/tests/util/test_image_utils.py index af97171d..ff4081a3 100644 --- a/tests/util/test_image_utils.py +++ b/tests/util/test_image_utils.py @@ -1,11 +1,10 @@ +import os +import tempfile import unittest import responses -from roboflow.util.image_utils import check_image_path, check_image_url -from roboflow.util.image_utils import load_labelmap -import tempfile -import os +from roboflow.util.image_utils import check_image_path, check_image_url, load_labelmap class TestCheckImagePath(unittest.TestCase):