Tracker#
The tracker module is the core of Norfair. It takes per-frame detections from
any object or keypoint detector and maintains a set of TrackedObjects with
stable ids across frames, using a configurable distance function and a Kalman
filter to smooth the estimated state.
Most pipelines only need to interact with three things from this module:
Detection— wraps the points/bbox produced by your detector for a single frame.Tracker— matches detections to existing tracks, spawns new ones, and ages out stale ones.TrackedObject— whatTracker.update()returns; the stable, id-carrying representation of an object across frames.
Minimal example#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
A few things worth knowing:
Tracker.update()must be called once per frame, in order. Norfair is an online tracker — it never looks at future frames.- Pass
period=Nif you only run the detector everyNframes but still callupdate()every frame so the filter can keep predicting. - For moving cameras, pass the
coord_transformationsreturned byMotionEstimatorintoupdate()— see Camera Motion.
Choosing a distance function#
The distance_function argument decides how detections are matched to tracks
each frame. Norfair ships with several built-ins that you can select by name
("euclidean", "mean_euclidean", "mean_manhattan", "frobenius",
"iou", "iou_opt") plus any metric supported by scipy.spatial.distance.cdist.
You can also pass your own Callable or a subclass of
Distance for appearance-aware matching.
See Distances for the full list and how to build custom ones.
API#
Tracking primitives: Tracker, TrackedObject and Detection.
Detection
#
A single detection produced by a detector, prepared for use by Norfair.
Detections returned by the detector must be converted to a Detection
object before being passed to the Tracker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
ndarray
|
Points detected. Must be a rank-2 array with shape
|
required |
scores
|
ndarray or float
|
A score per point, or a single scalar score applied to every point.
When an array is given, its length must match This is used to inform the tracker which points to ignore: any
point with a score at or below Useful when detections don't always have every point present, as is often the case in pose estimators. |
None
|
data
|
Any
|
A place to store any extra data which may be useful when calculating the distance function. Anything stored here will be available to use inside the distance function. This enables more interesting trackers that, for instance, attach an appearance embedding to each detection to aid in tracking. |
None
|
label
|
Hashable
|
When working with multiple classes, the detection's label can be stored to be used as a matching condition when associating tracked objects with new detections. The label's type must be hashable for drawing purposes. |
None
|
embedding
|
Any
|
An embedding used by the ReID distance function, if any. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
points |
ndarray
|
The detection points, validated to shape |
absolute_points |
ndarray
|
Starts as a copy of |
age |
int or None
|
Set by the tracker to the matched |
Source code in norfair/tracker.py
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
update_coordinate_transformation(coordinate_transformation)
#
Rewrite absolute_points into absolute coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinate_transformation
|
CoordinatesTransformation
|
Transformation used to map relative points into the absolute
reference frame. When |
required |
Source code in norfair/tracker.py
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
Tracker
#
Tracks detections produced by a detector across frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance_function
|
str or Callable[[Detection, TrackedObject], float]
|
Function used by the tracker to determine the distance between newly
detected objects and the objects that are currently being tracked.
This function should take 2 input arguments, the first being a
Detection, and the second a
TrackedObject. It has to return a
Some common distances are implemented in distances; as a shortcut
the tracker accepts the name of one of these
predefined distances. Scipy's
predefined distances are also accepted — pass a |
required |
distance_threshold
|
float
|
Maximum distance that can constitute a match. Detections and tracked objects whose distance is above this threshold won't be matched by the tracker. |
required |
hit_counter_max
|
int
|
Each tracked object keeps an internal hit counter which tracks how often it gets matched to a detection; each time it gets a match the counter goes up, and each time it doesn't the counter goes down. If the counter goes below 0 the object gets destroyed. This argument defines how large this inertia can grow, and therefore how long an object can live without getting matched to any detections before it is displaced as a dead object. If no ReID distance function is provided the object will then be destroyed. |
15
|
initialization_delay
|
int
|
Determines how large the object's hit counter must be in order to be
considered initialized and be returned to the user as a real object.
It must be smaller than If set to 0, objects will be returned to the user as soon as they are detected for the first time, which can be problematic because it can result in objects appearing and immediately disappearing. Defaults to |
None
|
pointwise_hit_counter_max
|
int
|
Each tracked object keeps track of how often the points it is
tracking have been getting matched. Points that are getting matched
( This is used to determine things like which individual points in a
tracked object get drawn by
|
4
|
detection_threshold
|
float
|
Threshold at which point scores in a detection must be above to be considered by the tracker. Any point whose score is at or below this value is ignored. |
0
|
filter_factory
|
FilterFactory
|
Selects which filter the
|
None
|
past_detections_length
|
int
|
How many past detections to save for each tracked object. Norfair tries to distribute these past detections uniformly through the object's lifetime so they're more representative. Very useful if you want to add metric learning to your model, because you can associate an embedding to each detection and access them in your distance function. |
4
|
reid_distance_function
|
Callable[[TrackedObject, TrackedObject], float]
|
Function used by the tracker to determine the ReID distance between newly detected trackers and unmatched trackers. This function should take 2 input arguments, the first being a
tracked object in the initialization phase, and the second being a
tracked object that has been unmatched. Both are
|
None
|
reid_distance_threshold
|
float
|
Maximum ReID distance that can constitute a match. Tracked objects whose distance is above this threshold won't be merged. If they are merged, the oldest tracked object is maintained with the position of the new tracked object. |
0
|
reid_hit_counter_max
|
int
|
Each tracked object keeps an internal ReID hit counter which tracks how often it is getting recognized by another tracker; each time it gets a match this counter goes up, and each time it doesn't it goes down. If it goes below 0 the object is destroyed. When set, this defines how long an object can live without being matched to any detection before it is destroyed. |
None
|
Notes
Individual Tracker instances are not thread-safe; do not call
update on the same instance from multiple threads. However, using
separate Tracker instances in different threads is safe — the shared
global_id counter is protected by a lock.
Source code in norfair/tracker.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 103 104 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 265 266 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 292 293 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 462 463 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 | |
current_object_count
property
#
Number of currently active TrackedObject instances.
total_object_count
property
#
Total number of TrackedObject instances ever initialized by this tracker.
update(detections=None, period=1, coord_transformations=None)
#
Process detections found in the current frame.
Detections can be matched to previous tracked objects, or new
tracked objects will be created according to the configuration of
this Tracker. The currently alive and initialized tracked
objects are returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detections
|
list[Detection]
|
The If no detections were found in the current frame, or the user is
purposely skipping frames to improve processing time, this
argument should be set to |
None
|
period
|
int
|
Many users choose not to run their detector on every frame in order to process video faster. This parameter sets how many frames pass between detector invocations, so the tracker is aware and can handle the situation properly. It can be reset on each frame processed, which is useful if you are dynamically changing how many frames the detector skips in a real-time video stream. |
1
|
coord_transformations
|
CoordinatesTransformation
|
The coordinate transformation calculated by the
|
None
|
Returns:
| Type | Description |
|---|---|
list[TrackedObject]
|
The list of active tracked objects. |
Notes
When coord_transformations is provided this method mutates
Detection.absolute_points on the caller's objects (overwritten
with absolute coordinates). All other Detection attributes
(including age) are left untouched; the tracker stores shallow
copies of detections internally.
Source code in norfair/tracker.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 265 266 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 292 293 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 325 326 327 328 329 330 331 | |
get_active_objects()
#
Return the list of currently active tracked objects.
An object is active if it has finished initializing and its hit counter is still positive.
Returns:
| Type | Description |
|---|---|
list[TrackedObject]
|
The active tracked objects. |
Source code in norfair/tracker.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
match_dets_and_objs(distance_matrix, distance_threshold)
#
Match detections with tracked objects from a distance matrix.
Instead of minimizing the global distance, this greedy strategy
starts with the global minimum entry and matches the det-obj
corresponding to that distance, then takes the second minimum, and
so on until distance_threshold is reached.
This avoids pathological cases where minimizing the global distance forces matches that shouldn't happen just to bring the overall sum down.
The distances are pre-sorted with np.argsort so the scan is
O(nmlog(nm) + nm) instead of the previous O(min(n,m)nm)
repeated-argmin approach.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance_matrix
|
ndarray
|
A matrix of shape |
required |
distance_threshold
|
float
|
Entries greater than or equal to this value are not matched. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[int], list[int]]
|
Matched detection and object indices, in matching order. |
Source code in norfair/tracker.py
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 462 463 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 | |
TrackedObject
#
Objects returned by the tracker's update method on each iteration.
They represent the objects currently being tracked by the tracker.
Users should not instantiate TrackedObject manually; the Tracker
is in charge of creating them.
Attributes:
| Name | Type | Description |
|---|---|---|
estimate |
ndarray
|
Where the tracker predicts the points will be in the current frame based on past detections. A NumPy array with the same shape as the detections being fed to the tracker that produced it. |
id |
int or None
|
The unique identifier assigned to this object by the tracker. Set to
|
global_id |
int or None
|
The globally unique identifier assigned to this object. Set to
|
last_detection |
Detection
|
The last detection that matched with this tracked object. Useful if you are storing embeddings in your detections and want to do metric learning, or for debugging. |
last_distance |
float or None
|
The distance the tracker had with the last object it matched with. |
age |
int
|
The age of this object measured in number of frames. |
live_points |
ndarray
|
A boolean mask with shape Functions like |
initializing_id |
int
|
On top of Each new object created by the |
Source code in norfair/tracker.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | |
hit_counter_is_positive
property
#
Whether the hit counter is still non-negative (object is alive).
reid_hit_counter_is_positive
property
#
Whether the ReID hit counter is still non-negative (object can still be re-identified).
estimate_velocity
property
#
Return the velocity estimate of the object from the Kalman filter.
The velocity is expressed in the absolute coordinate system.
Returns:
| Type | Description |
|---|---|
ndarray
|
An array of shape |
estimate
property
#
Return the position estimate of the object from the Kalman filter.
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
An array of shape |
live_points
property
#
Boolean mask marking which tracked points are live in the current frame.
A point is live only if it was matched to a detection in the current frame and its pointwise hit counter is still positive.
tracker_step()
#
Advance the internal state of the tracker by one frame.
Decrements the hit counters, increments the age and steps the
Kalman filter prediction forward. Called by the Tracker once per
update cycle.
Source code in norfair/tracker.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
get_estimate(absolute=False)
#
Return the position estimate in either absolute or relative coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
absolute
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
An array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If absolute coordinates are requested but the tracker has no coordinate transformation attached. |
Source code in norfair/tracker.py
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | |
hit(detection, period=1)
#
Update this tracked object with a new detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detection
|
Detection
|
The new detection matched to this tracked object. |
required |
period
|
int
|
Frames corresponding to the period of time since the last
update. Defaults to |
1
|
Source code in norfair/tracker.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
__repr__()
#
Return a human-readable representation of this tracked object.
Source code in norfair/tracker.py
901 902 903 904 905 906 907 908 909 910 911 912 913 | |
merge(tracked_object)
#
Merge another (not-yet-initialized) TrackedObject into this one.
Mutable state (filter, counters, arrays) is copied so the
discarded tracked_object is not silently aliased.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracked_object
|
TrackedObject
|
Another tracked object (typically not yet fully initialized) whose state will be absorbed into this one. |
required |
Source code in norfair/tracker.py
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
update_coordinate_transformation(coordinate_transformation)
#
Attach (or refresh) the abs→rel converter from a new coordinate transformation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinate_transformation
|
CoordinatesTransformation or None
|
The new coordinate transformation whose |
required |
Source code in norfair/tracker.py
972 973 974 975 976 977 978 979 980 981 982 983 984 985 | |
See also#
- Distances — how detections are matched to tracks.
- Filter — Kalman filter factories used to smooth state estimates.
- Drawing — rendering the returned
TrackedObjects on frames. - Camera Motion — handling moving cameras.