From 4f513ce2758c82431dfdce6ac64b5ab6c0b8a4b2 Mon Sep 17 00:00:00 2001 From: Akash A Desai <62583018+akashAD98@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:41:13 +0530 Subject: [PATCH 1/7] Update core.py remoed class_list arg from core.py file --- supervision/detection/core.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index f7f1bb5f..3b64c6ae 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -432,7 +432,7 @@ class Detections: ) @classmethod - def from_roboflow(cls, roboflow_result: dict, class_list: List[str]) -> Detections: + def from_roboflow(cls, roboflow_result: dict) -> Detections: """ Create a Detections object from the [Roboflow](https://roboflow.com/) API inference result. @@ -440,8 +440,6 @@ class Detections: Args: roboflow_result (dict): The result from the Roboflow API containing predictions. - class_list (List[str]): A list of class names - corresponding to the class IDs in the API result. Returns: (Detections): A Detections object containing the bounding boxes, class IDs, @@ -464,13 +462,12 @@ class Detections: ... # ... more predictions ... ... ] ... } - >>> class_list = ["person", "car", "dog"] - >>> detections = sv.Detections.from_roboflow(roboflow_result, class_list) + >>> detections = sv.Detections.from_roboflow(roboflow_result) ``` """ xyxy, confidence, class_id, masks = process_roboflow_result( - roboflow_result=roboflow_result, class_list=class_list + roboflow_result=roboflow_result ) if np.asarray(xyxy).shape[0] == 0: From c83959bb16bd8766b84d8262afd7358532fc2237 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:18:28 +0000 Subject: [PATCH 2/7] =?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 --- supervision/detection/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 3b64c6ae..6c6a948e 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -467,7 +467,7 @@ class Detections: ``` """ xyxy, confidence, class_id, masks = process_roboflow_result( - roboflow_result=roboflow_result + roboflow_result=roboflow_result ) if np.asarray(xyxy).shape[0] == 0: From 7cb57c9cc94722422460a30b3a1728cafeddeb26 Mon Sep 17 00:00:00 2001 From: Hardik Dava Date: Tue, 3 Oct 2023 19:33:56 +0200 Subject: [PATCH 3/7] Fixing int conversion bug --- supervision/detection/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index e882173b..3bd19031 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -332,7 +332,7 @@ def extract_ultralytics_masks(yolov8_results) -> Optional[np.ndarray]: def process_roboflow_result( - roboflow_result: dict, class_list: List[str] + roboflow_result: dict ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray]]: if not roboflow_result["predictions"]: return np.empty((0, 4)), np.empty(0), np.empty(0), None @@ -357,7 +357,7 @@ def process_roboflow_result( if "points" not in prediction: xyxy.append([x_min, y_min, x_max, y_max]) - class_id.append(class_list.index(prediction["class"])) + class_id.append(int(prediction["class"])) confidence.append(prediction["confidence"]) elif len(prediction["points"]) >= 3: polygon = np.array( @@ -365,7 +365,7 @@ def process_roboflow_result( ) mask = polygon_to_mask(polygon, resolution_wh=(image_width, image_height)) xyxy.append([x_min, y_min, x_max, y_max]) - class_id.append(class_list.index(prediction["class"])) + class_id.append(int(prediction["class"])) confidence.append(prediction["confidence"]) masks.append(mask) From 8746dd3d0e5cf3eebf38106b51089dc4af3c3305 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:34:29 +0000 Subject: [PATCH 4/7] =?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 --- supervision/detection/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index 3bd19031..94cbebdd 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -332,7 +332,7 @@ def extract_ultralytics_masks(yolov8_results) -> Optional[np.ndarray]: def process_roboflow_result( - roboflow_result: dict + roboflow_result: dict, ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray]]: if not roboflow_result["predictions"]: return np.empty((0, 4)), np.empty(0), np.empty(0), None From fefd273a0f5bb70de8740b7284a13db2cba8e836 Mon Sep 17 00:00:00 2001 From: Hardik Dava Date: Tue, 3 Oct 2023 19:45:38 +0200 Subject: [PATCH 5/7] removing class list from test inputs --- test/detection/test_utils.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index 63a2c33c..bd99aab5 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -264,7 +264,6 @@ def test_filter_polygons_by_area( [ ( {"predictions": [], "image": {"width": 1000, "height": 1000}}, - ["person", "car", "truck"], (np.empty((0, 4)), np.empty(0), np.empty(0), None), DoesNotRaise(), ), # empty result @@ -282,7 +281,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], ( np.array([[175.0, 275.0, 225.0, 325.0]]), np.array([0.9]), @@ -313,7 +311,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], ( np.array([[175.0, 275.0, 225.0, 325.0], [450.0, 450.0, 550.0, 550.0]]), np.array([0.9, 0.8]), @@ -337,7 +334,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], (np.empty((0, 4)), np.empty(0), np.empty(0), None), DoesNotRaise(), ), # single incorrect instance segmentation result with no points @@ -356,7 +352,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], (np.empty((0, 4)), np.empty(0), np.empty(0), None), DoesNotRaise(), ), # single incorrect instance segmentation result with no enough points @@ -380,7 +375,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], ( np.array([[175.0, 275.0, 225.0, 325.0]]), np.array([0.9]), @@ -418,7 +412,6 @@ def test_filter_polygons_by_area( ], "image": {"width": 1000, "height": 1000}, }, - ["person", "car", "truck"], ( np.array([[175.0, 275.0, 225.0, 325.0]]), np.array([0.9]), @@ -437,7 +430,7 @@ def test_process_roboflow_result( ) -> None: with exception: result = process_roboflow_result( - roboflow_result=roboflow_result, class_list=class_list + roboflow_result=roboflow_result ) assert np.array_equal(result[0], expected_result[0]) assert np.array_equal(result[1], expected_result[1]) From ed30ef68afbdc6c29d700b6d0048201f8426f31d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:47:43 +0000 Subject: [PATCH 6/7] =?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 --- test/detection/test_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index bd99aab5..330b94f0 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -429,9 +429,7 @@ def test_process_roboflow_result( exception: Exception, ) -> None: with exception: - result = process_roboflow_result( - roboflow_result=roboflow_result - ) + result = process_roboflow_result(roboflow_result=roboflow_result) assert np.array_equal(result[0], expected_result[0]) assert np.array_equal(result[1], expected_result[1]) assert np.array_equal(result[2], expected_result[2]) From e91151618042c2e99c3a91d66cacf9f76dc4470f Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 4 Oct 2023 10:44:49 +0200 Subject: [PATCH 7/7] Change prediction class handling from text to ids Updated the prediction parsing logic in supervision/detection/utils.py and supervision/detection/core.py to use 'class_id' instead of 'class' names. Adapted tests in test/detection/test_utils.py accordingly to reflect the same change. --- supervision/detection/core.py | 1 + supervision/detection/utils.py | 4 ++-- test/detection/test_utils.py | 13 ++++++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 6c6a948e..0ba28491 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -456,6 +456,7 @@ class Detections: ... "y": 0.5, ... "width": 0.2, ... "height": 0.3, + ... "class_id": 0, ... "class": "person", ... "confidence": 0.9 ... }, diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index 94cbebdd..2beff657 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -357,7 +357,7 @@ def process_roboflow_result( if "points" not in prediction: xyxy.append([x_min, y_min, x_max, y_max]) - class_id.append(int(prediction["class"])) + class_id.append(prediction["class_id"]) confidence.append(prediction["confidence"]) elif len(prediction["points"]) >= 3: polygon = np.array( @@ -365,7 +365,7 @@ def process_roboflow_result( ) mask = polygon_to_mask(polygon, resolution_wh=(image_width, image_height)) xyxy.append([x_min, y_min, x_max, y_max]) - class_id.append(int(prediction["class"])) + class_id.append(prediction["class_id"]) confidence.append(prediction["confidence"]) masks.append(mask) diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index 330b94f0..529914ae 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -260,7 +260,7 @@ def test_filter_polygons_by_area( @pytest.mark.parametrize( - "roboflow_result, class_list, expected_result, exception", + "roboflow_result, expected_result, exception", [ ( {"predictions": [], "image": {"width": 1000, "height": 1000}}, @@ -276,6 +276,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", } ], @@ -298,6 +299,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", }, { @@ -306,6 +308,7 @@ def test_filter_polygons_by_area( "width": 100.0, "height": 100.0, "confidence": 0.8, + "class_id": 7, "class": "truck", }, ], @@ -314,7 +317,7 @@ def test_filter_polygons_by_area( ( np.array([[175.0, 275.0, 225.0, 325.0], [450.0, 450.0, 550.0, 550.0]]), np.array([0.9, 0.8]), - np.array([0, 2]), + np.array([0, 7]), None, ), DoesNotRaise(), @@ -328,6 +331,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", "points": [], } @@ -346,6 +350,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", "points": [{"x": 200.0, "y": 300.0}, {"x": 250.0, "y": 300.0}], } @@ -364,6 +369,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", "points": [ {"x": 200.0, "y": 300.0}, @@ -392,6 +398,7 @@ def test_filter_polygons_by_area( "width": 50.0, "height": 50.0, "confidence": 0.9, + "class_id": 0, "class": "person", "points": [ {"x": 200.0, "y": 300.0}, @@ -406,6 +413,7 @@ def test_filter_polygons_by_area( "width": 100.0, "height": 100.0, "confidence": 0.8, + "class_id": 7, "class": "truck", "points": [], }, @@ -424,7 +432,6 @@ def test_filter_polygons_by_area( ) def test_process_roboflow_result( roboflow_result: dict, - class_list: List[str], expected_result: Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray]], exception: Exception, ) -> None: