|
| 1 | +import math |
1 | 2 | from collections import defaultdict |
2 | 3 | from typing import Any |
3 | 4 | from typing import Dict |
@@ -64,7 +65,7 @@ def interpolate_annotations( |
64 | 65 | ): |
65 | 66 | for idx, frame_idx in enumerate(range(from_frame, to_frame), 1): |
66 | 67 | keyframe = False |
67 | | - if idx in (1, len(range(from_frame, to_frame))): |
| 68 | + if idx == 1: |
68 | 69 | keyframe = True |
69 | 70 | points = None |
70 | 71 | if annotation_type == "bbox": |
@@ -103,67 +104,75 @@ def _add_annotation( |
103 | 104 |
|
104 | 105 | def _process(self): |
105 | 106 | for instance in self._annotation_data["instances"]: |
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 |
| 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 | + )) |
167 | 176 |
|
168 | 177 | def __iter__(self): |
169 | 178 | for frame_no in range(1, int(self.frames_count) + 1): |
|
0 commit comments