From afc240e6beceafde25c6bd01b99ebee408642649 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 16 Aug 2023 13:20:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20small=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/dataset/formats/pascal_voc.py | 24 ++++++++++------------- test/dataset/formats/test_pascal_voc.py | 4 ++-- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/supervision/dataset/formats/pascal_voc.py b/supervision/dataset/formats/pascal_voc.py index fda259d7..a5114ade 100644 --- a/supervision/dataset/formats/pascal_voc.py +++ b/supervision/dataset/formats/pascal_voc.py @@ -21,6 +21,7 @@ def object_to_pascal_voc( object_name = SubElement(root, "name") object_name.text = name + # https://github.com/roboflow/supervision/issues/144 xyxy += 1 bndbox = SubElement(root, "bndbox") @@ -34,6 +35,7 @@ def object_to_pascal_voc( ymax.text = str(int(xyxy[3])) if polygon is not None: + # https://github.com/roboflow/supervision/issues/144 polygon += 1 object_polygon = SubElement(root, "polygon") for index, point in enumerate(polygon, start=1): @@ -232,7 +234,6 @@ def detections_from_xml_obj( class_names.append(class_name) bbox = obj.find("bndbox") - x1 = int(bbox.find("xmin").text) y1 = int(bbox.find("ymin").text) x2 = int(bbox.find("xmax").text) @@ -244,20 +245,20 @@ def detections_from_xml_obj( with_masks = force_masks if force_masks else with_masks for polygon in obj.findall("polygon"): - polygon_points = np.array(parse_polygon_points(polygon)) + polygon = parse_polygon_points(polygon) + # https://github.com/roboflow/supervision/issues/144 + polygon -= 1 mask_from_polygon = polygon_to_mask( - polygon=polygon_points - 1, + polygon=polygon, resolution_wh=resolution_wh, ) masks.append(mask_from_polygon) xyxy = np.array(xyxy) if len(xyxy) > 0 else np.empty((0, 4)) - # Correction functions for VOC XML format as bounding boxes - # in VOC XML start at (1,1) not (0,0). Refer: # https://github.com/roboflow/supervision/issues/144 - xyxy = np.array(xyxy) - 1 + xyxy -= 1 for k in set(class_names): if k not in extended_classes: @@ -275,11 +276,6 @@ def detections_from_xml_obj( return annotation, extended_classes -def parse_polygon_points(polygon: Element) -> List[List[int]]: - polygon_points = [] - coords = polygon.findall(".//*") - for i in range(0, len(coords), 2): - x = int(coords[i].text) - y = int(coords[i + 1].text) - polygon_points.append([x, y]) - return polygon_points +def parse_polygon_points(polygon: Element) -> np.ndarray: + coordinates = [int(coord.text) for coord in polygon.findall(".//*")] + return np.array([(coordinates[i], coordinates[i+1]) for i in range(0, len(coordinates), 2)]) diff --git a/test/dataset/formats/test_pascal_voc.py b/test/dataset/formats/test_pascal_voc.py index 55148cb9..db6598d6 100644 --- a/test/dataset/formats/test_pascal_voc.py +++ b/test/dataset/formats/test_pascal_voc.py @@ -77,7 +77,7 @@ def test_object_to_pascal_voc( """0010010 10010""" ), - [[0, 0], [10, 0], [10, 10], [0, 10]], + np.array([[0, 0], [10, 0], [10, 10], [0, 10]]), DoesNotRaise(), ) ], @@ -89,7 +89,7 @@ def test_parse_polygon_points( ): with exception: result = parse_polygon_points(polygon_element) - assert result == expected_result + assert np.array_equal(result, expected_result) ONE_CLASS_N_BBOX = """test1