Spaces:
Sleeping
Sleeping
Victoria Oberascher
commited on
Commit
·
ff4be31
1
Parent(s):
a333457
change imports
Browse files- horizonmetrics.py +1 -267
- requirements.txt +1 -3
- utils.py +0 -266
horizonmetrics.py
CHANGED
|
@@ -15,6 +15,7 @@
|
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
|
|
|
| 18 |
|
| 19 |
# TODO: Add BibTeX citation
|
| 20 |
_CITATION = """\
|
|
@@ -54,273 +55,6 @@ Examples:
|
|
| 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)
|
|
|
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
+
from seametrics.horizon.utils import *
|
| 19 |
|
| 20 |
# TODO: Add BibTeX citation
|
| 21 |
_CITATION = """\
|
|
|
|
| 55 |
# TODO: Define external resources urls if needed
|
| 56 |
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 -1,4 +1,2 @@
|
|
| 1 |
git+https://github.com/huggingface/evaluate@main
|
| 2 |
-
git+https://github.com/SEA-AI/seametrics@
|
| 3 |
-
fiftyone
|
| 4 |
-
opencv-python
|
|
|
|
| 1 |
git+https://github.com/huggingface/evaluate@main
|
| 2 |
+
git+https://github.com/SEA-AI/seametrics@horizon_metrics
|
|
|
|
|
|
utils.py
DELETED
|
@@ -1,266 +0,0 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
def xy_points_to_slope_midpoint(xy_points):
|
| 5 |
-
"""
|
| 6 |
-
Given two points, return the slope and midpoint of the line
|
| 7 |
-
|
| 8 |
-
Args:
|
| 9 |
-
xy_points: list of two points, each point is a list of two elements
|
| 10 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 11 |
-
|
| 12 |
-
Returns:
|
| 13 |
-
slope: Slope of the line
|
| 14 |
-
midpoint : Midpoint is in the form of [x,y], and is also normalized to [0, 1]
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
x1, y1, x2, y2 = xy_points[0][0], xy_points[0][1], xy_points[1][
|
| 18 |
-
0], xy_points[1][1]
|
| 19 |
-
slope = (y2 - y1) / (x2 - x1)
|
| 20 |
-
|
| 21 |
-
midpoint_x = 0.5
|
| 22 |
-
midpoint_y = slope * (0.5 - x1) + y1
|
| 23 |
-
midpoint = [midpoint_x, midpoint_y]
|
| 24 |
-
return slope, midpoint
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def calculate_horizon_error(annotated_horizon, proposed_horizon):
|
| 28 |
-
"""
|
| 29 |
-
Calculate the error between the annotated horizon and the proposed horizon
|
| 30 |
-
|
| 31 |
-
Args:
|
| 32 |
-
annotated_horizon: list of two points, each point is a list of two elements
|
| 33 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 34 |
-
proposed_horizon: list of two points, each point is a list of two elements
|
| 35 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
slope_error: Error in the slope of the lines
|
| 39 |
-
midpoint_error: Error in the midpoint_y of the lines
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
slope_annotated, midpoint_annotated = xy_points_to_slope_midpoint(
|
| 43 |
-
annotated_horizon)
|
| 44 |
-
slope_proposed, midpoint_proposed = xy_points_to_slope_midpoint(
|
| 45 |
-
proposed_horizon)
|
| 46 |
-
|
| 47 |
-
slope_error = abs(slope_annotated - slope_proposed)
|
| 48 |
-
midpoint_error = abs(midpoint_annotated[1] - midpoint_proposed[1])
|
| 49 |
-
|
| 50 |
-
return slope_error, midpoint_error
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def calculate_horizon_error_across_sequence(slope_error_list,
|
| 54 |
-
midpoint_error_list,
|
| 55 |
-
slope_error_jump_threshold,
|
| 56 |
-
midpoint_error_jump_threshold):
|
| 57 |
-
"""
|
| 58 |
-
Calculate the error statistics across a sequence of frames
|
| 59 |
-
|
| 60 |
-
Args:
|
| 61 |
-
slope_error_list: List of errors in the slope of the lines
|
| 62 |
-
midpoint_error_list: List of errors in the midpoint_y of the lines
|
| 63 |
-
|
| 64 |
-
Returns:
|
| 65 |
-
average_slope_error: Average error in the slope of the lines
|
| 66 |
-
average_midpoint_error: Average error in the midpoint_y of the lines
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
# Calculate the average and standard deviation of the errors
|
| 70 |
-
average_slope_error = np.mean(slope_error_list)
|
| 71 |
-
average_midpoint_error = np.mean(midpoint_error_list)
|
| 72 |
-
|
| 73 |
-
stddev_slope_error = np.std(slope_error_list)
|
| 74 |
-
stddev_midpoint_error = np.std(midpoint_error_list)
|
| 75 |
-
|
| 76 |
-
# Calculate the maximum errors
|
| 77 |
-
max_slope_error = np.max(slope_error_list)
|
| 78 |
-
max_midpoint_error = np.max(midpoint_error_list)
|
| 79 |
-
|
| 80 |
-
# Calculate the differences between errors in successive frames
|
| 81 |
-
diff_slope_error = np.abs(np.diff(slope_error_list))
|
| 82 |
-
diff_midpoint_error = np.abs(np.diff(midpoint_error_list))
|
| 83 |
-
|
| 84 |
-
# Calculate the number of jumps in the errors
|
| 85 |
-
num_slope_error_jumps = np.sum(
|
| 86 |
-
diff_slope_error > slope_error_jump_threshold)
|
| 87 |
-
num_midpoint_error_jumps = np.sum(
|
| 88 |
-
diff_midpoint_error > midpoint_error_jump_threshold)
|
| 89 |
-
|
| 90 |
-
# Create a dictionary to store the results
|
| 91 |
-
sequence_results = {
|
| 92 |
-
'average_slope_error': average_slope_error,
|
| 93 |
-
'average_midpoint_error': average_midpoint_error,
|
| 94 |
-
'stddev_slope_error': stddev_slope_error,
|
| 95 |
-
'stddev_midpoint_error': stddev_midpoint_error,
|
| 96 |
-
'max_slope_error': max_slope_error,
|
| 97 |
-
'max_midpoint_error': max_midpoint_error,
|
| 98 |
-
'num_slope_error_jumps': num_slope_error_jumps,
|
| 99 |
-
'num_midpoint_error_jumps': num_midpoint_error_jumps
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
return sequence_results
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
import numpy as np
|
| 106 |
-
import cv2
|
| 107 |
-
import matplotlib.pyplot as plt
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def xy_points_to_slope_midpoint(xy_points):
|
| 111 |
-
"""
|
| 112 |
-
Given two points, return the slope and midpoint of the line
|
| 113 |
-
|
| 114 |
-
Args:
|
| 115 |
-
xy_points: list of two points, each point is a list of two elements
|
| 116 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 117 |
-
|
| 118 |
-
Returns:
|
| 119 |
-
slope: Slope of the line
|
| 120 |
-
midpoint : Midpoint is in the form of [x,y], and is also normalized to [0, 1]
|
| 121 |
-
"""
|
| 122 |
-
|
| 123 |
-
x1, y1, x2, y2 = xy_points[0][0], xy_points[0][1], xy_points[1][
|
| 124 |
-
0], xy_points[1][1]
|
| 125 |
-
slope = (y2 - y1) / (x2 - x1)
|
| 126 |
-
|
| 127 |
-
midpoint_x = 0.5
|
| 128 |
-
midpoint_y = slope * (0.5 - x1) + y1
|
| 129 |
-
midpoint = [midpoint_x, midpoint_y]
|
| 130 |
-
return slope, midpoint
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
def calculate_horizon_error(annotated_horizon, proposed_horizon):
|
| 134 |
-
"""
|
| 135 |
-
Calculate the error between the annotated horizon and the proposed horizon
|
| 136 |
-
|
| 137 |
-
Args:
|
| 138 |
-
annotated_horizon: list of two points, each point is a list of two elements
|
| 139 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 140 |
-
proposed_horizon: list of two points, each point is a list of two elements
|
| 141 |
-
Points are in the form of [x, y], where x and y are normalized to [0, 1]
|
| 142 |
-
|
| 143 |
-
Returns:
|
| 144 |
-
slope_error: Error in the slope of the lines
|
| 145 |
-
midpoint_error: Error in the midpoint_y of the lines
|
| 146 |
-
"""
|
| 147 |
-
|
| 148 |
-
slope_annotated, midpoint_annotated = xy_points_to_slope_midpoint(
|
| 149 |
-
annotated_horizon)
|
| 150 |
-
slope_proposed, midpoint_proposed = xy_points_to_slope_midpoint(
|
| 151 |
-
proposed_horizon)
|
| 152 |
-
|
| 153 |
-
slope_error = abs(slope_annotated - slope_proposed)
|
| 154 |
-
midpoint_error = abs(midpoint_annotated[1] - midpoint_proposed[1])
|
| 155 |
-
|
| 156 |
-
return slope_error, midpoint_error
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
def calculate_horizon_error_across_sequence(slope_error_list,
|
| 160 |
-
midpoint_error_list,
|
| 161 |
-
slope_error_jump_threshold,
|
| 162 |
-
midpoint_error_jump_threshold):
|
| 163 |
-
"""
|
| 164 |
-
Calculate the error statistics across a sequence of frames
|
| 165 |
-
|
| 166 |
-
Args:
|
| 167 |
-
slope_error_list: List of errors in the slope of the lines
|
| 168 |
-
midpoint_error_list: List of errors in the midpoint_y of the lines
|
| 169 |
-
|
| 170 |
-
Returns:
|
| 171 |
-
average_slope_error: Average error in the slope of the lines
|
| 172 |
-
average_midpoint_error: Average error in the midpoint_y of the lines
|
| 173 |
-
"""
|
| 174 |
-
|
| 175 |
-
# Calculate the average and standard deviation of the errors
|
| 176 |
-
average_slope_error = np.mean(slope_error_list)
|
| 177 |
-
average_midpoint_error = np.mean(midpoint_error_list)
|
| 178 |
-
|
| 179 |
-
stddev_slope_error = np.std(slope_error_list)
|
| 180 |
-
stddev_midpoint_error = np.std(midpoint_error_list)
|
| 181 |
-
|
| 182 |
-
# Calculate the maximum errors
|
| 183 |
-
max_slope_error = np.max(slope_error_list)
|
| 184 |
-
max_midpoint_error = np.max(midpoint_error_list)
|
| 185 |
-
|
| 186 |
-
# Calculate the differences between errors in successive frames
|
| 187 |
-
diff_slope_error = np.abs(np.diff(slope_error_list))
|
| 188 |
-
diff_midpoint_error = np.abs(np.diff(midpoint_error_list))
|
| 189 |
-
|
| 190 |
-
# Calculate the number of jumps in the errors
|
| 191 |
-
num_slope_error_jumps = np.sum(
|
| 192 |
-
diff_slope_error > slope_error_jump_threshold)
|
| 193 |
-
num_midpoint_error_jumps = np.sum(
|
| 194 |
-
diff_midpoint_error > midpoint_error_jump_threshold)
|
| 195 |
-
|
| 196 |
-
# Create a dictionary to store the results
|
| 197 |
-
sequence_results = {
|
| 198 |
-
'average_slope_error': average_slope_error,
|
| 199 |
-
'average_midpoint_error': average_midpoint_error,
|
| 200 |
-
'stddev_slope_error': stddev_slope_error,
|
| 201 |
-
'stddev_midpoint_error': stddev_midpoint_error,
|
| 202 |
-
'max_slope_error': max_slope_error,
|
| 203 |
-
'max_midpoint_error': max_midpoint_error,
|
| 204 |
-
'num_slope_error_jumps': num_slope_error_jumps,
|
| 205 |
-
'num_midpoint_error_jumps': num_midpoint_error_jumps
|
| 206 |
-
}
|
| 207 |
-
|
| 208 |
-
return sequence_results
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
def slope_to_roll(slope):
|
| 212 |
-
"""
|
| 213 |
-
Convert the slope of the horizon to roll
|
| 214 |
-
|
| 215 |
-
Args:
|
| 216 |
-
slope: Slope of the horizon
|
| 217 |
-
|
| 218 |
-
Returns:
|
| 219 |
-
roll: Roll in degrees
|
| 220 |
-
"""
|
| 221 |
-
roll = np.arctan(slope) * 180 / np.pi
|
| 222 |
-
return roll
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
def roll_to_slope(roll):
|
| 226 |
-
"""
|
| 227 |
-
Convert the roll of the horizon to slope
|
| 228 |
-
|
| 229 |
-
Args:
|
| 230 |
-
roll: Roll of the horizon in degrees
|
| 231 |
-
|
| 232 |
-
Returns:
|
| 233 |
-
slope: Slope of the horizon
|
| 234 |
-
"""
|
| 235 |
-
slope = np.tan(roll * np.pi / 180)
|
| 236 |
-
return slope
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
def midpoint_to_pitch(midpoint, vertical_fov_degrees):
|
| 240 |
-
"""
|
| 241 |
-
Convert the midpoint of the horizon to pitch
|
| 242 |
-
|
| 243 |
-
Args:
|
| 244 |
-
midpoint: Midpoint of the horizon
|
| 245 |
-
vertical_fov_degrees: Vertical field of view of the camera in degrees
|
| 246 |
-
|
| 247 |
-
Returns:
|
| 248 |
-
pitch: Pitch in degrees
|
| 249 |
-
"""
|
| 250 |
-
pitch = midpoint * vertical_fov_degrees
|
| 251 |
-
return pitch
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
def pitch_to_midpoint(pitch, vertical_fov_degrees):
|
| 255 |
-
"""
|
| 256 |
-
Convert the pitch of the horizon to midpoint
|
| 257 |
-
|
| 258 |
-
Args:
|
| 259 |
-
pitch: Pitch of the horizon in degrees
|
| 260 |
-
vertical_fov_degrees: Vertical field of view of the camera in degrees
|
| 261 |
-
|
| 262 |
-
Returns:
|
| 263 |
-
midpoint: Midpoint of the horizon
|
| 264 |
-
"""
|
| 265 |
-
midpoint = pitch / vertical_fov_degrees
|
| 266 |
-
return midpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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