diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index 95d1fc26..3d4309b9 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -360,11 +360,9 @@ def process_roboflow_result( class_id.append(class_list.index(prediction["class"])) confidence.append(prediction["confidence"]) elif len(prediction["points"]) >= 3: - polygon = np.array([ - [point["x"], point["y"]] - for point - in prediction["points"] - ], dtype=int) + polygon = np.array( + [[point["x"], point["y"]] for point in prediction["points"]], dtype=int + ) 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"])) diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index 316f23e5..26c682ce 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -11,7 +11,6 @@ from supervision.detection.utils import ( process_roboflow_result, ) - TEST_MASK = np.zeros((1, 1000, 1000), dtype=bool) TEST_MASK[:, 300:351, 200:251] = True @@ -263,17 +262,9 @@ def test_filter_polygons_by_area( "roboflow_result, class_list, expected_result, exception", [ ( - { - "predictions": [], - "image": {"width": 1000, "height": 1000} - }, + {"predictions": [], "image": {"width": 1000, "height": 1000}}, ["person", "car", "truck"], - ( - np.empty((0, 4)), - np.empty(0), - np.empty(0), - None - ), + (np.empty((0, 4)), np.empty(0), np.empty(0), None), DoesNotRaise(), ), # empty result ( @@ -299,7 +290,7 @@ def test_filter_polygons_by_area( ), DoesNotRaise(), ), # single correct object detection result -( + ( { "predictions": [ { @@ -317,7 +308,7 @@ def test_filter_polygons_by_area( "height": 100.0, "confidence": 0.8, "class": "truck", - } + }, ], "image": {"width": 1000, "height": 1000}, }, @@ -340,20 +331,34 @@ def test_filter_polygons_by_area( "height": 50.0, "confidence": 0.9, "class": "person", - "points": [] + "points": [], } ], "image": {"width": 1000, "height": 1000}, }, ["person", "car", "truck"], - ( - np.empty((0, 4)), - np.empty(0), - np.empty(0), - None - ), + (np.empty((0, 4)), np.empty(0), np.empty(0), None), DoesNotRaise(), ), # single incorrect instance segmentation result with no points + ( + { + "predictions": [ + { + "x": 200.0, + "y": 300.0, + "width": 50.0, + "height": 50.0, + "confidence": 0.9, + "class": "person", + "points": [{"x": 200.0, "y": 300.0}, {"x": 250.0, "y": 300.0}], + } + ], + "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 ( { "predictions": [ @@ -366,87 +371,60 @@ def test_filter_polygons_by_area( "class": "person", "points": [ {"x": 200.0, "y": 300.0}, - {"x": 250.0, "y": 300.0} - ] + {"x": 250.0, "y": 300.0}, + {"x": 250.0, "y": 350.0}, + {"x": 200.0, "y": 350.0}, + ], } ], "image": {"width": 1000, "height": 1000}, }, ["person", "car", "truck"], ( - np.empty((0, 4)), - np.empty(0), - np.empty(0), - None + np.array([[175.0, 275.0, 225.0, 325.0]]), + np.array([0.9]), + np.array([0]), + TEST_MASK, ), DoesNotRaise(), ), # single incorrect instance segmentation result with no enough points ( - { - "predictions": [ - { - "x": 200.0, - "y": 300.0, - "width": 50.0, - "height": 50.0, - "confidence": 0.9, - "class": "person", - "points": [ - {"x": 200.0, "y": 300.0}, - {"x": 250.0, "y": 300.0}, - {"x": 250.0, "y": 350.0}, - {"x": 200.0, "y": 350.0}, - ] - } - ], - "image": {"width": 1000, "height": 1000}, - }, - ["person", "car", "truck"], - ( - np.array([[175.0, 275.0, 225.0, 325.0]]), - np.array([0.9]), - np.array([0]), - TEST_MASK - ), - DoesNotRaise(), - ), # single incorrect instance segmentation result with no enough points - ( - { - "predictions": [ - { - "x": 200.0, - "y": 300.0, - "width": 50.0, - "height": 50.0, - "confidence": 0.9, - "class": "person", - "points": [ - {"x": 200.0, "y": 300.0}, - {"x": 250.0, "y": 300.0}, - {"x": 250.0, "y": 350.0}, - {"x": 200.0, "y": 350.0}, - ] - }, - { - "x": 500.0, - "y": 500.0, - "width": 100.0, - "height": 100.0, - "confidence": 0.8, - "class": "truck", - "points": [] - } - ], - "image": {"width": 1000, "height": 1000}, - }, - ["person", "car", "truck"], - ( - np.array([[175.0, 275.0, 225.0, 325.0]]), - np.array([0.9]), - np.array([0]), - TEST_MASK - ), - DoesNotRaise(), + { + "predictions": [ + { + "x": 200.0, + "y": 300.0, + "width": 50.0, + "height": 50.0, + "confidence": 0.9, + "class": "person", + "points": [ + {"x": 200.0, "y": 300.0}, + {"x": 250.0, "y": 300.0}, + {"x": 250.0, "y": 350.0}, + {"x": 200.0, "y": 350.0}, + ], + }, + { + "x": 500.0, + "y": 500.0, + "width": 100.0, + "height": 100.0, + "confidence": 0.8, + "class": "truck", + "points": [], + }, + ], + "image": {"width": 1000, "height": 1000}, + }, + ["person", "car", "truck"], + ( + np.array([[175.0, 275.0, 225.0, 325.0]]), + np.array([0.9]), + np.array([0]), + TEST_MASK, + ), + DoesNotRaise(), ), # two instance segmentation results - one correct, one incorrect ], )