@@ -27,34 +27,34 @@ def export_annotations(task_id, dataset_id):
2727
2828 task .info ("Beginning Export (COCO Format)" )
2929
30- categories = CategoryModel .objects (deleted = False ) \
31- .exclude ('deleted_date' ).in_bulk (dataset .categories ).items ()
30+ db_categories = CategoryModel .objects (id__in = dataset .categories , deleted = False ) \
31+ .only (* CategoryModel .COCO_PROPERTIES )
32+ db_images = ImageModel .objects (deleted = False , annotated = True , dataset_id = dataset .id )\
33+ .only (* ImageModel .COCO_PROPERTIES )
34+ db_annotations = AnnotationModel .objects (deleted = False )
3235
33- total_items = len ( dataset . categories )
36+ total_items = db_categories . count ( )
3437 dataset = fix_ids (dataset )
3538
36- images = ImageModel .objects (deleted = False , annotated = True , dataset_id = dataset .get ('id' )).exclude ('deleted_date' )
37- all_annotations = AnnotationModel .objects (deleted = False ).exclude ('deleted_date' , 'paper_object' )
38-
3939 coco = {
4040 'images' : [],
4141 'categories' : [],
4242 'annotations' : []
4343 }
4444
45- total_items += images .count ()
45+ total_items += db_images .count ()
4646 progress = 0
47- for category in categories :
48- category = fix_ids (category [1 ])
4947
50- del category ['deleted' ]
48+ # iterate though all categoires and upsert
49+ for category in fix_ids (db_categories ):
50+
5151 if len (category .get ('keypoint_labels' , [])) > 0 :
5252 category ['keypoints' ] = category .pop ('keypoint_labels' , [])
5353 category ['skeleton' ] = category .pop ('keypoint_edges' , [])
5454 else :
5555 if 'keypoint_edges' in category :
5656 del category ['keypoint_edges' ]
57- if 'keypoint_labels' in categories :
57+ if 'keypoint_labels' in category :
5858 del category ['keypoint_labels' ]
5959
6060 task .info (f"Adding category: { category .get ('name' )} " )
@@ -63,22 +63,23 @@ def export_annotations(task_id, dataset_id):
6363 progress += 1
6464 task .set_progress ((progress / total_items )* 100 , socket = socket )
6565
66- total_annotations = 0
67- total_images = 0
68- for image in images :
69- annotations = all_annotations . filter ( image_id = image . id )
70- if annotations . count () == 0 :
71- continue
66+ total_annotations = db_annotations . count ()
67+ total_images = db_images . count ()
68+ for image in fix_ids ( db_images ) :
69+
70+ progress += 1
71+ task . set_progress (( progress / total_items ) * 100 , socket = socket )
7272
73- annotations = fix_ids (annotations .all ())
73+ annotations = db_annotations .filter (image_id = image .get ('id' ))\
74+ .only (* AnnotationModel .COCO_PROPERTIES )
75+ annotations = fix_ids (annotations )
7476 num_annotations = 0
7577 for annotation in annotations :
7678
7779 has_keypoints = len (annotation .get ('keypoints' , [])) > 0
7880 has_segmentation = len (annotation .get ('segmentation' , [])) > 0
7981
8082 if has_keypoints or has_segmentation :
81- del annotation ['deleted' ]
8283
8384 if not has_keypoints :
8485 if 'keypoints' in annotation :
@@ -91,16 +92,8 @@ def export_annotations(task_id, dataset_id):
9192 num_annotations += 1
9293 coco .get ('annotations' ).append (annotation )
9394
94- task .info (f'Exporting { num_annotations } annotations for image { image .id } ' )
95- total_annotations += num_annotations
96-
97- image = fix_ids (image )
98- del image ['deleted' ]
95+ task .info (f"Exporting { num_annotations } annotations for image { image .get ('id' )} " )
9996 coco .get ('images' ).append (image )
100- total_images += 1
101-
102- progress += 1
103- task .set_progress ((progress / total_items )* 100 , socket = socket )
10497
10598 file_path = dataset .get ('directory' ) + '/coco.json'
10699 with open (file_path , 'w' ) as fp :
0 commit comments