Skip to content

Commit

Permalink
black reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristofferEdlund committed Nov 14, 2023
1 parent 5ff60ae commit 8ded8db
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions darwin/exporter/formats/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""



def build_image_annotation(annotation_file: dt.AnnotationFile) -> Dict[str, Any]:
"""
Builds and returns a dictionary with the annotations present in the given file in Darwin v2 format.
Expand All @@ -36,52 +35,19 @@ def build_image_annotation(annotation_file: dt.AnnotationFile) -> Dict[str, Any]
annotations_list.append(annotation_data)

slots_data = _build_slots_data(annotation_file.slots)
item =_build_item_data(annotation_file)
item['slots'] = slots_data
item = _build_item_data(annotation_file)
item["slots"] = slots_data

return {
"version": "2.0",
"schema_ref": "https://darwin-public.s3.eu-west-1.amazonaws.com/darwin_json/2.0/schema.json",
"item": item,
"annotations": annotations_list
"annotations": annotations_list,
}


@deprecation.deprecated(
deprecated_in="0.7.8",
removed_in="0.8.0",
current_version=__version__,
details=DEPRECATION_MESSAGE,
)
def build_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:
if annotation.annotation_class.annotation_type == "complex_polygon":
return {"path": annotation.data["paths"]}

if annotation.annotation_class.annotation_type == "polygon":
return dict(
filter(lambda item: item[0] != "bounding_box", annotation.data.items())
)

return dict(annotation.data)


def _build_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:
if annotation.annotation_class.annotation_type == "complex_polygon":
return {"path": annotation.data["paths"]}

if annotation.annotation_class.annotation_type == "polygon":
return dict(
filter(lambda item: item[0] != "bounding_box", annotation.data.items())
)

return dict(annotation.data)


def _build_v2_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:
annotation_data = {
"id": annotation.id,
"name": annotation.annotation_class.name
}
annotation_data = {"id": annotation.id, "name": annotation.annotation_class.name}

if annotation.annotation_class.annotation_type == "bounding_box":
annotation_data["bounding_box"] = _build_bounding_box_data(annotation.data)
Expand All @@ -92,16 +58,19 @@ def _build_v2_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:

return annotation_data


def _build_bounding_box_data(data: Dict[str, Any]) -> Dict[str, Any]:
return {
"h": data.get("h"),
"w": data.get("w"),
"x": data.get("x"),
"y": data.get("y")
"y": data.get("y"),
}


def _build_polygon_data(data: Dict[str, Any]) -> Dict[str, List[List[Dict[str, float]]]]:
def _build_polygon_data(
data: Dict[str, Any]
) -> Dict[str, List[List[Dict[str, float]]]]:
"""
Builds the polygon data for Darwin v2 format.
Expand Down Expand Up @@ -150,15 +119,17 @@ def _build_item_data(annotation_file: dt.AnnotationFile) -> Dict[str, Any]:
"source_info": {
"dataset": {
"name": annotation_file.dataset_name,
"slug": annotation_file.dataset_name.lower().replace(" ", "-") if annotation_file.dataset_name else None
"slug": annotation_file.dataset_name.lower().replace(" ", "-")
if annotation_file.dataset_name
else None,
},
"item_id": annotation_file.item_id or "unknown-item-id",
"team": {
"name": None, # TODO Replace with actual team name
"slug": None # TODO Replace with actual team slug
"name": None, # TODO Replace with actual team name
"slug": None, # TODO Replace with actual team slug
},
"workview_url": annotation_file.workview_url
}
"workview_url": annotation_file.workview_url,
},
}


Expand All @@ -184,8 +155,38 @@ def _build_slots_data(slots: List[dt.Slot]) -> List[Dict[str, Any]]:
"width": slot.width,
"height": slot.height,
"thumbnail_url": slot.thumbnail_url,
"source_files": slot.source_files
"source_files": slot.source_files,
}
slots_data.append(slot_data)

return slots_data


@deprecation.deprecated(
deprecated_in="0.7.8",
removed_in="0.8.0",
current_version=__version__,
details=DEPRECATION_MESSAGE,
)
def build_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:
if annotation.annotation_class.annotation_type == "complex_polygon":
return {"path": annotation.data["paths"]}

if annotation.annotation_class.annotation_type == "polygon":
return dict(
filter(lambda item: item[0] != "bounding_box", annotation.data.items())
)

return dict(annotation.data)


def _build_annotation_data(annotation: dt.Annotation) -> Dict[str, Any]:
if annotation.annotation_class.annotation_type == "complex_polygon":
return {"path": annotation.data["paths"]}

if annotation.annotation_class.annotation_type == "polygon":
return dict(
filter(lambda item: item[0] != "bounding_box", annotation.data.items())
)

return dict(annotation.data)

0 comments on commit 8ded8db

Please sign in to comment.