ObjectTracking

class CorrelationTracker(max_objects=None, deregister_frames=30, max_distance=50, min_inertia=5, history_length=0, enter_cb=None, exit_cb=None)

Bases: edgeiq.object_tracking.object_tracking.TrackerAlgorithm

Track objects based on a correlation tracking algorithm.

Typical usage:

def object_enters(object_id, prediction):
    print("{}: {} enters".format(object_id, prediction.label))

def object_exits(object_id, prediction):
    print("{} exits".format(prediction.label))

obj_detect = edgeiq.ObjectDetection(
        'alwaysai/res10_300x300_ssd_iter_140000')
obj_detect.load(engine=edgeiq.Engine.DNN)

tracker = edgeiq.CorrelationTracker(
        deregister_frames=20,
        max_distance=50, enter_cb=object_enters,
        exit_cb=object_exits)

while True:
    <get video frame>
    results = obj_detect.detect_objects(frame)

    objects = tracker.update(results.predictions, frame)

    # Use the object dictionary to create a new prediction list
    for (object_id, prediction) in objects.items():
        new_label = 'object {}'.format(object_id)
        prediction.label = new_label
        predictions.append(prediction)

    frame = edgeiq.markup_image(frame, predictions)
Parameters
  • max_objects (Optional[int]) – The maximum number of objects to track.

  • deregister_frames (int) – The number of frames before deregistering an object that can no longer be found.

  • max_distance (int) – The maximum distance between two centroids to associate an object.

  • min_inertia (int) – The inertia is computed by incrementing when a detection is matched and decrementing when it isn’t, flooring at zero. This value is the threshold at which an item begins being treated as a tracked object.

  • enter_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time a new object is detected.

  • exit_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time an existing object is removed from the tracking list. This event occurs deregister_frames after the object is last detected.

update(predictions, image)

Update tracked objects based on a new set of predictions and a new image.

Parameters

predictions (List[ObjectDetectionPrediction]) – The list of bounding boxes to track.

Return type

TrackingResults

Returns

A dictionary with object ID as the key and the ObjectDetectionPrediction as the value.

remove_id(id)

Remove a particular object from the list of objects being tracked

The function removes the object if a tracked object id is provided. Otherwise, the function will not update anything.

Parameters

id (int) – The tracking id of the object to be deleted from the list of objects that are being tracked.

class TrackableCorrelationPrediction(*args, **kwargs)

Bases: edgeiq.object_tracking.object_tracking.TrackablePrediction

handle_found(prediction, dereg_tracked_obj, **kwargs)
handle_disappeared(image, reg_tracked_obj, can_track_new_obj, **kwargs)
property box

The bounding box around the object.

Return type

BoundingBox

property confidence

The confidence of this prediction.

Return type

float

handle_removed()
property history
Return type

List[ObjectDetectionPrediction]

property index

The index of this result in the master label list.

Return type

int

property is_initialized
Return type

bool

property is_lost
Return type

bool

property label

The label describing this prediction result.

Return type

str

step(**kwargs)
tracker_id = 0
tracker_init_id = 0
class CentroidTracker(deregister_frames=30, max_distance=50, min_inertia=5, history_length=0, enter_cb=None, exit_cb=None)

Bases: edgeiq.object_tracking.object_tracking.TrackerAlgorithm

Associate a bounding box with an object ID based on distances from previous detections.

Typical usage:

def object_enters(object_id, prediction):
    print("{}: {} enters".format(object_id, prediction.label))

def object_exits(object_id, prediction):
    print("{} exits".format(prediction.label))

obj_detect = edgeiq.ObjectDetection(
        'alwaysai/res10_300x300_ssd_iter_140000')
obj_detect.load(engine=edgeiq.Engine.DNN)

centroid_tracker = edgeiq.CentroidTracker(
        deregister_frames=20,
        max_distance=50, enter_cb=object_enters,
        exit_cb=object_exits)

while True:
    <get video frame>
    results = obj_detect.detect_objects(frame)

    objects = centroid_tracker.update(results.predictions)

    # Use the object dictionary to create a new prediction list
    for (object_id, prediction) in objects.items():
        new_label = 'object {}'.format(object_id)
        prediction.label = new_label
        predictions.append(prediction)

    frame = edgeiq.markup_image(frame, predictions)
Parameters
  • deregister_frames (int) – The number of frames before deregistering an object that can no longer be found.

  • max_distance (int) – The maximum distance between two centroids to associate an object.

  • min_inertia (int) – The inertia is computed by incrementing when a detection is matched and decrementing when it isn’t, flooring at zero. This value is the threshold at which an item begins being treated as a tracked object.

  • enter_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time a new object is detected.

  • exit_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time an existing object is removed from the tracking list. This event occurs deregister_frames after the object is last detected.

remove_id(id)

Remove a particular object from the list of objects being tracked

The function removes the object if a tracked object id is provided. Otherwise, the function will not update anything.

Parameters

id (int) – The tracking id of the object to be deleted from the list of objects that are being tracked.

update(predictions, **trackable_kwargs)

Update tracked objects based on a new set of predictions.

Parameters

predictions (List[ObjectDetectionPrediction]) – The list of bounding boxes to track.

Return type

TrackingResults[~TrackablePredictionT]

