77from lib .core .entities .utils import AnnotationStatusEnum
88from lib .core .entities .utils import Attribute
99from lib .core .entities .utils import BaseInstance
10+ from lib .core .entities .utils import BaseModel
1011from lib .core .entities .utils import BboxPoints
1112from lib .core .entities .utils import MetadataBase
1213from lib .core .entities .utils import NotEmptyStr
1314from lib .core .entities .utils import PointLabels
1415from lib .core .entities .utils import Tag
15- from pydantic import BaseModel
1616from pydantic import conlist
1717from pydantic import Field
18+ from pydantic import validator
1819
1920
2021class VideoType (str , Enum ):
@@ -24,15 +25,15 @@ class VideoType(str, Enum):
2425
2526class MetaData (MetadataBase ):
2627 name : NotEmptyStr
27- url : str
28+ url : Optional [ str ]
2829 status : Optional [AnnotationStatusEnum ]
2930 duration : Optional [int ]
3031 error : Optional [bool ]
3132
3233
3334class BaseTimeStamp (BaseModel ):
3435 timestamp : int
35- attributes : List [Attribute ] # TODO check is it required
36+ attributes : List [Attribute ]
3637
3738
3839class BboxTimeStamp (BaseTimeStamp ):
@@ -55,11 +56,11 @@ class Config:
5556
5657
5758class BBoxInstanceMetadata (InstanceMetadata ):
58- type : str = Field (VideoType .BBOX , const = True )
59+ type : VideoType = Field (VideoType .BBOX . value , const = True )
5960
6061
6162class EventInstanceMetadata (InstanceMetadata ):
62- type : str = Field (VideoType .EVENT , const = True )
63+ type : VideoType = Field (VideoType .EVENT . value , const = True )
6364
6465
6566class BaseVideoInstance (BaseModel ):
@@ -93,10 +94,34 @@ class EventInstance(BaseModel):
9394 parameters : conlist (EventParameter , min_items = 1 )
9495
9596
96- ANNOTATION_TYPES = {VideoType .BBOX : BboxInstance , VideoType .EVENT : EventInstance }
97+ INSTANCES = {
98+ VideoType .BBOX .value : BboxInstance ,
99+ VideoType .EVENT .value : EventInstance
100+ }
101+
102+
103+ class VideoInstance (BaseModel ):
104+ __root__ : Union [BboxInstance , EventInstance ]
105+
106+ @classmethod
107+ def __get_validators__ (cls ):
108+ yield cls .return_action
109+
110+ @classmethod
111+ def return_action (cls , values ):
112+ try :
113+ instance_type = values ["meta" ]["type" ]
114+ except KeyError :
115+ raise ValueError (
116+ f"meta.type required"
117+ )
118+ try :
119+ return INSTANCES [instance_type ](** values )
120+ except KeyError :
121+ raise ValueError (f"invalid type, valid types is { ', ' .join (INSTANCES .keys ())} " )
97122
98123
99124class VideoAnnotation (BaseModel ):
100125 metadata : MetaData
101- instances : Optional [List [Union [ EventInstance , BboxInstance ] ]] = Field (list ())
126+ instances : Optional [List [VideoInstance ]] = Field (list ())
102127 tags : Optional [List [Tag ]] = Field (list ())
0 commit comments