ObjectTracking

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

Bases: object

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 (integer) – The maximum number of objects to track.

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

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

  • min_inertia (integer) – 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 (function that takes the object ID and ObjectDetectionPrediction as arguments.) – A callback function to be called each time a new object is detected.

  • exit_cb (function that takes the object ID and ObjectDetectionPrediction as arguments.) – 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 of ObjectDetectionPrediction) – The list of bounding boxes to track.

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 the correct object id is provided. Otherwise, the function will not update anything.

Parameters

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

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

Bases: object

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 (integer) – The number of frames before deregistering an object that can no longer be found.

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

  • min_inertia (integer) – 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 (function that takes the object ID and ObjectDetectionPrediction as arguments.) – A callback function to be called each time a new object is detected.

  • exit_cb (function that takes the object ID and ObjectDetectionPrediction as arguments.) – 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)

Update tracked objects based on a new set of predictions.

Parameters

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

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 the correct object id is provided. Otherwise, the function will not update anything.

Parameters

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

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

Bases: object

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 (integer) – The number of frames before deregistering an object that can no longer be found.

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

  • min_inertia (integer) – 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 (function that takes the object ID and ObjectDetectionPrediction as arguments.) – A callback function to be called each time a new object is detected.

  • exit_cb (function that takes the object ID and ObjectDetectionPrediction as arguments.) – 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.

update(predictions)

Update tracked objects based on a new set of predictions.

Parameters

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

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 the correct object id is provided. Otherwise, the function will not update anything.

Parameters

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

class TrackingResults(objects, history, algorithm)

Bases: dict

The output results of the tracker

property history

The historical list of predictions, indexed by object ID

Return type

dict

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