Spaces:
Build error
Build error
Victoria Oberascher
commited on
Commit
·
8efc7e7
1
Parent(s):
bc89e29
test
Browse files- horizonmetrics.py +268 -1
horizonmetrics.py
CHANGED
@@ -16,7 +16,7 @@
|
|
16 |
import evaluate
|
17 |
import datasets
|
18 |
|
19 |
-
from seametrics.horizon.utils import *
|
20 |
|
21 |
# TODO: Add BibTeX citation
|
22 |
_CITATION = """\
|
@@ -59,6 +59,273 @@ BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
|
59 |
|
60 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION,
|
61 |
_KWARGS_DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
class horizonmetrics(evaluate.Metric):
|
63 |
"""TODO: Short description of my evaluation module."""
|
64 |
|
|
|
16 |
import evaluate
|
17 |
import datasets
|
18 |
|
19 |
+
#from seametrics.horizon.utils import *
|
20 |
|
21 |
# TODO: Add BibTeX citation
|
22 |
_CITATION = """\
|
|
|
59 |
|
60 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION,
|
61 |
_KWARGS_DESCRIPTION)
|
62 |
+
|
63 |
+
# begin utils
|
64 |
+
import numpy as np
|
65 |
+
|
66 |
+
|
67 |
+
def xy_points_to_slope_midpoint(xy_points):
|
68 |
+
"""
|
69 |
+
Given two points, return the slope and midpoint of the line
|
70 |
+
|
71 |
+
Args:
|
72 |
+
xy_points: list of two points, each point is a list of two elements
|
73 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
74 |
+
|
75 |
+
Returns:
|
76 |
+
slope: Slope of the line
|
77 |
+
midpoint : Midpoint is in the form of [x,y], and is also normalized to [0, 1]
|
78 |
+
"""
|
79 |
+
|
80 |
+
#x1, y1, x2, y2 = xy_points[0][0], xy_points[0][1], xy_points[1][
|
81 |
+
# 0], xy_points[1][1]
|
82 |
+
|
83 |
+
x1, y1, x2, y2 = xy_points[0], xy_points[1], xy_points[2], xy_points[3]
|
84 |
+
slope = (y2 - y1) / (x2 - x1)
|
85 |
+
|
86 |
+
midpoint_x = 0.5
|
87 |
+
midpoint_y = slope * (0.5 - x1) + y1
|
88 |
+
midpoint = [midpoint_x, midpoint_y]
|
89 |
+
return slope, midpoint
|
90 |
+
|
91 |
+
|
92 |
+
def calculate_horizon_error(annotated_horizon, proposed_horizon):
|
93 |
+
"""
|
94 |
+
Calculate the error between the annotated horizon and the proposed horizon
|
95 |
+
|
96 |
+
Args:
|
97 |
+
annotated_horizon: list of two points, each point is a list of two elements
|
98 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
99 |
+
proposed_horizon: list of two points, each point is a list of two elements
|
100 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
101 |
+
|
102 |
+
Returns:
|
103 |
+
slope_error: Error in the slope of the lines
|
104 |
+
midpoint_error: Error in the midpoint_y of the lines
|
105 |
+
"""
|
106 |
+
|
107 |
+
slope_annotated, midpoint_annotated = xy_points_to_slope_midpoint(
|
108 |
+
annotated_horizon)
|
109 |
+
slope_proposed, midpoint_proposed = xy_points_to_slope_midpoint(
|
110 |
+
proposed_horizon)
|
111 |
+
|
112 |
+
slope_error = abs(slope_annotated - slope_proposed)
|
113 |
+
midpoint_error = abs(midpoint_annotated[1] - midpoint_proposed[1])
|
114 |
+
|
115 |
+
return slope_error, midpoint_error
|
116 |
+
|
117 |
+
|
118 |
+
def calculate_horizon_error_across_sequence(slope_error_list,
|
119 |
+
midpoint_error_list,
|
120 |
+
slope_error_jump_threshold,
|
121 |
+
midpoint_error_jump_threshold):
|
122 |
+
"""
|
123 |
+
Calculate the error statistics across a sequence of frames
|
124 |
+
|
125 |
+
Args:
|
126 |
+
slope_error_list: List of errors in the slope of the lines
|
127 |
+
midpoint_error_list: List of errors in the midpoint_y of the lines
|
128 |
+
|
129 |
+
Returns:
|
130 |
+
average_slope_error: Average error in the slope of the lines
|
131 |
+
average_midpoint_error: Average error in the midpoint_y of the lines
|
132 |
+
"""
|
133 |
+
|
134 |
+
# Calculate the average and standard deviation of the errors
|
135 |
+
average_slope_error = np.mean(slope_error_list)
|
136 |
+
average_midpoint_error = np.mean(midpoint_error_list)
|
137 |
+
|
138 |
+
stddev_slope_error = np.std(slope_error_list)
|
139 |
+
stddev_midpoint_error = np.std(midpoint_error_list)
|
140 |
+
|
141 |
+
# Calculate the maximum errors
|
142 |
+
max_slope_error = np.max(slope_error_list)
|
143 |
+
max_midpoint_error = np.max(midpoint_error_list)
|
144 |
+
|
145 |
+
# Calculate the differences between errors in successive frames
|
146 |
+
diff_slope_error = np.abs(np.diff(slope_error_list))
|
147 |
+
diff_midpoint_error = np.abs(np.diff(midpoint_error_list))
|
148 |
+
|
149 |
+
# Calculate the number of jumps in the errors
|
150 |
+
num_slope_error_jumps = np.sum(
|
151 |
+
diff_slope_error > slope_error_jump_threshold)
|
152 |
+
num_midpoint_error_jumps = np.sum(
|
153 |
+
diff_midpoint_error > midpoint_error_jump_threshold)
|
154 |
+
|
155 |
+
# Create a dictionary to store the results
|
156 |
+
sequence_results = {
|
157 |
+
'average_slope_error': average_slope_error,
|
158 |
+
'average_midpoint_error': average_midpoint_error,
|
159 |
+
'stddev_slope_error': stddev_slope_error,
|
160 |
+
'stddev_midpoint_error': stddev_midpoint_error,
|
161 |
+
'max_slope_error': max_slope_error,
|
162 |
+
'max_midpoint_error': max_midpoint_error,
|
163 |
+
'num_slope_error_jumps': num_slope_error_jumps,
|
164 |
+
'num_midpoint_error_jumps': num_midpoint_error_jumps
|
165 |
+
}
|
166 |
+
|
167 |
+
return sequence_results
|
168 |
+
|
169 |
+
|
170 |
+
def xy_points_to_slope_midpoint(xy_points):
|
171 |
+
"""
|
172 |
+
Given two points, return the slope and midpoint of the line
|
173 |
+
|
174 |
+
Args:
|
175 |
+
xy_points: list of two points, each point is a list of two elements
|
176 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
177 |
+
|
178 |
+
Returns:
|
179 |
+
slope: Slope of the line
|
180 |
+
midpoint : Midpoint is in the form of [x,y], and is also normalized to [0, 1]
|
181 |
+
"""
|
182 |
+
|
183 |
+
x1, y1, x2, y2 = xy_points[0][0], xy_points[0][1], xy_points[1][
|
184 |
+
0], xy_points[1][1]
|
185 |
+
slope = (y2 - y1) / (x2 - x1)
|
186 |
+
|
187 |
+
midpoint_x = 0.5
|
188 |
+
midpoint_y = slope * (0.5 - x1) + y1
|
189 |
+
midpoint = [midpoint_x, midpoint_y]
|
190 |
+
return slope, midpoint
|
191 |
+
|
192 |
+
|
193 |
+
def calculate_horizon_error(annotated_horizon, proposed_horizon):
|
194 |
+
"""
|
195 |
+
Calculate the error between the annotated horizon and the proposed horizon
|
196 |
+
|
197 |
+
Args:
|
198 |
+
annotated_horizon: list of two points, each point is a list of two elements
|
199 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
200 |
+
proposed_horizon: list of two points, each point is a list of two elements
|
201 |
+
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
202 |
+
|
203 |
+
Returns:
|
204 |
+
slope_error: Error in the slope of the lines
|
205 |
+
midpoint_error: Error in the midpoint_y of the lines
|
206 |
+
"""
|
207 |
+
|
208 |
+
slope_annotated, midpoint_annotated = xy_points_to_slope_midpoint(
|
209 |
+
annotated_horizon)
|
210 |
+
slope_proposed, midpoint_proposed = xy_points_to_slope_midpoint(
|
211 |
+
proposed_horizon)
|
212 |
+
|
213 |
+
slope_error = abs(slope_annotated - slope_proposed)
|
214 |
+
midpoint_error = abs(midpoint_annotated[1] - midpoint_proposed[1])
|
215 |
+
|
216 |
+
return slope_error, midpoint_error
|
217 |
+
|
218 |
+
|
219 |
+
def calculate_horizon_error_across_sequence(slope_error_list,
|
220 |
+
midpoint_error_list,
|
221 |
+
slope_error_jump_threshold,
|
222 |
+
midpoint_error_jump_threshold):
|
223 |
+
"""
|
224 |
+
Calculate the error statistics across a sequence of frames
|
225 |
+
|
226 |
+
Args:
|
227 |
+
slope_error_list: List of errors in the slope of the lines
|
228 |
+
midpoint_error_list: List of errors in the midpoint_y of the lines
|
229 |
+
|
230 |
+
Returns:
|
231 |
+
average_slope_error: Average error in the slope of the lines
|
232 |
+
average_midpoint_error: Average error in the midpoint_y of the lines
|
233 |
+
"""
|
234 |
+
|
235 |
+
# Calculate the average and standard deviation of the errors
|
236 |
+
average_slope_error = np.mean(slope_error_list)
|
237 |
+
average_midpoint_error = np.mean(midpoint_error_list)
|
238 |
+
|
239 |
+
stddev_slope_error = np.std(slope_error_list)
|
240 |
+
stddev_midpoint_error = np.std(midpoint_error_list)
|
241 |
+
|
242 |
+
# Calculate the maximum errors
|
243 |
+
max_slope_error = np.max(slope_error_list)
|
244 |
+
max_midpoint_error = np.max(midpoint_error_list)
|
245 |
+
|
246 |
+
# Calculate the differences between errors in successive frames
|
247 |
+
diff_slope_error = np.abs(np.diff(slope_error_list))
|
248 |
+
diff_midpoint_error = np.abs(np.diff(midpoint_error_list))
|
249 |
+
|
250 |
+
# Calculate the number of jumps in the errors
|
251 |
+
num_slope_error_jumps = np.sum(
|
252 |
+
diff_slope_error > slope_error_jump_threshold)
|
253 |
+
num_midpoint_error_jumps = np.sum(
|
254 |
+
diff_midpoint_error > midpoint_error_jump_threshold)
|
255 |
+
|
256 |
+
# Create a dictionary to store the results
|
257 |
+
sequence_results = {
|
258 |
+
'average_slope_error': average_slope_error,
|
259 |
+
'average_midpoint_error': average_midpoint_error,
|
260 |
+
'stddev_slope_error': stddev_slope_error,
|
261 |
+
'stddev_midpoint_error': stddev_midpoint_error,
|
262 |
+
'max_slope_error': max_slope_error,
|
263 |
+
'max_midpoint_error': max_midpoint_error,
|
264 |
+
'num_slope_error_jumps': num_slope_error_jumps,
|
265 |
+
'num_midpoint_error_jumps': num_midpoint_error_jumps
|
266 |
+
}
|
267 |
+
|
268 |
+
return sequence_results
|
269 |
+
|
270 |
+
|
271 |
+
def slope_to_roll(slope):
|
272 |
+
"""
|
273 |
+
Convert the slope of the horizon to roll
|
274 |
+
|
275 |
+
Args:
|
276 |
+
slope: Slope of the horizon
|
277 |
+
|
278 |
+
Returns:
|
279 |
+
roll: Roll in degrees
|
280 |
+
"""
|
281 |
+
roll = np.arctan(slope) * 180 / np.pi
|
282 |
+
return roll
|
283 |
+
|
284 |
+
|
285 |
+
def roll_to_slope(roll):
|
286 |
+
"""
|
287 |
+
Convert the roll of the horizon to slope
|
288 |
+
|
289 |
+
Args:
|
290 |
+
roll: Roll of the horizon in degrees
|
291 |
+
|
292 |
+
Returns:
|
293 |
+
slope: Slope of the horizon
|
294 |
+
"""
|
295 |
+
slope = np.tan(roll * np.pi / 180)
|
296 |
+
return slope
|
297 |
+
|
298 |
+
|
299 |
+
def midpoint_to_pitch(midpoint, vertical_fov_degrees):
|
300 |
+
"""
|
301 |
+
Convert the midpoint of the horizon to pitch
|
302 |
+
|
303 |
+
Args:
|
304 |
+
midpoint: Midpoint of the horizon
|
305 |
+
vertical_fov_degrees: Vertical field of view of the camera in degrees
|
306 |
+
|
307 |
+
Returns:
|
308 |
+
pitch: Pitch in degrees
|
309 |
+
"""
|
310 |
+
pitch = midpoint * vertical_fov_degrees
|
311 |
+
return pitch
|
312 |
+
|
313 |
+
|
314 |
+
def pitch_to_midpoint(pitch, vertical_fov_degrees):
|
315 |
+
"""
|
316 |
+
Convert the pitch of the horizon to midpoint
|
317 |
+
|
318 |
+
Args:
|
319 |
+
pitch: Pitch of the horizon in degrees
|
320 |
+
vertical_fov_degrees: Vertical field of view of the camera in degrees
|
321 |
+
|
322 |
+
Returns:
|
323 |
+
midpoint: Midpoint of the horizon
|
324 |
+
"""
|
325 |
+
midpoint = pitch / vertical_fov_degrees
|
326 |
+
return midpoint
|
327 |
+
|
328 |
+
# end utils
|
329 |
class horizonmetrics(evaluate.Metric):
|
330 |
"""TODO: Short description of my evaluation module."""
|
331 |
|