1- import math
21from collections import defaultdict
32from typing import Any
43from typing import Dict
@@ -65,7 +64,7 @@ def interpolate_annotations(
6564 ):
6665 for idx , frame_idx in enumerate (range (from_frame , to_frame ), 1 ):
6766 keyframe = False
68- if idx == 1 :
67+ if idx in ( 1 , len ( range ( from_frame , to_frame ))) :
6968 keyframe = True
7069 points = None
7170 if annotation_type == "bbox" :
@@ -75,75 +74,96 @@ def interpolate_annotations(
7574 "x2" : round (data ["points" ]["x2" ] + steps ["x2" ] * idx , 2 ),
7675 "y2" : round (data ["points" ]["y2" ] + steps ["y2" ] * idx , 2 ),
7776 }
78- frame = self .get_frame ( frame_idx )
79- frame . annotations . append ( Annotation (
80- type = annotation_type ,
81- className = class_name ,
77+ self ._add_annotation (
78+ frame_idx ,
79+ annotation_type = annotation_type ,
80+ class_name = class_name ,
8281 points = points ,
8382 attributes = data ["attributes" ],
8483 keyframe = keyframe
85- ))
84+ )
85+
86+ def _add_annotation (
87+ self ,
88+ frame_no : int ,
89+ annotation_type : str ,
90+ class_name : str ,
91+ points : list = None ,
92+ attributes : list = None ,
93+ keyframe : bool = False
94+ ):
95+ frame = self .get_frame (frame_no )
96+ frame .annotations .append (Annotation (
97+ type = annotation_type ,
98+ className = class_name ,
99+ points = points ,
100+ attributes = attributes ,
101+ keyframe = keyframe
102+ ))
86103
87104 def _process (self ):
88105 for instance in self ._annotation_data ["instances" ]:
89- for parameter in instance ["parameters" ]:
90- time_stamp_frame_map = []
91- for timestamp in parameter ["timestamps" ]:
92- time_stamp_frame_map .append ((int (math .ceil (timestamp ["timestamp" ] / self .ratio )), timestamp ))
93- for idx , (frame_no , timestamp_data ) in enumerate (time_stamp_frame_map ):
94- annotation_type = instance ["meta" ]["type" ]
95- try :
96- next_frame_no , next_timestamp = time_stamp_frame_map [idx + 1 ]
97- if frame_no == next_frame_no :
98- median = (timestamp_data ["timestamp" ] // self .ratio ) + (self .ratio / 2 )
99- if abs (median - timestamp_data ["timestamp" ]) < abs (median - next_timestamp ["timestamp" ]):
100- time_stamp_frame_map [idx + 1 ] = (frame_no , timestamp_data )
101- continue
102-
103- frames_diff = next_frame_no - frame_no
104- steps = None
105- if annotation_type == "bbox" :
106- if not frames_diff :
107- steps = {
108- "y1" : 0 ,
109- "x2" : 0 ,
110- "x1" : 0 ,
111- "y2" : 0
112- }
113- else :
114- steps = {
115- "y1" : round (
116- (next_timestamp ["points" ]["y1" ] - timestamp_data ["points" ]["y1" ]) / frames_diff ,
117- 2 ),
118- "x2" : round (
119- (next_timestamp ["points" ]["x2" ] - timestamp_data ["points" ]["x2" ]) / frames_diff ,
120- 2 ),
121- "x1" : round (
122- (next_timestamp ["points" ]["x1" ] - timestamp_data ["points" ]["x1" ]) / frames_diff ,
123- 2 ),
124- "y2" : round (
125- (next_timestamp ["points" ]["y2" ] - timestamp_data ["points" ]["y2" ]) / frames_diff ,
126- 2 ),
127- }
128- self .interpolate_annotations (
129- class_name = instance ["meta" ]["className" ],
130- from_frame = frame_no ,
131- to_frame = next_frame_no ,
132- data = timestamp_data ,
133- steps = steps ,
134- annotation_type = annotation_type
135- )
136- except IndexError :
137- # print(frame_no)
138- frame = self .get_frame (frame_no )
139- points = timestamp_data .get ("points" )
140- frame .annotations .append (Annotation (
141- type = annotation_type ,
142- className = instance ["meta" ]["className" ],
143- points = points ,
144- attributes = timestamp_data .get ("attributes" ),
145- keyframe = True
146- ))
106+ try :
107+ for parameter in instance ["parameters" ]:
108+ time_stamp_frame_map = []
109+ for timestamp in parameter ["timestamps" ]:
110+ time_stamp_frame_map .append ((round (timestamp ["timestamp" ] / self .ratio ) + 1 , timestamp ))
111+ for idx , (frame_no , timestamp_data ) in enumerate (time_stamp_frame_map ):
112+ annotation_type = instance ["meta" ]["type" ]
113+ try :
114+ next_frame_no , next_timestamp = time_stamp_frame_map [idx + 1 ]
115+ if frame_no == next_frame_no :
116+ median = (timestamp_data ["timestamp" ] // self .ratio ) + (self .ratio / 2 )
117+ if abs (median - timestamp_data ["timestamp" ]) < abs (median - next_timestamp ["timestamp" ]):
118+ time_stamp_frame_map [idx + 1 ] = timestamp_data
119+ continue
120+
121+ frames_diff = next_frame_no - frame_no
122+ steps = None
123+ if annotation_type == "bbox" :
124+ if not frames_diff :
125+ steps = {
126+ "y1" : 0 ,
127+ "x2" : 0 ,
128+ "x1" : 0 ,
129+ "y2" : 0
130+ }
131+ else :
132+ steps = {
133+ "y1" : round (
134+ (next_timestamp ["points" ]["y1" ] - timestamp_data ["points" ]["y1" ]) / frames_diff ,
135+ 2 ),
136+ "x2" : round (
137+ (next_timestamp ["points" ]["x2" ] - timestamp_data ["points" ]["x2" ]) / frames_diff ,
138+ 2 ),
139+ "x1" : round (
140+ (next_timestamp ["points" ]["x1" ] - timestamp_data ["points" ]["x1" ]) / frames_diff ,
141+ 2 ),
142+ "y2" : round (
143+ (next_timestamp ["points" ]["y2" ] - timestamp_data ["points" ]["y2" ]) / frames_diff ,
144+ 2 ),
145+ }
146+ self .interpolate_annotations (
147+ class_name = instance ["meta" ]["className" ],
148+ from_frame = frame_no ,
149+ to_frame = next_frame_no ,
150+ data = timestamp_data ,
151+ steps = steps ,
152+ annotation_type = annotation_type
153+ )
154+ except IndexError :
155+ last_frame_no , last_timestamp = time_stamp_frame_map [- 1 ]
156+ end = round (parameter ["end" ] / self .ratio )
157+ self .interpolate_annotations (
158+ annotation_type = annotation_type ,
159+ class_name = instance ["meta" ]["className" ],
160+ from_frame = last_frame_no ,
161+ to_frame = end ,
162+ data = last_timestamp ,
163+ steps = {"x1" : 0 , "y1" : 0 , "x2" : 0 , "y2" : 0 }
164+ )
165+ except Exception as e :
166+ pass
147167
148168 def __iter__ (self ):
149169 for frame_no in range (1 , int (self .frames_count ) + 1 ):
0 commit comments