@@ -1189,7 +1189,6 @@ def run_child_validation(self, data):
1189
1189
1190
1190
raise exceptions .ValidationError (errors )
1191
1191
1192
-
1193
1192
class ShapeSerializer (serializers .Serializer ):
1194
1193
type = serializers .ChoiceField (choices = models .ShapeType .choices ())
1195
1194
occluded = serializers .BooleanField (default = False )
@@ -1207,6 +1206,63 @@ class SubLabeledShapeSerializer(ShapeSerializer, AnnotationSerializer):
1207
1206
class LabeledShapeSerializer (SubLabeledShapeSerializer ):
1208
1207
elements = SubLabeledShapeSerializer (many = True , required = False )
1209
1208
1209
+ def _convert_annotation (obj , keys ):
1210
+ return OrderedDict ([(key , obj [key ]) for key in keys ])
1211
+
1212
+ def _convert_attributes (attr_set ):
1213
+ attr_keys = ['spec_id' , 'value' ]
1214
+ return [
1215
+ OrderedDict ([(key , attr [key ]) for key in attr_keys ]) for attr in attr_set
1216
+ ]
1217
+
1218
+ class LabeledImageSerializerFromDB (serializers .BaseSerializer ):
1219
+ # Use this serializer to export data from the database
1220
+ # Because default DRF serializer is too slow on huge collections
1221
+ def to_representation (self , instance ):
1222
+ def convert_tag (tag ):
1223
+ result = _convert_annotation (tag , ['id' , 'label_id' , 'frame' , 'group' , 'source' ])
1224
+ result ['attributes' ] = _convert_attributes (tag ['labeledimageattributeval_set' ])
1225
+ return result
1226
+
1227
+ return convert_tag (instance )
1228
+
1229
+ class LabeledShapeSerializerFromDB (serializers .BaseSerializer ):
1230
+ # Use this serializer to export data from the database
1231
+ # Because default DRF serializer is too slow on huge collections
1232
+ def to_representation (self , instance ):
1233
+ def convert_shape (shape ):
1234
+ result = _convert_annotation (shape , [
1235
+ 'id' , 'label_id' , 'type' , 'frame' , 'group' , 'source' ,
1236
+ 'occluded' , 'outside' , 'z_order' , 'rotation' , 'points' ,
1237
+ ])
1238
+ result ['attributes' ] = _convert_attributes (shape ['labeledshapeattributeval_set' ])
1239
+ if shape .get ('elements' , None ) is not None and shape ['parent' ] is None :
1240
+ result ['elements' ] = [convert_shape (element ) for element in shape ['elements' ]]
1241
+ return result
1242
+
1243
+ return convert_shape (instance )
1244
+
1245
+ class LabeledTrackSerializerFromDB (serializers .BaseSerializer ):
1246
+ # Use this serializer to export data from the database
1247
+ # Because default DRF serializer is too slow on huge collections
1248
+ def to_representation (self , instance ):
1249
+ def convert_track (track ):
1250
+ shape_keys = [
1251
+ 'id' , 'type' , 'frame' , 'occluded' , 'outside' , 'z_order' ,
1252
+ 'rotation' , 'points' , 'trackedshapeattributeval_set' ,
1253
+ ]
1254
+ result = _convert_annotation (track , ['id' , 'label_id' , 'frame' , 'group' , 'source' ])
1255
+ result ['shapes' ] = [_convert_annotation (shape , shape_keys ) for shape in track ['trackedshape_set' ]]
1256
+ result ['attributes' ] = _convert_attributes (track ['labeledtrackattributeval_set' ])
1257
+ for shape in result ['shapes' ]:
1258
+ shape ['attributes' ] = _convert_attributes (shape ['trackedshapeattributeval_set' ])
1259
+ shape .pop ('trackedshapeattributeval_set' , None )
1260
+ if track .get ('elements' , None ) is not None and track ['parent' ] is None :
1261
+ result ['elements' ] = [convert_track (element ) for element in track ['elements' ]]
1262
+ return result
1263
+
1264
+ return convert_track (instance )
1265
+
1210
1266
class TrackedShapeSerializer (ShapeSerializer ):
1211
1267
id = serializers .IntegerField (default = None , allow_null = True )
1212
1268
frame = serializers .IntegerField (min_value = 0 )
0 commit comments