1
- from typing import Dict , Any
1
+ from typing import Dict , Any , Union
2
2
from pathlib import Path
3
3
import os
4
4
7
7
from labelbox .data .serialization .coco .panoptic_dataset import CocoPanopticDataset
8
8
9
9
10
- def create_path_if_not_exists (path : Path , ignore_existing_data = False ):
10
+ def create_path_if_not_exists (path : Union [Path , str ],
11
+ ignore_existing_data = False ):
12
+ path = Path (path )
11
13
if not path .exists ():
12
14
path .mkdir (parents = True , exist_ok = True )
13
15
elif not ignore_existing_data and os .listdir (path ):
14
16
raise ValueError (
15
17
f"Directory `{ path } `` must be empty. Or set `ignore_existing_data=True`"
16
18
)
19
+ return path
17
20
18
21
19
- def validate_path (path : Path , name : str ):
22
+ def validate_path (path : Union [Path , str ], name : str ):
23
+ path = Path (path )
20
24
if not path .exists ():
21
25
raise ValueError (f"{ name } `{ path } ` must exist" )
22
26
@@ -29,10 +33,12 @@ class COCOConverter:
29
33
Subclasses are currently ignored.
30
34
To use subclasses, manually flatten them before using the converter.
31
35
"""
36
+
32
37
@staticmethod
33
38
def serialize_instances (labels : LabelCollection ,
34
- image_root : Path ,
35
- ignore_existing_data = False , max_workers = 8 ) -> Dict [str , Any ]:
39
+ image_root : Union [Path , str ],
40
+ ignore_existing_data = False ,
41
+ max_workers = 8 ) -> Dict [str , Any ]:
36
42
"""
37
43
Convert a Labelbox LabelCollection into an mscoco dataset.
38
44
This function will only convert masks, polygons, and rectangles.
@@ -48,16 +54,18 @@ def serialize_instances(labels: LabelCollection,
48
54
Returns:
49
55
A dictionary containing labels in the coco object format.
50
56
"""
51
- create_path_if_not_exists (image_root , ignore_existing_data )
57
+ image_root = create_path_if_not_exists (image_root , ignore_existing_data )
52
58
return CocoInstanceDataset .from_common (labels = labels ,
53
- image_root = image_root , max_workers = max_workers ).dict ()
59
+ image_root = image_root ,
60
+ max_workers = max_workers ).dict ()
54
61
55
62
@staticmethod
56
63
def serialize_panoptic (labels : LabelCollection ,
57
- image_root : Path ,
58
- mask_root : Path ,
64
+ image_root : Union [ Path , str ] ,
65
+ mask_root : Union [ Path , str ] ,
59
66
all_stuff : bool = False ,
60
- ignore_existing_data = False , max_workers = 8 ) -> Dict [str , Any ]:
67
+ ignore_existing_data = False ,
68
+ max_workers = 8 ) -> Dict [str , Any ]:
61
69
"""
62
70
Convert a Labelbox LabelCollection into an mscoco dataset.
63
71
This function will only convert masks, polygons, and rectangles.
@@ -76,16 +84,18 @@ def serialize_panoptic(labels: LabelCollection,
76
84
Returns:
77
85
A dictionary containing labels in the coco panoptic format.
78
86
"""
79
- create_path_if_not_exists (image_root , ignore_existing_data )
80
- create_path_if_not_exists (mask_root , ignore_existing_data )
87
+ image_root = create_path_if_not_exists (image_root , ignore_existing_data )
88
+ mask_root = create_path_if_not_exists (mask_root , ignore_existing_data )
81
89
return CocoPanopticDataset .from_common (labels = labels ,
82
90
image_root = image_root ,
83
91
mask_root = mask_root ,
84
- all_stuff = all_stuff , max_workers = max_workers ).dict ()
92
+ all_stuff = all_stuff ,
93
+ max_workers = max_workers ).dict ()
85
94
86
95
@staticmethod
87
- def deserialize_panoptic (json_data : Dict [str , Any ], image_root : Path ,
88
- mask_root : Path ) -> LabelGenerator :
96
+ def deserialize_panoptic (json_data : Dict [str , Any ], image_root : Union [Path ,
97
+ str ],
98
+ mask_root : Union [Path , str ]) -> LabelGenerator :
89
99
"""
90
100
Convert coco panoptic data into the labelbox format (as a LabelGenerator).
91
101
@@ -96,8 +106,8 @@ def deserialize_panoptic(json_data: Dict[str, Any], image_root: Path,
96
106
Returns:
97
107
LabelGenerator
98
108
"""
99
- validate_path (image_root , 'image_root' )
100
- validate_path (mask_root , 'mask_root' )
109
+ image_root = validate_path (image_root , 'image_root' )
110
+ mask_root = validate_path (mask_root , 'mask_root' )
101
111
objs = CocoPanopticDataset (** json_data )
102
112
gen = objs .to_common (image_root , mask_root )
103
113
return LabelGenerator (data = gen )
@@ -114,7 +124,7 @@ def deserialize_instances(json_data: Dict[str, Any],
114
124
Returns:
115
125
LabelGenerator
116
126
"""
117
- validate_path (image_root , 'image_root' )
127
+ image_root = validate_path (image_root , 'image_root' )
118
128
objs = CocoInstanceDataset (** json_data )
119
129
gen = objs .to_common (image_root )
120
130
return LabelGenerator (data = gen )
0 commit comments