Error while using polygon zone #133
-
Search before asking
BugWhen I was trying to use the polygon zone and trying to trigger the count, the output video runs for sometime and throws this error
Environment
Minimal Reproducible Example`import cv2
from ultralytics import YOLO
import supervision as sv
import numpy as np
polygon = np.array([
[409, 226],
[327, 235],
[327, 204],
[398, 198],
])
video_info = sv.VideoInfo.from_video_path('./test.mp4')
zone = sv.PolygonZone(polygon=polygon, frame_resolution_wh=video_info.resolution_wh)
`
def main():
global model
zone_annotator = sv.PolygonZoneAnnotator(zone=zone, color=sv.Color.white(), thickness=1, text_thickness=1, text_scale=0.5)
box_annotator = sv.BoxAnnotator(
thickness=2,
text_thickness=1,
text_scale=0.5
)
model = YOLO("yolov8n.pt")
for result in model.track(source='./test.mp4', classes=0, imgsz= 480, show=True, stream=True,
agnostic_nms=True):
frame = result.orig_img
detections = sv.Detections.from_yolov8(result)
if result.boxes.id is not None:
detections.tracker_id = result.boxes.id.cpu().numpy().astype(int)
else:
detections.tracker_id = np.array([])
detections.conf = np.array([])
detections.xyxy = np.array([])
# detections = detections[(detections.class_id != 60) & (detections.class_id != 0)]
labels=[str(id) for id in detections.tracker_id]
frame = box_annotator.annotate(
scene=frame,
detections=detections,
labels=labels
)
zone.trigger(detections=detections)
zone_annotator.annotate(scene=frame)
print(output)
cv2.imshow("yolov8", frame)
if (cv2.waitKey(30) == 27):
break
if __name__ == "__main__":
main() Additionalsame code works fine with Linezone, I'm trying to do both in my code Are you willing to submit a PR?
|
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 3 replies
-
Hi @uzumakinaruto19 👋🏻 ! Could you give me a full stack trace? I'm pretty sure it is not full error message:
|
Beta Was this translation helpful? Give feedback.
-
Also, can you share |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I cant share the video, but I have tried with 2 videos both failed around 118 and 230th frame |
Beta Was this translation helpful? Give feedback.
-
It will be hard for me to help you if I can't reproduce the error. Can you change your code like this 👇🏻 ? We want to wrap try:
zone.trigger(detections=detections)
catch IndexError:
print(detections.xyxy)
print(detections.xyxy.shape) |
Beta Was this translation helpful? Give feedback.
-
it worked when I put a try and except statement |
Beta Was this translation helpful? Give feedback.
-
This is not a solution :)) This is just a workaround. I assume there was some output/log when you executed it. Could you paste it here? |
Beta Was this translation helpful? Give feedback.
-
I didn't try catch Indexerror statement,
these are the output from the prints statements I think it happened while the detection is empty |
Beta Was this translation helpful? Give feedback.
-
Yup, it looks like it is the case. I'll work on the fix today. But it won't be accessible in |
Beta Was this translation helpful? Give feedback.
-
Okay, I know what's up. It looks like the problem is in your code. Please change: if result.boxes.id is not None:
detections.tracker_id = result.boxes.id.cpu().numpy().astype(int)
else:
detections.tracker_id = np.array([])
detections.conf = np.array([])
detections.xyxy = np.array([]) Into: if result.boxes.id is not None:
detections.tracker_id = result.boxes.id.cpu().numpy().astype(int)
else:
detections.tracker_id = np.array([]) I don't think you need. detections.conf = np.array([])
detections.xyxy = np.array([]) |
Beta Was this translation helpful? Give feedback.
-
hey, I tried that, and got a new error,
`
File "~/webrtc-streaming/counter.py", line 44, in trigger
File "~/anaconda3/envs/lobby/lib/python3.9/site-packages/supervision/detection/core.py", line 108, in iter
IndexError: index 0 is out of bounds for axis 0 with size 0 |
Beta Was this translation helpful? Give feedback.
-
Since, at this stage, we are discussing details related to your code and not potential problems with the library, I am converting this issue into a discussion. |
Beta Was this translation helpful? Give feedback.
-
@uzumakinaruto19 okey so in that case do it like this: if result.boxes.id is not None:
detections.tracker_id = result.boxes.id.cpu().numpy().astype(int)
else:
detections.tracker_id = np.array([])
detections.conf = np.array([])
detections.xyxy=np.empty((0, 4), dtype=np.float32) |
Beta Was this translation helpful? Give feedback.
Hi @Archangel31
Can you please try this supervision version?