class KalmanTracker(deregister_frames=30, max_distance=50, min_inertia=5, history_length=0, enter_cb=None, exit_cb=None)

Bases: edgeiq.object_tracking.object_tracking.TrackerAlgorithm

Associate a bounding box with an object ID based on distances from previous detections. Each tracked object is assigned a Kalman Filter, which is updated after each frame, whose purpose is to model and understand the motion of the object being tracked. This adds a level of robustness to the tracking system, as the future position/velocity of the object may be estimated and used when matching objects in frame rather than just the object’s current position. This additional complexity adds computational overhead.

The output predictions have additional attributes:

  • estimated_position: The estimated position from the Kalman filter

  • estimated_velocity: The estimated velocity from the Kalman filter

Typical usage:

def object_enters(object_id, prediction):
    print("{}: {} enters".format(object_id, prediction.label))

def object_exits(object_id, prediction):
    print("{} exits".format(prediction.label))

obj_detect = edgeiq.ObjectDetection(
        'alwaysai/res10_300x300_ssd_iter_140000')
obj_detect.load(engine=edgeiq.Engine.DNN)

kalman_tracker = edgeiq.KalmanTracker(
        deregister_frames=20,
        max_distance=50, enter_cb=object_enters,
        exit_cb=object_exits)

while True:
    <get video frame>
    results = obj_detect.detect_objects(frame)

    objects = kalman_tracker.update(results.predictions)

    # Use the object dictionary to create a new prediction list
    for (object_id, prediction) in objects.items():
        new_label = 'object {}: position {} velocity {}'.format(
                object_id,
                prediction.estimated_position,
                prediction.estimated_velocity)
        prediction.label = new_label
        predictions.append(prediction)

    frame = edgeiq.markup_image(frame, predictions)
Parameters
  • deregister_frames (int) – The number of frames before deregistering an object that can no longer be found.

  • max_distance (int) – The maximum distance between two centroids to associate an object.

  • min_inertia (int) – The inertia is computed by incrementing when a detection is matched and decrementing when it isn’t, flooring at zero. This value is the threshold at which an item begins being treated as a tracked object.

  • enter_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time a new object is detected.

  • exit_cb (Optional[Callable[[int, ~TrackablePredictionT], None]]) – A callback function to be called each time an existing object is removed from the tracking list. This event occurs deregister_frames after the object is last detected.

  • history_length (int) – The number of historical predictions to remember.

remove_id(id)

Remove a particular object from the list of objects being tracked

The function removes the object if a tracked object id is provided. Otherwise, the function will not update anything.

Parameters

id (int) – The tracking id of the object to be deleted from the list of objects that are being tracked.

update(predictions, **trackable_kwargs)

Update tracked objects based on a new set of predictions.

Parameters

predictions (List[ObjectDetectionPrediction]) – The list of bounding boxes to track.

Return type

TrackingResults[~TrackablePredictionT]

class TrackableKalmanPrediction(*args, **kwargs)

Bases: edgeiq.object_tracking.object_tracking.TrackablePrediction

property estimated_position
Return type

ndarray

property estimated_velocity
Return type

ndarray

step(*args, **kwargs)
handle_found(prediction)
property box

The bounding box around the object.

Return type

BoundingBox

property confidence

The confidence of this prediction.

Return type

float

handle_disappeared()
handle_removed()
property history
Return type

List[ObjectDetectionPrediction]

property index

The index of this result in the master label list.

Return type

int

property is_initialized
Return type

bool

property is_lost
Return type

bool

property label

The label describing this prediction result.

Return type

str

tracker_id = 0
tracker_init_id = 0
class TrackingResults(objects, tracking_algorithm)

Bases: dict, typing.Generic

The output results of the tracker

property tracking_algorithm

The tracking algorithm used

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() → a set-like object providing a view on D’s items
keys() → a set-like object providing a view on D’s keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → an object providing a view on D’s values
class TrackablePrediction(prediction, deregister_frames, min_inertia, history_length, enter_cb=None, exit_cb=None)

Bases: edgeiq.object_detection.ObjectDetectionPrediction

tracker_init_id = 0
tracker_id = 0
step(**kwargs)
handle_found(prediction)
handle_disappeared()
handle_removed()
property is_initialized
Return type

bool

property is_lost
Return type

bool

property history
Return type

List[ObjectDetectionPrediction]

property label

The label describing this prediction result.

Return type

str

property index

The index of this result in the master label list.

Return type

int

property box

The bounding box around the object.

Return type

BoundingBox

property confidence

The confidence of this prediction.

Return type

float

class TrackerAlgorithm(deregister_frames, min_inertia, history_length, enter_cb, exit_cb, trackable, distance_function, match_optimizer)

Bases: typing.Generic

update(predictions, **trackable_kwargs)

Update tracked objects based on a new set of predictions.

Parameters

predictions (List[ObjectDetectionPrediction]) – The list of bounding boxes to track.

Return type

TrackingResults[~TrackablePredictionT]

remove_id(id)

Remove a particular object from the list of objects being tracked

The function removes the object if a tracked object id is provided. Otherwise, the function will not update anything.

Parameters

id (int) – The tracking id of the object to be deleted from the list of objects that are being tracked.

match_greedy(dist)
Return type

List[Tuple[int, int]]

match_optimal(dist)
Return type

List[Tuple[int, int]]