@@ -2167,6 +2167,7 @@ def __get_yolo_format_annotations(self, dataset_folder_path: str) -> dict:
21672167
21682168 def export_coco (
21692169 self ,
2170+ project : str ,
21702171 tasks : list ,
21712172 annotations : list = [],
21722173 output_dir : str = os .path .join ("output" , "coco" ),
@@ -2176,6 +2177,7 @@ def export_coco(
21762177 Convert tasks to COCO format and export as a file.
21772178 If you pass annotations, you can export Pose Estimation type annotations.
21782179
2180+ project is slug of your project (Required).
21792181 tasks is a list of tasks (Required).
21802182 annotations is a list of annotations (Optional).
21812183 output_dir is output directory(default: output/coco) (Optional).
@@ -2185,14 +2187,27 @@ def export_coco(
21852187 raise FastLabelInvalidException (
21862188 "Output file name must have a json extension" , 422
21872189 )
2188- coco = converters .to_coco (tasks , annotations )
2190+
2191+ project = self .find_project_by_slug (project )
2192+ if project is None :
2193+ raise FastLabelInvalidException (
2194+ "Project not found. Check the project slag." , 422
2195+ )
2196+
21892197 os .makedirs (output_dir , exist_ok = True )
2198+ coco = converters .to_coco (
2199+ project_type = project ["type" ],
2200+ tasks = tasks ,
2201+ annotations = annotations ,
2202+ output_dir = output_dir ,
2203+ )
21902204 file_path = os .path .join (output_dir , output_file_name )
21912205 with open (file_path , "w" ) as f :
21922206 json .dump (coco , f , indent = 4 , ensure_ascii = False )
21932207
21942208 def export_yolo (
21952209 self ,
2210+ project : str ,
21962211 tasks : list ,
21972212 classes : list = [],
21982213 output_dir : str = os .path .join ("output" , "yolo" ),
@@ -2203,43 +2218,75 @@ def export_yolo(
22032218 If not , classes.txt will be generated based on passed tasks .
22042219 (Annotations never used in your project will not be exported.)
22052220
2221+ project is slug of your project (Required).
22062222 tasks is a list of tasks (Required).
22072223 classes is a list of annotation values. e.g. ['dog','bird'] (Optional).
22082224 output_dir is output directory(default: output/yolo) (Optional).
22092225 """
2210- annos , categories = converters .to_yolo (tasks , classes )
2226+
2227+ project = self .find_project_by_slug (project )
2228+ if project is None :
2229+ raise FastLabelInvalidException (
2230+ "Project not found. Check the project slag." , 422
2231+ )
2232+
2233+ os .makedirs (output_dir , exist_ok = True )
2234+ annos , categories = converters .to_yolo (
2235+ project_type = project ["type" ],
2236+ tasks = tasks ,
2237+ classes = classes ,
2238+ output_dir = output_dir ,
2239+ )
22112240 for anno in annos :
22122241 file_name = anno ["filename" ]
22132242 basename = utils .get_basename (file_name )
22142243 file_path = os .path .join (output_dir , "annotations" , basename + ".txt" )
22152244 os .makedirs (os .path .dirname (file_path ), exist_ok = True )
22162245 with open (file_path , "w" , encoding = "utf8" ) as f :
2217- for obj in anno ["object" ]:
2246+ objects = anno .get ("object" )
2247+ if objects is None :
2248+ continue
2249+ for obj in objects :
22182250 f .write (obj )
22192251 f .write ("\n " )
22202252 classes_file_path = os .path .join (output_dir , "classes.txt" )
2221- os .makedirs (os .path .dirname (classes_file_path ), exist_ok = True )
22222253 with open (classes_file_path , "w" , encoding = "utf8" ) as f :
22232254 for category in categories :
22242255 f .write (category ["name" ])
22252256 f .write ("\n " )
22262257
22272258 def export_pascalvoc (
2228- self , tasks : list , output_dir : str = os .path .join ("output" , "pascalvoc" )
2259+ self ,
2260+ project : str ,
2261+ tasks : list ,
2262+ output_dir : str = os .path .join ("output" , "pascalvoc" ),
22292263 ) -> None :
22302264 """
22312265 Convert tasks to Pascal VOC format as files.
22322266
2267+ project is slug of your project (Required).
22332268 tasks is a list of tasks (Required).
22342269 output_dir is output directory(default: output/pascalvoc) (Optional).
22352270 """
2236- pascalvoc = converters .to_pascalvoc (tasks )
2271+
2272+ project = self .find_project_by_slug (project )
2273+ if project is None :
2274+ raise FastLabelInvalidException (
2275+ "Project not found. Check the project slag." , 422
2276+ )
2277+
2278+ os .makedirs (output_dir , exist_ok = True )
2279+ pascalvoc = converters .to_pascalvoc (
2280+ project_type = project ["type" ], tasks = tasks , output_dir = output_dir
2281+ )
22372282 for voc in pascalvoc :
22382283 file_name = voc ["annotation" ]["filename" ]
22392284 basename = utils .get_basename (file_name )
2240- file_path = os .path .join (output_dir , basename + ".xml" )
2285+ file_path = os .path .join (output_dir , "annotations" , basename + ".xml" )
22412286 os .makedirs (os .path .dirname (file_path ), exist_ok = True )
2242- xml = xmltodict .unparse (voc , pretty = True , full_document = False )
2287+ xml = xmltodict .unparse (
2288+ voc , pretty = True , indent = " " , full_document = False
2289+ )
22432290 with open (file_path , "w" , encoding = "utf8" ) as f :
22442291 f .write (xml )
22452292
0 commit comments