trackers icon indicating copy to clipboard operation
trackers copied to clipboard

Tracking fails with custom video

Open picjul opened this issue 1 year ago • 8 comments

Hello!

Trying to do tracking on a video sequence from Kaggle here's the link, in MOV format I encounter this problem, from the first frame:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[<ipython-input-1-3941a463781d>](https://localhost:8080/#) in <cell line: 0>()
     14     return annotator.annotate(frame, detections, detections.tracker_id)
     15 
---> 16 sv.process_video(
     17     source_path="/content/input.mov",
     18     target_path="/content/output.mov",

4 frames
[/usr/local/lib/python3.11/dist-packages/supervision/annotators/core.py](https://localhost:8080/#) in _get_label_properties(self, detections, labels)
   1183         for label, center_coords in zip(labels, anchors_coordinates):
   1184             print(label)
-> 1185             (text_w, text_h) = cv2.getTextSize(
   1186                 text=label,
   1187                 fontFace=CV2_FONT,

TypeError: Can't convert object to 'str' for 'text'

Detections are done correctly, but an exception is thrown when “writing” the labels to the output video images. These are the detections provided by model.predict(frame) and tracker.update(detections):

# Predict
Detections(xyxy=array([[288.5417 , 396.33487, 436.9482 , 507.0605 ],
       [478.7965 , 412.24393, 525.56165, 449.1659 ],
       [538.68396, 418.6501 , 556.72406, 431.59842],
       [566.6572 , 417.84848, 592.8051 , 440.30402]], dtype=float32), mask=None, confidence=array([0.917346  , 0.8645104 , 0.7171507 , 0.70425004], dtype=float32), class_id=array([3, 3, 3, 3]), tracker_id=None, data={}, metadata={})
#Tracker
Detections(xyxy=array([[288.5417 , 396.33487, 436.9482 , 507.0605 ],
       [478.7965 , 412.24393, 525.56165, 449.1659 ],
       [538.68396, 418.6501 , 556.72406, 431.59842],
       [566.6572 , 417.84848, 592.8051 , 440.30402]], dtype=float32), mask=None, confidence=array([0.917346  , 0.8645104 , 0.7171507 , 0.70425004], dtype=float32), class_id=array([3, 3, 3, 3]), tracker_id=array([-1, -1, -1, -1]), data={}, metadata={})

It seems related to -1 values in tracker_id array. Setting str(label) in supervision/annotators/core.py in _draw_labels and _get_label_properties seems to be a workaround: I'm able to get the output video with labelled detections.

Thanks!

How to reproduce the issue

On COLAB:

  1. Install rfdetr and trackers
> pip install trackers
> pip install rfdetr
  1. Copy sample code from README, adding some print for debug:
import supervision as sv
from rfdetr import RFDETRBase
from trackers.sort_tracker import SORTTracker

model = RFDETRBase()
tracker = SORTTracker()
annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)

def callback(frame, _):
    detections = model.predict(frame)
    print(detections)
    detections = tracker.update(detections)
    print(detections)
    return annotator.annotate(frame, detections, detections.tracker_id)

sv.process_video(
    source_path="/content/input.mov",
    target_path="/content/output.mov",
    callback=callback,
)

picjul avatar Apr 18 '25 09:04 picjul

Ideally, tracks with tracker ID -1 are to be filtered out, since they are not yet mature enough to be tracked. You can insert the following line:

detections = tracker.update(detections)

# Filter out detections with IDs of -1 (fresh tracks not yet confirmed)
detections = detections[detections.tracker_id != -1]

You can check our quickstart colab for reference.

Thanks for pointing this out. We will update the docs and the readme before the official release.

soumik12345 avatar Apr 20 '25 07:04 soumik12345

import supervision as sv
from rfdetr import RFDETRBase
from trackers import SORTTracker
import os

model = RFDETRBase()
tracker = SORTTracker()
annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)

def callback(frame, _):
    detections = model.predict(frame)
    # print(detections)
    detections = tracker.update(detections)

    # Filter out detections with IDs of -1 (fresh tracks not yet confirmed)
    detections = detections[detections.tracker_id != -1]
    print(detections)
    
    # Convert tracker_id to string and handle empty tracker_id
    if detections.tracker_id is None or len(detections.tracker_id) == 0:
        detections.tracker_id = ["Unknown"] * len(detections)  # Handle empty tracker_id or None
    else:
        detections.tracker_id = [str(id) if id is not None else "Unknown" for id in detections.tracker_id]
    
    return annotator.annotate(frame, detections, detections.tracker_id)


SOURCE_VIDEO_PATH = "./data/Camera10_20250418_203651_20fps_clip_000.mp4"
TARGET_VIDEO_PATH = "./results/Camera10_20250418_203651_20fps_clip_000.mp4"


# Create directory for TARGET_VIDEO_PATH if it does not exist
target_dir = os.path.dirname(TARGET_VIDEO_PATH)
if not os.path.exists(target_dir):
    os.makedirs(target_dir)

sv.process_video(
    source_path=SOURCE_VIDEO_PATH,
    target_path=TARGET_VIDEO_PATH,
    callback=callback,
)

This works!

Please check.

Pranjal-neo avatar Apr 23 '25 13:04 Pranjal-neo

Hi @Pranjal-neo , thanks for the solution! It seems like the failure is caused when no objects are detected in a frame, i.e, the sv. Detections passed to the tracker don't consist of any detected objects. IMO, we should add a filter to the trackers to handle this scenario.

@SkalskiP I would love to know you'd thoughts.

soumik12345 avatar Apr 23 '25 16:04 soumik12345

@soumik12345 First of all, it looks like the bug reported by @picjul and your PR #23 are two separate issues. Support for -1 tracker ID was added to Supervision today via https://github.com/roboflow/supervision/pull/1826. I'm reviewing your PR now.

SkalskiP avatar Apr 23 '25 20:04 SkalskiP

Hi @SkalskiP and @soumik12345 – glad the snippet helped! You’re absolutely right that we need to guard against empty detections. In my testing I ran into three edge cases:

  1. tracker_id == -1
    Fresh tracks show up with ID –1 until they’re confirmed. I’m already filtering those out with
    detections  = detections[detections.tracker_id != -1]
    
  2. Empty tracker_id list
    If no objects make it through the tracker, detections.tracker_id can be None or an empty list. I’m handling that by initializing unknown IDs:
    if not detections.tracker_id:
        detections.tracker_id = ["Unknown"] * len(detections)
    
  3. Missing output directory
    sv.process_video doesn’t auto‐create nested folders, so I added:
    os.makedirs(os.path.dirname(TARGET_VIDEO_PATH), exist_ok=True)
    

Putting it all together in the callback ensures we never pass an empty or invalid set of detections back to the tracker (and that our output path always exists).

Would love your thoughts on this approach 😊

Pranjal-neo avatar Apr 24 '25 07:04 Pranjal-neo

Hey @Pranjal-neo! 👋🏻 Could you please try reinstalling the trackers using this command: pip install trackers==2.0.0, and then run your demo without these lines of code:

detections  = detections[detections.tracker_id != -1]

and this one:

if not detections.tracker_id:
    detections.tracker_id = ["Unknown"] * len(detections)

I've been working on several fixes recently that should significantly improve usability.

SkalskiP avatar Apr 24 '25 12:04 SkalskiP

Hi @SkalskiP—sorry for the delay! I’ve reinstalled trackers==2.0.0, removed those lines, and the demo runs smoothly now. Thanks for the fix! 😊

Pranjal-neo avatar Apr 27 '25 08:04 Pranjal-neo

Hi @Pranjal-neo 👋🏻 this is absolutely awesome! Thanks for letting me know. I'm closing the issue.

SkalskiP avatar Apr 27 '25 16:04 SkalskiP