Victoria Oberascher commited on
Commit
98d4608
·
1 Parent(s): 2f9b9a5

restructure main file

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