Distances#
Predefined distances
Distance
#
Bases: ABC
Abstract class representing a distance.
Subclasses must implement the method get_distances
Source code in norfair/distances.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
get_distances(objects, candidates)
abstractmethod
#
Method that calculates the distances between new candidates and objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objects |
Sequence[TrackedObject]
|
Sequence of TrackedObject to be compared with potential Detection or TrackedObject candidates. |
required |
candidates |
Union[List[Detection], List[TrackedObject]], optional
|
List of candidates (Detection or TrackedObject) to be compared to TrackedObject. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
A matrix containing the distances between objects and candidates. |
Source code in norfair/distances.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
ScalarDistance
#
Bases: Distance
ScalarDistance class represents a distance that is calculated pointwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distance_function |
Union[Callable[[Detection, TrackedObject], float], Callable[[TrackedObject, TrackedObject], float]]
|
Distance function used to determine the pointwise distance between new candidates and objects.
This function should take 2 input arguments, the first being a |
required |
Source code in norfair/distances.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
get_distances(objects, candidates)
#
Method that calculates the distances between new candidates and objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objects |
Sequence[TrackedObject]
|
Sequence of TrackedObject to be compared with potential Detection or TrackedObject candidates. |
required |
candidates |
Union[List[Detection], List[TrackedObject]], optional
|
List of candidates (Detection or TrackedObject) to be compared to TrackedObject. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
A matrix containing the distances between objects and candidates. |
Source code in norfair/distances.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
VectorizedDistance
#
Bases: Distance
VectorizedDistance class represents a distance that is calculated in a vectorized way. This means that instead of going through every pair and explicitly calculating its distance, VectorizedDistance uses the entire vectors to compare to each other in a single operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distance_function |
Callable[[np.ndarray, np.ndarray], np.ndarray]
|
Distance function used to determine the distances between new candidates and objects.
This function should take 2 input arguments, the first being a |
required |
Source code in norfair/distances.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
get_distances(objects, candidates)
#
Method that calculates the distances between new candidates and objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objects |
Sequence[TrackedObject]
|
Sequence of TrackedObject to be compared with potential Detection or TrackedObject candidates. |
required |
candidates |
Union[List[Detection], List[TrackedObject]], optional
|
List of candidates (Detection or TrackedObject) to be compared to TrackedObject. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
A matrix containing the distances between objects and candidates. |
Source code in norfair/distances.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
ScipyDistance
#
Bases: VectorizedDistance
ScipyDistance class extends VectorizedDistance for the use of Scipy's vectorized distances.
This class uses scipy.spatial.distance.cdist
to calculate distances between two np.ndarray
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
str, optional
|
Defines the specific Scipy metric to use to calculate the pairwise distances between new candidates and objects. |
'euclidean'
|
Other kwargs are passed through to cdist
See Also#
Source code in norfair/distances.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
frobenius(detection, tracked_object)
#
Frobernius norm on the difference of the points in detection and the estimates in tracked_object.
The Frobenius distance and norm are given by:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detection |
Detection
|
A detection. |
required |
tracked_object |
TrackedObject
|
A tracked object. |
required |
Returns:
Type | Description |
---|---|
float
|
The distance. |
See Also#
Source code in norfair/distances.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
mean_euclidean(detection, tracked_object)
#
Average euclidean distance between the points in detection and estimates in tracked_object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detection |
Detection
|
A detection. |
required |
tracked_object |
TrackedObject
|
A tracked object |
required |
Returns:
Type | Description |
---|---|
float
|
The distance. |
See Also#
Source code in norfair/distances.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
mean_manhattan(detection, tracked_object)
#
Average manhattan distance between the points in detection and the estimates in tracked_object
Given by:
Where \(||a||_1\) is the manhattan norm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detection |
Detection
|
A detection. |
required |
tracked_object |
TrackedObject
|
a tracked object. |
required |
Returns:
Type | Description |
---|---|
float
|
The distance. |
See Also#
Source code in norfair/distances.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
iou(candidates, objects)
#
Calculate IoU between two sets of bounding boxes. Both sets of boxes are expected
to be in [x_min, y_min, x_max, y_max]
format.
Normal IoU is 1 when the boxes are the same and 0 when they don't overlap,
to transform that into a distance that makes sense we return 1 - iou
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
candidates |
numpy.ndarray
|
(N, 4) numpy.ndarray containing candidates bounding boxes. |
required |
objects |
numpy.ndarray
|
(K, 4) numpy.ndarray containing objects bounding boxes. |
required |
Returns:
Type | Description |
---|---|
numpy.ndarray
|
(N, K) numpy.ndarray of |
Source code in norfair/distances.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
|
get_distance_by_name(name)
#
Select a distance by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
A string defining the metric to get. |
required |
Returns:
Type | Description |
---|---|
Distance
|
The distance object. |
Source code in norfair/distances.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
|
create_keypoints_voting_distance(keypoint_distance_threshold, detection_threshold)
#
Construct a keypoint voting distance function configured with the thresholds.
Count how many points in a detection match the with a tracked_object.
A match is considered when distance between the points is < keypoint_distance_threshold
and the score of the last_detection of the tracked_object is > detection_threshold
.
Notice the if multiple points are tracked, the ith point in detection can only match the ith
point in the tracked object.
Distance is 1 if no point matches and approximates 0 as more points are matched.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keypoint_distance_threshold |
float
|
Points closer than this threshold are considered a match. |
required |
detection_threshold |
float
|
Detections and objects with score lower than this threshold are ignored. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The distance funtion that must be passed to the Tracker. |
Source code in norfair/distances.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
|
create_normalized_mean_euclidean_distance(height, width)
#
Construct a normalized mean euclidean distance function configured with the max height and width.
The result distance is bound to [0, 1] where 1 indicates oposite corners of the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
height |
int
|
Height of the image. |
required |
width |
int
|
Width of the image. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The distance funtion that must be passed to the Tracker. |
Source code in norfair/distances.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|