Deep SORT Realtime

repository·master·Indexed 42 days ago

https://github.com/levan92/deep_sort_realtime

A real-time adaptation of the Deep SORT tracking algorithm for Python. It features support for custom appearance embedders, polygon detections, and background masking. The library integrates with Pytorch (MobileNetV2, TorchReid, CLIP) and Tensorflow embedding networks to perform object tracking via the DeepSort class and its update_tracks method.

Tokens
1.4K
Snippets
4
Records
8
Agent score
4%

What's inside Deep SORT Realtime

  1. Use background masking for embeddings

    master
    To reduce background bias in embeddings, you can provide an instance mask during the update step. If an instance mask is provided via DeepSort.update_tracks and no external appearance embeddings are provided, the mask will be used to mask out the background of the detection crop. Only the foreground information will be passed to the embedder.
  2. Enable polygon support for detections

    master

    The tracker supports polygon detections by converting them to bounding rectangles.

    Setup:

    1. When instantiating DeepSort, set the polygon=True argument.
    2. Provide detections as polygons (format details in DeepSort.update_tracks docstring).

    Behavior:

    • Tracking is performed on the bounding rectangle of the polygon.
    • If embedding is enabled, the embedder works on the crop around the bounding rectangle, but the area not covered by the polygon is masked away.
    • The original polygon coordinates are passed to the associated track via the supplementary info mechanism.
  3. Install deep-sort-realtime

    master

    You can install the package via PyPI using pip, or by cloning the repository and installing it locally.

    Via PyPI:

    pip3 install deep-sort-realtime

    From source:

    cd deep_sort_realtime && pip3 install .

    You can also download the .whl file from the project's releases page.

  4. Run basic object tracking

    master

    To use the tracker, instantiate DeepSort and call update_tracks with a list of detections.

    Detection Format: bbs (bounding boxes) should be a list of tuples, where each tuple contains:

    1. [left, top, w, h] (the bounding box coordinates)
    2. confidence (float)
    3. detection_class (class identifier)

    Example:

    from deep_sort_realtime.deepsort_tracker import DeepSort
    
    tracker = DeepSort(max_age=5)
    # bbs expected to be a list of detections: ([left,top,w,h], confidence, detection_class)
    bbs = object_detector.detect(frame) 
    tracks = tracker.update_tracks(bbs, frame=frame)
    
    for track in tracks:
        if not track.is_confirmed():
            continue
        track_id = track.track_id
        ltrb = track.to_ltrb()
    from deep_sort_realtime.deepsort_tracker import DeepSort
    tracker = DeepSort(max_age=5)
    bbs = object_detector.detect(frame) 
    tracks = tracker.update_tracks(bbs, frame=frame)
    for track in tracks:
        if not track.is_confirmed():
            continue
        track_id = track.track_id
        ltrb = track.to_ltrb()
  5. Use a custom appearance embedder

    master

    If you have your own feature extraction logic, you can pass pre-computed embeddings directly to update_tracks. This avoids the need to pass the frame argument to the tracker.

    Example:

    from deep_sort_realtime.deepsort_tracker import DeepSort
    
    tracker = DeepSort(max_age=5)
    bbs = object_detector.detect(frame) # your own object detection
    object_chips = chipper(frame, bbs) # your own logic to crop frame based on bbox values
    embeds = embedder(object_chips) # your own embedder to take in the cropped object chips, and output feature vectors
    
    # Pass embeds directly; frame is no longer required
    tracks = tracker.update_tracks(bbs, embeds=embeds)
    
    for track in tracks:
        if not track.is_confirmed():
            continue
        track_id = track.track_id
        ltrb = track.to_ltrb()
    from deep_sort_realtime.deepsort_tracker import DeepSort
    tracker = DeepSort(max_age=5)
    bbs = object_detector.detect(frame)
    object_chips = chipper(frame, bbs)
    embeds = embedder(object_chips)
    tracks = tracker.update_tracks(bbs, embeds=embeds)
    for track in tracks:
        if not track.is_confirmed():
            continue
        track_id = track.track_id
        ltrb = track.to_ltrb()
  6. Store and retrieve supplementary detection info

    master

    You can pass extra data (like instance segmentation masks) from a detection to its associated track using the others argument in DeepSort.update_tracks. This argument expects a list of the same length as raw_detections.

    To retrieve this data from a track, use the Track.get_det_supplementary method.

  7. Retrieve original detection bounding boxes

    master

    By default, Track.to_* methods return Kalman predicted bounding box values. To retrieve the bounding box values from the original detection associated with the track in the current round, use the orig=True argument.

    • orig=True: Returns the original detection's bounding box if the track is associated with a detection this round; otherwise returns Kalman predicted values.
    • orig_strict=True: (Only active when orig=True) Returns None if there is no original detection associated with this track in the current frame; otherwise returns the original detection's bounding box.
  8. Configure Appearance Embedding Networks

    master

    The tracker supports several embedding options:

    Pytorch Embedders (Default)

    • Default: MobileNetV2 (trained on ImageNet).
    • Torchreid: A person re-identification library. Requires pip install torchreid gdown tensorboard.
      • Set embedder='torchreid' in DeepSort.
      • You can specify embedder_model_name and embedder_wts (weights file path).
    • CLIP: OpenAI's CLIP network.
      • Requires pip install git+https://github.com/openai/CLIP.git.
      • Provide weights via the embedder_wts argument.

    Tensorflow Embedder

    • An alternative to the Pytorch embedder located at deep_sort_realtime/embedder/embedder_tf.py.
    • Requires manual code changes to switch to this implementation.
    • Requires downloading mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5 and placing it in deep_sort_realtime/embedder/weights/.
    # Example using Torchreid
    from deep_sort_realtime.deepsort_tracker import DeepSort
    tracker = DeepSort(max_age=5, embedder='torchreid')