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" :
@@ -85,67 +86,64 @@ def interpolate_annotations(
8586
8687 def _process (self ):
8788 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
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+ ))
149147
150148 def __iter__ (self ):
151149 for frame_no in range (1 , int (self .frames_count ) + 1 ):
0 commit comments