@@ -36,7 +36,7 @@ def __init__(self, annotation_data: dict, fps: int):
3636 self .fps = fps
3737 self .ratio = 1000 * 1000 / fps
3838 self ._frame_id = 1
39- self .frames_count = int (self .duration * fps )
39+ self .frames_count = int (math . ceil ( self .duration * fps ) )
4040 self .annotations : dict = {}
4141 self ._mapping = {}
4242 self ._process ()
@@ -107,7 +107,7 @@ def get_median(self, annotations: List[dict]) -> dict:
107107 if len (annotations ) == 1 :
108108 return annotations [0 ]
109109 first_annotations = annotations [:1 ][0 ]
110- median = (first_annotations ["timestamp" ] // self .ratio ) + ( self .ratio / 2 )
110+ median = (first_annotations ["timestamp" ] // self .ratio ) * self . ratio + self .ratio / 2
111111 median_annotation = first_annotations
112112 distance = abs (median - first_annotations ["timestamp" ])
113113 for annotation in annotations [1 :]:
@@ -163,8 +163,6 @@ def _process(self):
163163 class_name = instance ["meta" ].get ("className" )
164164 for parameter in instance ["parameters" ]:
165165 frames_mapping = defaultdict (list )
166- last_frame_no = None
167- last_annotation = None
168166 interpolated_frames = {}
169167 for timestamp in parameter ["timestamps" ]:
170168 frames_mapping [
@@ -190,33 +188,35 @@ def _process(self):
190188 instance_id = instance_id
191189 )
192190 )
191+
193192 start_median_frame = self .get_median (frames_mapping [from_frame_no ])
194193 end_median_frame = self .get_median (frames_mapping [to_frame_no ])
195- interpolated_frames [from_frame_no ] = Annotation (
196- instanceId = instance_id ,
197- type = annotation_type ,
198- className = class_name ,
199- x = start_median_frame .get ("x" ),
200- y = start_median_frame .get ("y" ),
201- points = start_median_frame .get ("points" ),
202- attributes = start_median_frame ["attributes" ],
203- keyframe = True ,
204- )
205- last_annotation = Annotation (
194+ for frame_no , frame in (from_frame_no , start_median_frame ), (last_frame_no , end_median_frame ):
195+ interpolated_frames [frame_no ] = Annotation (
196+ instanceId = instance_id ,
197+ type = annotation_type ,
198+ className = class_name ,
199+ x = frame .get ("x" ),
200+ y = frame .get ("y" ),
201+ points = frame .get ("points" ),
202+ attributes = frame ["attributes" ],
203+ keyframe = True ,
204+ )
205+ if frames_mapping and not interpolated_frames :
206+ median = self .get_median (frames_mapping [1 ])
207+ interpolated_frames [1 ] = Annotation (
206208 instanceId = instance_id ,
207209 type = annotation_type ,
208210 className = class_name ,
209- x = start_median_frame .get ("x" ),
210- y = start_median_frame .get ("y" ),
211- points = end_median_frame .get ("points" ),
212- attributes = end_median_frame ["attributes" ],
211+ x = median .get ("x" ),
212+ y = median .get ("y" ),
213+ points = median .get ("points" ),
214+ attributes = median ["attributes" ],
213215 keyframe = True ,
214216 )
215- self . _add_annotation ( last_frame_no , last_annotation )
216- [
217+
218+ for frame_no , annotation in interpolated_frames . items ():
217219 self ._add_annotation (frame_no , annotation )
218- for frame_no , annotation in interpolated_frames .items ()
219- ]
220220
221221 def __iter__ (self ):
222222 for frame_no in range (1 , int (self .frames_count ) + 1 ):
0 commit comments