def test_eq(self): polygon1 = LabeledPolygon([[1, 1], [1, 2]], category="cat", attributes={"gender": "male"}) polygon2 = LabeledPolygon([[1, 1], [1, 2]], category="cat", attributes={"gender": "male"}) polygon3 = LabeledPolygon([[1, 1], [1, 3]], category="cat", attributes={"gender": "male"}) assert polygon1 == polygon2 assert polygon1 != polygon3
def _get_data(image_path: str, root_path: str, segment_name: str, folder_name: str) -> Data: filename = os.path.basename(image_path) city = filename.split("_", 1)[0] image_prefix = filename.rsplit("_", 1)[0] label_dir = os.path.join(root_path, folder_name, segment_name, city) data = Data(image_path) # get semantic mask and instance mask label = data.label label.semantic_mask = SemanticMask( os.path.join(label_dir, f"{image_prefix}_{folder_name}_labelIds.png")) label.instance_mask = InstanceMask( os.path.join(label_dir, f"{image_prefix}_{folder_name}_instanceIds.png")) # get polygons polygons: List[LabeledPolygon] = [] with open( os.path.join(label_dir, f"{image_prefix}_{folder_name}_polygons.json"), encoding="utf-8", ) as fp: objects = json.load(fp)["objects"] for obj in objects: polygons.append(LabeledPolygon(obj["polygon"], category=obj["label"])) label.polygon = polygons return data
def _add_poly2d_label_10k(label_info: Dict[str, Any], polygon: List[LabeledPolygon]) -> None: poly2d_info = label_info["poly2d"][0] labeled_polygon = LabeledPolygon( points=poly2d_info["vertices"], category=label_info["category"], attributes=label_info.get("attributes", {}), ) polygon.append(labeled_polygon)
def test_init(self): labeledpolygon = LabeledPolygon([(1, 2)], category="cat", attributes={"gender": "male"}, instance="12345") assert labeledpolygon[0] == Vector2D(1, 2) assert labeledpolygon.category == "cat" assert labeledpolygon.attributes == {"gender": "male"} assert labeledpolygon.instance == "12345"
def test_dumps(self): labeledpolygon = LabeledPolygon([(1, 2)], category="cat", attributes={"gender": "male"}, instance="12345") assert labeledpolygon.dumps() == { "polygon": [ { "x": 1, "y": 2 }, ], "category": "cat", "attributes": { "gender": "male" }, "instance": "12345", }
def _get_polygons(image_path: str) -> List[LabeledPolygon]: attributes: Dict[str, Union[int, str]] = {} annotations = os.path.splitext(os.path.basename(image_path))[0].split("-", 6) points = (map(int, point.split("&")) for point in annotations[3].split("_", 3)) tilt_degree = annotations[1].split("_", 1) attributes["horizontal_tilt"] = int(tilt_degree[0]) attributes["vertical_tilt"] = int(tilt_degree[1]) attributes["license_plate"] = _get_license_plate(annotations[4]) attributes["brightness"] = int(annotations[5]) attributes["blurriness"] = int(annotations[6]) return [LabeledPolygon(points, attributes=attributes)]
def _add_poly2d_label_100k( label_info: Dict[str, Any], polygon: List[LabeledPolygon], polyline2d: List[LabeledPolyline2D] ) -> None: poly2d_info = label_info["poly2d"][0] if poly2d_info["closed"]: labeled_polygon = LabeledPolygon( points=poly2d_info["vertices"], category=label_info["category"], attributes=label_info["attributes"], ) polygon.append(labeled_polygon) else: labeled_polyline2d = LabeledPolyline2D( points=poly2d_info["vertices"], category=label_info["category"], attributes=label_info.get("attributes", {}), beizer_point_types=poly2d_info["types"], ) polyline2d.append(labeled_polyline2d)
def test_loads(self): content = { "polygon": [ { "x": 1, "y": 2 }, ], "category": "cat", "attributes": { "gender": "male" }, "instance": 12345, } labeledpolygon = LabeledPolygon.loads(content) assert labeledpolygon[0] == Vector2D(1, 2) assert labeledpolygon.category == "cat" assert labeledpolygon.attributes == {"gender": "male"} assert labeledpolygon.instance == 12345
def _get_polygon_labels( annotations_dir: str, segment_name: str, polygon_attribute_names: Tuple[str, ...] ) -> DefaultDict[str, List[LabeledPolygon]]: label_path = os.path.join( annotations_dir, f"RarePlanes_{segment_name.capitalize()}_Coco_Annotations_tiled.json") image_name_to_polygons: DefaultDict[ str, List[LabeledPolygon]] = defaultdict(list) with open(label_path, encoding="utf-8") as fp: label_contents = json.load(fp) annotations, categories = label_contents["annotations"], label_contents[ "categories"] for annotation, category in zip(annotations, categories): attributes = { attribute: annotation[attribute] for attribute in polygon_attribute_names } attributes["canards"] = annotation["canards"] == "Yes" attributes["truncated"] = bool(annotation["truncated"]) image_name_to_polygons[category["image_fname"]].append( LabeledPolygon(chunked(annotation["segmentation"][0], 2), attributes=attributes)) return image_name_to_polygons