@@ -64,7 +64,7 @@ def interpolate_annotations(
6464 ):
6565 for idx , frame_idx in enumerate (range (from_frame , to_frame ), 1 ):
6666 keyframe = False
67- if idx == from_frame or idx - 1 == to_frame :
67+ if idx in ( 1 , len ( range ( from_frame , to_frame ))) :
6868 keyframe = True
6969 points = None
7070 if annotation_type == "bbox" :
@@ -85,60 +85,67 @@ def interpolate_annotations(
8585
8686 def _process (self ):
8787 for instance in self ._annotation_data ["instances" ]:
88- for parameter in instance ["parameters" ]:
89- time_stamp_frame_map = []
90- for timestamp in parameter ["timestamps" ]:
91- time_stamp_frame_map .append ((round (timestamp ["timestamp" ] / self .ratio ), timestamp ))
92- for idx , (frame_no , timestamp_data ) in enumerate (time_stamp_frame_map ):
93- annotation_type = instance ["meta" ]["type" ]
94- try :
95- next_frame_no , next_timestamp = time_stamp_frame_map [idx + 1 ]
96- if frame_no == next_frame_no :
97- continue
98- frames_diff = next_frame_no - frame_no
99- steps = None
100- if annotation_type == "bbox" :
101- if not frames_diff :
102- steps = {
103- "y1" : 0 ,
104- "x2" : 0 ,
105- "x1" : 0 ,
106- "y2" : 0
107- }
108- else :
109- steps = {
110- "y1" : round (
111- (next_timestamp ["points" ]["y1" ] - timestamp_data ["points" ]["y1" ]) / frames_diff ,
112- 2 ),
113- "x2" : round (
114- (next_timestamp ["points" ]["x2" ] - timestamp_data ["points" ]["x2" ]) / frames_diff ,
115- 2 ),
116- "x1" : round (
117- (next_timestamp ["points" ]["x1" ] - timestamp_data ["points" ]["x1" ]) / frames_diff ,
118- 2 ),
119- "y2" : round (
120- (next_timestamp ["points" ]["y2" ] - timestamp_data ["points" ]["y2" ]) / frames_diff ,
121- 2 ),
122- }
123- self .interpolate_annotations (
124- class_name = instance ["meta" ]["className" ],
125- from_frame = frame_no ,
126- to_frame = next_frame_no ,
127- data = timestamp_data ,
128- steps = steps ,
129- annotation_type = annotation_type
130- )
131- except IndexError :
132- last_frame_no , last_timestamp = time_stamp_frame_map [- 1 ]
133- end = round (parameter ["end" ] / self .ratio )
134- self .interpolate_annotations (
135- annotation_type = annotation_type ,
136- class_name = instance ["meta" ]["className" ],
137- from_frame = last_frame_no ,
138- to_frame = end ,
139- data = last_timestamp ,
140- steps = {"x1" : 0 , "y1" : 0 , "x2" : 0 , "y2" : 0 }
141- )
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
142149
143150 def __iter__ (self ):
144151 for frame_no in range (1 , int (self .frames_count ) + 1 ):
0 commit comments