@@ -114,24 +114,40 @@ def get_image_coco(image):
114114 annotations = []
115115
116116 for category in bulk_categories :
117-
117+ category = category [ 1 ]
118118 category_annotations = AnnotationModel .objects (
119- deleted = False , category_id = category [ 1 ] .id , image_id = image .get ('id' )
119+ deleted = False , category_id = category .id , image_id = image .get ('id' )
120120 ).exclude ('paper_object' , 'deleted_date' ).all ()
121-
121+
122122 if len (category_annotations ) == 0 :
123123 continue
124+
125+ has_keypoints = len (category .keypoint_labels ) > 0
124126
125127 for annotation in category_annotations :
126128 annotation = fix_ids (annotation )
127129
128- if len (annotation .get ('segmentation' )) != 0 :
130+ if len (annotation .get ('segmentation' , [])) != 0 or \
131+ len (annotation .get ('keypoints' , [])) != 0 :
129132 del annotation ['deleted' ]
130- del annotation ['paper_object' ]
133+
134+ if not has_keypoints :
135+ del annotation ['keypoints' ]
136+ else :
137+ arr = np .array (annotation .get ('keypoints' , []))
138+ arr = arr [2 ::3 ]
139+ annotation ['num_keypoints' ] = len (arr [arr > 0 ])
140+
131141 annotations .append (annotation )
132142
133- category = fix_ids (category [ 1 ] )
143+ category = fix_ids (category )
134144 del category ['deleted' ]
145+ if has_keypoints :
146+ category ['keypoints' ] = category .pop ('keypoint_labels' )
147+ category ['skeleton' ] = category .pop ('keypoint_edges' )
148+ else :
149+ del category ['keypoint_edges' ]
150+ del category ['keypoint_labels' ]
135151 categories .append (category )
136152
137153 del image ['deleted' ]
@@ -169,7 +185,15 @@ def get_dataset_coco(dataset):
169185
170186 for category in categories :
171187 category = fix_ids (category [1 ])
188+
172189 del category ['deleted' ]
190+ if len (category .keypoint_labels ) > 0 :
191+ category ['keypoints' ] = category .pop ('keypoint_labels' )
192+ category ['skeleton' ] = category .pop ('keypoint_edges' )
193+ else :
194+ del category ['keypoint_edges' ]
195+ del category ['keypoint_labels' ]
196+
173197 coco .get ('categories' ).append (category )
174198
175199 for image in images :
@@ -180,8 +204,19 @@ def get_dataset_coco(dataset):
180204 annotations = fix_ids (annotations .all ())
181205
182206 for annotation in annotations :
183- if len (annotation .get ('segmentation' , [])) != 0 :
207+
208+ has_keypoints = len (annotation .get ('keypoints' , [])) > 0
209+ has_segmentation = len (annotation .get ('segmentation' , [])) > 0
210+
211+ if has_keypoints or has_keypoints :
184212 del annotation ['deleted' ]
213+
214+ if not has_keypoints :
215+ del annotation ['keypoints' ]
216+ else :
217+ arr = np .array (annotation .get ('keypoints' , []))
218+ arr = arr [2 ::3 ]
219+ annotation ['num_keypoints' ] = len (arr [arr > 0 ])
185220 coco .get ('annotations' ).append (annotation )
186221
187222 image = fix_ids (image )
@@ -192,11 +227,4 @@ def get_dataset_coco(dataset):
192227
193228
194229def _fit (value , max_value , min_value ):
195-
196- if value > max_value :
197- return max_value
198-
199- if value < min_value :
200- return min_value
201-
202- return value
230+ return max (min (value , max_value ), min_value )
0 commit comments