Skip to content

Track Objects on Video (inference) example not retrieving class name correctly #1827

Open
@fmousinho

Description

@fmousinho

Search before asking

  • I have searched the Supervision issues and found no similar bug report.

Bug

In the example in # https://supervision.roboflow.com/develop/how_to/track_objects/#annotate-video-with-tracking-ids, we see label array assignment below. However, "results" does not have the attribute "names" which yields an error when running the code.

labels = [
        f"#{tracker_id} {results.names[class_id]}"
        for class_id, tracker_id
        in zip(detections.class_id, detections.tracker_id)
    ]

I was able to solve this by using the data attribute instead:

  labels = [
        f"#{tracker_id} {class_name}"
        for class_name, tracker_id
        in zip(detections.data['class_name'], detections.tracker_id)
    ]

Traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[23], line 26
     21     annotated_frame = box_annotator.annotate(
     22         frame.copy(), detections=detections)
     23     return label_annotator.annotate(
     24         annotated_frame, detections=detections, labels=labels)
---> 26 sv.process_video(
     27     source_path=VIDEO_URL,
     28     target_path="./result.mp4",
     29     callback=callback
     30 )

File ~/Documents/Learning_to_Code/roboflow/roboenv/lib/python3.12/site-packages/supervision/utils/video.py:230, in process_video(source_path, target_path, callback)
    226 with VideoSink(target_path=target_path, video_info=source_video_info) as sink:
    227     for index, frame in enumerate(
    228         get_video_frames_generator(source_path=source_path)
    229     ):
--> 230         result_frame = callback(frame, index)
    231         sink.write_frame(frame=result_frame)

Cell In[23], line 16, in callback(frame, _)
     12 detections = sv.Detections.from_inference(results)
     13 detections = tracker.update_with_detections(detections)
     15 labels = [
---> 16     f"#{tracker_id} {results.names[class_id]}"
     17     for class_id, tracker_id
     18     in zip(detections.class_id, detections.tracker_id)
     19 ]
     21 annotated_frame = box_annotator.annotate(
     22     frame.copy(), detections=detections)
     23 return label_annotator.annotate(
     24     annotated_frame, detections=detections, labels=labels)

File ~/Documents/Learning_to_Code/roboflow/roboenv/lib/python3.12/site-packages/pydantic/main.py:994, in BaseModel.__getattr__(self, item)
    991     return super().__getattribute__(item)  # Raises AttributeError if appropriate
    992 else:
    993     # this is the current error
--> 994     raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')

AttributeError: 'ObjectDetectionInferenceResponse' object has no attribute 'names'

Environment

  • Supervision: 0.25.1
  • OS: MacOS 15.4.1
  • Python 3.12.10

Minimal Reproducible Example

Use code in doc example at # https://supervision.roboflow.com/develop/how_to/track_objects/#annotate-video-with-tracking-ids

Additional

First time I file a bug.. Constructive criticism welcome!

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions