trackers icon indicating copy to clipboard operation
trackers copied to clipboard

feature proposal: `QueryTracker`, a rule-based tracker using pandas-style expressions

Open juan-cobos opened this issue 2 months ago • 1 comments

Hi, thanks for your work on this library, I'm really impressed by it :)

I'd like to suggest a feature that's been on my mind for a few days. In many practical settings, I end up wanting a tracker based on a simple rule, since that alone would prevent most ID switches. For example, when there's a fixed number of objects in a known layout, a rule like "the top one is always ID 0" is often more reliable than appearance or motion matching.

So I thought of a QueryTracker, inspired by the pandas query mechanism. In practice, this is the post-processing step I end up applying to other trackers' output anyway, so having it built in would save that extra step.

The idea is to expose the same interface as the existing trackers and operate on sv.Detections. IDs would be assigned according to a user-defined expression. A few examples of rules it could handle:

  • id0.y > id1.y: fixed vertical layout (top vs. bottom)
  • id0.x < id1.x: fixed horizontal layout (left vs. right)
  • id0.area > id1.area: one detection is consistently larger
  • id0.x < 400: a rule involving a single detection
  • id0.class_id == 0: track only detections of a particular class
  • id0.class_id == 0 and id1.class_id == 2: one ID per class
  • id0.class_id in [0, 1] and id0.xyxy[0] < 300: a subset of classes whose left edge (x1) is within the first 300 pixels

Usage could look something like this:

tracker = QueryTracker(expr="id0.y > id1.y")
detections = tracker.update(detections)

I put together a naive implementation that works for my use case, but I'm not sure what the right interface would be if you wanted to include it in trackers. Some open questions I had:

  • How should it behave when the number of detections doesn't match the number of IDs in the expression?
  • Should detections that don't match any ID in the expression be dropped, or passed through with tracker_id = -1?
  • Could it be layered on top of an existing tracker as a correction step, rather than being a standalone tracker?

Let me know if this makes sense and whether you think trackers could benefit from it. Ideas on the syntax are very welcome, and I'd be happy to open a PR if there's interest.

Thanks! Juan

juan-cobos avatar Sep 25 '26 09:09 juan-cobos

This issue has not had activity in 28 days. If this is still relevant, please add context or a minimal reproducible example. Inactive issues are deprioritized over time.

github-actions[bot] avatar Oct 24 '26 00:10 github-actions[bot]