@@ -268,7 +268,9 @@ def create_tracks(tracks, parent_track=None):
268
268
self ._validate_label_for_existence (db_track .label_id )
269
269
270
270
for attr in track_attributes :
271
- db_attr_val = models .LabeledTrackAttributeVal (** attr , track_id = len (db_tracks ))
271
+ db_attr_val = models .LabeledTrackAttributeVal (
272
+ ** attr , job_id = self .db_job .id , track_id = len (db_tracks )
273
+ )
272
274
273
275
self ._validate_attribute_for_existence (
274
276
db_attr_val , db_track .label_id , "immutable"
@@ -282,7 +284,9 @@ def create_tracks(tracks, parent_track=None):
282
284
283
285
for attr in shape_attributes :
284
286
db_attr_val = models .TrackedShapeAttributeVal (
285
- ** attr , shape_id = len (db_shapes )
287
+ ** attr ,
288
+ shape_id = len (db_shapes ),
289
+ job_id = self .db_job .id ,
286
290
)
287
291
288
292
self ._validate_attribute_for_existence (
@@ -345,7 +349,9 @@ def create_shapes(shapes, parent_shape=None):
345
349
self ._validate_label_for_existence (db_shape .label_id )
346
350
347
351
for attr in attributes :
348
- db_attr_val = models .LabeledShapeAttributeVal (** attr , shape_id = len (db_shapes ))
352
+ db_attr_val = models .LabeledShapeAttributeVal (
353
+ ** attr , job_id = self .db_job .id , shape_id = len (db_shapes )
354
+ )
349
355
350
356
self ._validate_attribute_for_existence (db_attr_val , db_shape .label_id , "all" )
351
357
@@ -382,7 +388,7 @@ def _save_tags_to_db(self, tags):
382
388
self ._validate_label_for_existence (db_tag .label_id )
383
389
384
390
for attr in attributes :
385
- db_attr_val = models .LabeledImageAttributeVal (** attr )
391
+ db_attr_val = models .LabeledImageAttributeVal (** attr , job_id = self . db_job . id )
386
392
387
393
self ._validate_attribute_for_existence (db_attr_val , db_tag .label_id , "all" )
388
394
@@ -574,35 +580,39 @@ def _extend_attributes(attributeval_set, default_attribute_values):
574
580
)
575
581
576
582
def _init_tags_from_db (self ):
577
- # NOTE: do not use .prefetch_related() with .values() since it's useless:
578
- # https://github.com/cvat-ai/cvat/pull/7748#issuecomment-2063695007
579
- db_tags = (
580
- self .db_job .labeledimage_set .values (
583
+ db_tags = {
584
+ row ["id" ]: dotdict (row , attributes = [])
585
+ for row in self .db_job .labeledimage_set .values (
581
586
"id" ,
582
587
"frame" ,
583
588
"label_id" ,
584
589
"group" ,
585
590
"source" ,
586
- "attribute__spec_id" ,
587
- "attribute__value" ,
588
- "attribute__id" ,
589
591
)
590
592
.order_by ("frame" )
591
593
.iterator (chunk_size = 2000 )
592
- )
594
+ }
593
595
594
- db_tags = merge_table_rows (
595
- rows = db_tags ,
596
- keys_for_merge = {
597
- "attributes" : [
598
- "attribute__spec_id" ,
599
- "attribute__value" ,
600
- "attribute__id" ,
601
- ],
602
- },
603
- field_id = "id" ,
604
- )
596
+ for attr in self .db_job .labeledimageattributeval_set .values (
597
+ "image_id" ,
598
+ "spec_id" ,
599
+ "value" ,
600
+ "id" ,
601
+ ).iterator (chunk_size = 2000 ):
602
+ if attr ["image_id" ] not in db_tags :
603
+ continue
604
+
605
+ db_tags [attr ["image_id" ]]["attributes" ].append (
606
+ dotdict (
607
+ {
608
+ "id" : attr ["id" ],
609
+ "spec_id" : attr ["spec_id" ],
610
+ "value" : attr ["value" ],
611
+ }
612
+ )
613
+ )
605
614
615
+ db_tags = list (db_tags .values ())
606
616
for db_tag in db_tags :
607
617
self ._extend_attributes (
608
618
db_tag .attributes , self .db_attributes [db_tag .label_id ]["all" ].values ()
@@ -612,10 +622,9 @@ def _init_tags_from_db(self):
612
622
self .ir_data .tags = serializer .data
613
623
614
624
def _init_shapes_from_db (self ):
615
- # NOTE: do not use .prefetch_related() with .values() since it's useless:
616
- # https://github.com/cvat-ai/cvat/pull/7748#issuecomment-2063695007
617
- db_shapes = (
618
- self .db_job .labeledshape_set .values (
625
+ db_shapes = {
626
+ row ["id" ]: dotdict (row , attributes = [])
627
+ for row in self .db_job .labeledshape_set .values (
619
628
"id" ,
620
629
"label_id" ,
621
630
"type" ,
@@ -628,29 +637,33 @@ def _init_shapes_from_db(self):
628
637
"rotation" ,
629
638
"points" ,
630
639
"parent" ,
631
- "attribute__spec_id" ,
632
- "attribute__value" ,
633
- "attribute__id" ,
634
640
)
635
641
.order_by ("frame" )
636
642
.iterator (chunk_size = 2000 )
637
- )
643
+ }
638
644
639
- db_shapes = merge_table_rows (
640
- rows = db_shapes ,
641
- keys_for_merge = {
642
- "attributes" : [
643
- "attribute__spec_id" ,
644
- "attribute__value" ,
645
- "attribute__id" ,
646
- ],
647
- },
648
- field_id = "id" ,
649
- )
645
+ for attr in self .db_job .labeledshapeattributeval_set .values (
646
+ "shape_id" ,
647
+ "spec_id" ,
648
+ "value" ,
649
+ "id" ,
650
+ ).iterator (chunk_size = 2000 ):
651
+ if attr ["shape_id" ] not in db_shapes :
652
+ continue
653
+
654
+ db_shapes [attr ["shape_id" ]]["attributes" ].append (
655
+ dotdict (
656
+ {
657
+ "id" : attr ["id" ],
658
+ "spec_id" : attr ["spec_id" ],
659
+ "value" : attr ["value" ],
660
+ }
661
+ )
662
+ )
650
663
651
664
shapes = {}
652
665
elements = {}
653
- for db_shape in db_shapes :
666
+ for db_shape in db_shapes . values () :
654
667
self ._extend_attributes (
655
668
db_shape .attributes , self .db_attributes [db_shape .label_id ]["all" ].values ()
656
669
)
0 commit comments