1+ import math
12from collections import defaultdict
23from typing import Any
34from typing import Dict
@@ -64,7 +65,7 @@ def interpolate_annotations(
6465 ):
6566 for idx , frame_idx in enumerate (range (from_frame , to_frame ), 1 ):
6667 keyframe = False
67- if idx in ( 1 , len ( range ( from_frame , to_frame ))) :
68+ if idx == 1 :
6869 keyframe = True
6970 points = None
7071 if annotation_type == "bbox" :
@@ -74,78 +75,104 @@ def interpolate_annotations(
7475 "x2" : round (data ["points" ]["x2" ] + steps ["x2" ] * idx , 2 ),
7576 "y2" : round (data ["points" ]["y2" ] + steps ["y2" ] * idx , 2 ),
7677 }
77- frame = self .get_frame ( frame_idx )
78- frame . annotations . append ( Annotation (
79- type = annotation_type ,
80- className = class_name ,
78+ self ._add_annotation (
79+ frame_idx ,
80+ annotation_type = annotation_type ,
81+ class_name = class_name ,
8182 points = points ,
8283 attributes = data ["attributes" ],
8384 keyframe = keyframe
84- ))
85+ )
86+
87+ def _add_annotation (
88+ self ,
89+ frame_no : int ,
90+ annotation_type : str ,
91+ class_name : str ,
92+ points : list = None ,
93+ attributes : list = None ,
94+ keyframe : bool = False
95+ ):
96+ frame = self .get_frame (frame_no )
97+ frame .annotations .append (Annotation (
98+ type = annotation_type ,
99+ className = class_name ,
100+ points = points ,
101+ attributes = attributes ,
102+ keyframe = keyframe
103+ ))
85104
86105 def _process (self ):
87106 for instance in self ._annotation_data ["instances" ]:
88- try :
89- for parameter in instance ["parameters" ]:
90- time_stamp_frame_map = []
91- for timestamp in parameter ["timestamps" ]:
92- time_stamp_frame_map .append ((round (timestamp ["timestamp" ] / self .ratio ) + 1 , 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 ] = 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- last_frame_no , last_timestamp = time_stamp_frame_map [- 1 ]
138- end = round (parameter ["end" ] / self .ratio )
139- self .interpolate_annotations (
140- annotation_type = annotation_type ,
141- class_name = instance ["meta" ]["className" ],
142- from_frame = last_frame_no ,
143- to_frame = end ,
144- data = last_timestamp ,
145- steps = {"x1" : 0 , "y1" : 0 , "x2" : 0 , "y2" : 0 }
146- )
147- except Exception as e :
148- pass
107+ for parameter in instance ["parameters" ]:
108+ time_stamp_frame_map = []
109+ for timestamp in parameter ["timestamps" ]:
110+ time_stamp_frame_map .append ((int (math .ceil (timestamp ["timestamp" ] / self .ratio )), timestamp ))
111+ skip_current = False
112+ for idx , (frame_no , timestamp_data ) in enumerate (time_stamp_frame_map ):
113+ annotation_type = instance ["meta" ]["type" ]
114+ try :
115+ next_frame_no , next_timestamp = time_stamp_frame_map [idx + 1 ]
116+ if frame_no == next_frame_no :
117+ median = (timestamp_data ["timestamp" ] // self .ratio ) + (self .ratio / 2 )
118+ if abs (median - timestamp_data ["timestamp" ]) < abs (
119+ median - next_timestamp ["timestamp" ]) and not skip_current :
120+ time_stamp_frame_map [idx + 1 ] = (frame_no , timestamp_data )
121+ self ._add_annotation (
122+ frame_no = frame_no ,
123+ annotation_type = annotation_type ,
124+ class_name = instance ["meta" ]["className" ],
125+ points = timestamp_data .get ("points" ),
126+ attributes = timestamp_data .get ("attributes" ),
127+ keyframe = True
128+ )
129+ skip_current = True
130+ elif skip_current :
131+ frame_no += 1
132+ skip_current = False
133+
134+ frames_diff = next_frame_no - frame_no
135+ steps = None
136+ if annotation_type == "bbox" :
137+ if not frames_diff :
138+ steps = {
139+ "y1" : 0 ,
140+ "x2" : 0 ,
141+ "x1" : 0 ,
142+ "y2" : 0
143+ }
144+ else :
145+ steps = {
146+ "y1" : round (
147+ (next_timestamp ["points" ]["y1" ] - timestamp_data ["points" ]["y1" ]) / frames_diff ,
148+ 2 ),
149+ "x2" : round (
150+ (next_timestamp ["points" ]["x2" ] - timestamp_data ["points" ]["x2" ]) / frames_diff ,
151+ 2 ),
152+ "x1" : round (
153+ (next_timestamp ["points" ]["x1" ] - timestamp_data ["points" ]["x1" ]) / frames_diff ,
154+ 2 ),
155+ "y2" : round (
156+ (next_timestamp ["points" ]["y2" ] - timestamp_data ["points" ]["y2" ]) / frames_diff ,
157+ 2 ),
158+ }
159+ self .interpolate_annotations (
160+ class_name = instance ["meta" ]["className" ],
161+ from_frame = frame_no ,
162+ to_frame = next_frame_no ,
163+ data = timestamp_data ,
164+ steps = steps ,
165+ annotation_type = annotation_type
166+ )
167+ except IndexError :
168+ frame = self .get_frame (frame_no )
169+ frame .annotations .append (Annotation (
170+ type = annotation_type ,
171+ className = instance ["meta" ]["className" ],
172+ points = timestamp_data .get ("points" ),
173+ attributes = timestamp_data .get ("attributes" ),
174+ keyframe = True
175+ ))
149176
150177 def __iter__ (self ):
151178 for frame_no in range (1 , int (self .frames_count ) + 1 ):
0 commit comments