1
- # Mapping can be used to select classes as instsance or segmentation
2
- # By default polygon will be instance and segmentation will be semantic
3
-
4
- # Will use subclasses too..
5
- # checklist and radio will be turned into underscore delimitated names
6
1
from typing import Dict , Any
7
2
import os
8
3
14
9
def create_path_if_not_exists (path : str ):
15
10
if not os .path .exists (path ):
16
11
os .makedirs (path )
12
+ elif os .path .listdir (path ):
13
+ raise ValueError (f"Directory `{ path } `` must be empty." )
17
14
18
15
19
16
def validate_path (path , name ):
20
17
if not os .path .exists (path ):
21
- raise ValueError (f"{ name } { path } must exist" )
18
+ raise ValueError (f"{ name } ` { path } ` must exist" )
22
19
23
20
24
21
class COCOConverter :
25
22
"""
26
- Note that this class is only compatible with image data.. it will ignore other data types.
27
- # TODO: Filter out video annotations..
28
-
23
+ Class for convertering between coco and labelbox formats
24
+ Note that this class is only compatible with image data.
29
25
30
- # Note that these functions are not compatible with subclasses currently.
31
- # If you have subclasses you will have to manually flatten them before using the converter.
26
+ Subclasses are currently ignored .
27
+ To use subclasses, manually flatten them before using the converter.
32
28
"""
33
29
34
30
def serialize_instances (labels : LabelCollection ,
35
31
image_root : str ) -> Dict [str , Any ]:
36
32
"""
37
- Compatible with masks, polygons, and masks. Will turn masks into individual instances
33
+ Convert a Labelbox LabelCollection into an mscoco dataset.
34
+ This function will only convert masks, polygons, and rectangles.
35
+ Masks will be converted into individual instances.
36
+ Use deserialize_panoptic to prevent masks from being split apart.
37
+
38
+ Args:
39
+ labels: A collection of labels to convert
40
+ image_root: Where to save images to
41
+ Returns:
42
+ A dictionary containing labels in the coco object format.
38
43
"""
39
44
create_path_if_not_exists (image_root )
40
45
return CocoInstanceDataset .from_common (labels = labels ,
@@ -43,8 +48,19 @@ def serialize_instances(labels: LabelCollection,
43
48
def serialize_panoptic (labels : LabelCollection , image_root : str ,
44
49
mask_root : str , all_stuff : bool = False ) -> Dict [str , Any ]:
45
50
"""
46
- All stuff turns every class into segmentation classes
51
+ Convert a Labelbox LabelCollection into an mscoco dataset.
52
+ This function will only convert masks, polygons, and rectangles.
53
+ Masks will be converted into individual instances.
54
+ Use deserialize_panoptic to prevent masks from being split apart.
47
55
56
+ Args:
57
+ labels: A collection of labels to convert
58
+ image_root: Where to save images to
59
+ mask_root: Where to save segmentation masks to
60
+ all_stuff: If rectangle or polygon annotations are encountered, they will be treated as instances.
61
+ To convert them to stuff class set `all_stuff=True`.
62
+ Returns:
63
+ A dictionary containing labels in the coco panoptic format.
48
64
"""
49
65
create_path_if_not_exists (image_root )
50
66
create_path_if_not_exists (mask_root )
@@ -55,6 +71,16 @@ def serialize_panoptic(labels: LabelCollection, image_root: str,
55
71
@classmethod
56
72
def deserialize_panoptic (cls , json_data : Dict [str , Any ], image_root : str ,
57
73
mask_root : str ) -> LabelGenerator :
74
+ """
75
+ Convert coco panoptic data into the labelbox format (as a LabelGenerator).
76
+
77
+ Args:
78
+ json_data: panoptic data as a dict
79
+ image_root: Path to local images that are referenced by the panoptic json
80
+ mask_root: Path to local segmentation masks that are referenced by the panoptic json
81
+ Returns:
82
+ LabelGenerator
83
+ """
58
84
validate_path (image_root , 'image_root' )
59
85
validate_path (mask_root , 'mask_root' )
60
86
objs = CocoPanopticDataset (** json_data )
@@ -64,6 +90,15 @@ def deserialize_panoptic(cls, json_data: Dict[str, Any], image_root: str,
64
90
@classmethod
65
91
def deserialize_instances (cls , json_data : Dict [str , Any ],
66
92
image_root : str ) -> LabelGenerator :
93
+ """
94
+ Convert coco object data into the labelbox format (as a LabelGenerator).
95
+
96
+ Args:
97
+ json_data: coco object data as a dict
98
+ image_root: Path to local images that are referenced by the coco object json
99
+ Returns:
100
+ LabelGenerator
101
+ """
67
102
validate_path (image_root , 'image_root' )
68
103
objs = CocoInstanceDataset (** json_data )
69
104
gen = objs .to_common (image_root )
0 commit comments