Skip to content

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() #1911

@wyd1520

Description

@wyd1520

Search before asking

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

Bug

I received the following error after running the code below。But the same code, however, runs successfully on version 0.25.0。This code is a modified version of the one from https://github.com/roboflow/supervision/blob/develop/examples/time_in_zone/ultralytics_file_example.py.

`
CODE:


import argparse

import cv2
import numpy as np
from ultralytics import YOLO
from utils.general import find_in_list, load_zones_config
from utils.timers import FPSBasedTimer

import supervision as sv

COLORS = sv.ColorPalette.from_hex(["#E6194B", "#3CB44B", "#FFE119", "#3C76D1"])
COLOR_ANNOTATOR = sv.ColorAnnotator(color=COLORS)
LABEL_ANNOTATOR = sv.LabelAnnotator(
    color=COLORS, text_color=sv.Color.from_hex("#000000")
)


def main(
        source_video_path: str,
        zone_configuration_path: str,
        weights: str,
        device: str,
        confidence: float,
        iou: float,
        classes: list[int],
) -> None:
    model = YOLO(weights)
    tracker = sv.ByteTrack(minimum_matching_threshold=0.5)
    video_info = sv.VideoInfo.from_video_path(video_path=source_video_path)
    frames_generator = sv.get_video_frames_generator(source_video_path)

    polygons = load_zones_config(file_path=zone_configuration_path)
    zones = [
        sv.PolygonZone(
            polygon=polygon,
            triggering_anchors=(sv.Position.CENTER,),
        )
        for polygon in polygons
    ]
    timers = [FPSBasedTimer(video_info.fps) for _ in zones]

    for frame in frames_generator:
        results = model(frame, verbose=False, device=device, conf=confidence)[0]
        detections = sv.Detections.from_ultralytics(results)
        detections = detections[find_in_list(detections.class_id, classes)]
        detections = detections.with_nms(threshold=iou)
        detections = tracker.update_with_detections(detections)

        annotated_frame = frame.copy()

        for idx, zone in enumerate(zones):
            annotated_frame = sv.draw_polygon(
                scene=annotated_frame, polygon=zone.polygon, color=COLORS.by_idx(idx)
            )

            detections_in_zone = detections[zone.trigger(detections)]
            time_in_zone = timers[idx].tick(detections_in_zone)
            custom_color_lookup = np.full(detections_in_zone.class_id.shape, idx)

            annotated_frame = COLOR_ANNOTATOR.annotate(
                scene=annotated_frame,
                detections=detections_in_zone,
                custom_color_lookup=custom_color_lookup,
            )
            labels = [
                f"#{tracker_id} {int(time // 60):02d}:{int(time % 60):02d}"
                for tracker_id, time in zip(detections_in_zone.tracker_id, time_in_zone)
            ]
            annotated_frame = LABEL_ANNOTATOR.annotate(
                scene=annotated_frame,
                detections=detections_in_zone,
                labels=labels,
                custom_color_lookup=custom_color_lookup,
            )

        cv2.imshow("Processed Video", annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    cv2.destroyAllWindows()

`
Error Info:

G:\PythonDemo\python_supervision\venv\Scripts\python.exe G:\PythonDemo\python_supervision\demo_track\03_time_in_zone.py 
Traceback (most recent call last):
  File "G:\PythonDemo\python_supervision\demo_track\03_time_in_zone.py", line 104, in <module>
    main(
  File "G:\PythonDemo\python_supervision\demo_track\03_time_in_zone.py", line 60, in main
    annotated_frame = COLOR_ANNOTATOR.annotate(
  File "G:\PythonDemo\python_supervision\venv\lib\site-packages\supervision\utils\conversion.py", line 22, in wrapper
    return annotate_func(self, scene, *args, **kwargs)
  File "G:\PythonDemo\python_supervision\venv\lib\site-packages\supervision\annotators\core.py", line 566, in annotate
    color = resolve_color(
  File "G:\PythonDemo\python_supervision\venv\lib\site-packages\supervision\annotators\utils.py", line 145, in resolve_color
    if color_lookup == ColorLookup.TRACK and idx == PENDING_TRACK_ID:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

### Environment

-SuperVersion 0.26.1
-Windows 11
-Python 3.9.13


### Minimal Reproducible Example

_No response_

### Additional

_No response_

### 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