@@ -27,34 +27,34 @@ def export_annotations(task_id, dataset_id):
27
27
28
28
task .info ("Beginning Export (COCO Format)" )
29
29
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 )
32
35
33
- total_items = len ( dataset . categories )
36
+ total_items = db_categories . count ( )
34
37
dataset = fix_ids (dataset )
35
38
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
-
39
39
coco = {
40
40
'images' : [],
41
41
'categories' : [],
42
42
'annotations' : []
43
43
}
44
44
45
- total_items += images .count ()
45
+ total_items += db_images .count ()
46
46
progress = 0
47
- for category in categories :
48
- category = fix_ids (category [1 ])
49
47
50
- del category ['deleted' ]
48
+ # iterate though all categoires and upsert
49
+ for category in fix_ids (db_categories ):
50
+
51
51
if len (category .get ('keypoint_labels' , [])) > 0 :
52
52
category ['keypoints' ] = category .pop ('keypoint_labels' , [])
53
53
category ['skeleton' ] = category .pop ('keypoint_edges' , [])
54
54
else :
55
55
if 'keypoint_edges' in category :
56
56
del category ['keypoint_edges' ]
57
- if 'keypoint_labels' in categories :
57
+ if 'keypoint_labels' in category :
58
58
del category ['keypoint_labels' ]
59
59
60
60
task .info (f"Adding category: { category .get ('name' )} " )
@@ -63,22 +63,23 @@ def export_annotations(task_id, dataset_id):
63
63
progress += 1
64
64
task .set_progress ((progress / total_items )* 100 , socket = socket )
65
65
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 )
72
72
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 )
74
76
num_annotations = 0
75
77
for annotation in annotations :
76
78
77
79
has_keypoints = len (annotation .get ('keypoints' , [])) > 0
78
80
has_segmentation = len (annotation .get ('segmentation' , [])) > 0
79
81
80
82
if has_keypoints or has_segmentation :
81
- del annotation ['deleted' ]
82
83
83
84
if not has_keypoints :
84
85
if 'keypoints' in annotation :
@@ -91,16 +92,8 @@ def export_annotations(task_id, dataset_id):
91
92
num_annotations += 1
92
93
coco .get ('annotations' ).append (annotation )
93
94
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' )} " )
99
96
coco .get ('images' ).append (image )
100
- total_images += 1
101
-
102
- progress += 1
103
- task .set_progress ((progress / total_items )* 100 , socket = socket )
104
97
105
98
file_path = dataset .get ('directory' ) + '/coco.json'
106
99
with open (file_path , 'w' ) as fp :
0 commit comments