import cv2
import argparse
import pandas as pd
from ultralytics import YOLO
import supervision as sv
import numpy as np
from supervision.tools.detections import Detections, BoxAnnotator
from supervision.tools.line_counter import LineCounter, LineCounterAnnotator
from supervision.draw.color import ColorPalette
from supervision import *
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="YOLOv8 live")
parser.add_argument(
"--webcam-resolution",
default=[1280, 720],
nargs=2,
type=int
)
args = parser.parse_args()
return args
def main():
args = parse_arguments()
frame_width, frame_height = args.webcam_resolution
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
model = YOLO("yolov8l.pt")
# dict maping class_id to class_name
CLASS_NAMES_DICT = model.model.names
# class_ids of interest - car, motorcycle, bus and truck
CLASS_ID = [2, 3, 5, 7, 9]
box_annotator = BoxAnnotator(
color= ColorPalette(),
thickness=2,
text_thickness=2,
text_scale=1
)
while True:
ret, frame = cap.read()
results = model(frame, agnostic_nms=True)[0]
detections = Detections(
xyxy=results[0].boxes.xyxy.cpu().numpy(),
confidence=results[0].boxes.conf.cpu().numpy(),
class_id=results[0].boxes.cls.cpu().numpy().astype(int)
)
labels = [
f"{CLASS_NAMES_DICT[class_id]} {confidence:0.2f}"
for _, confidence, class_id, _
in detections
]
frame = box_annotator.annotate(
frame =frame,
detections=detections,
labels=labels
)
cv2.imshow("yolov8", frame)
if (cv2. waitKey(20)& 0xFF==ord('d')):
break
if __name__ == "__main__":
main()
So the above code is me trying to use live camera feed to detect vehicles and to draw a line to detect entrace and exiting of the vehicles. The code runs but it crashes after a few seconds and the time before crash is not consistent. It also returns this error.
return Boxes(self.boxes[idx], self.orig_shape)
IndexError: index 0 is out of bounds for dimension 0 with size 0
(base)
I believe the code is ending when it doesn't detect anything. Could you help me find out the cause of this issue?
My guess is the problem is you do:
results = model(frame, agnostic_nms=True)[0]
and
xyxy=results[0].boxes.xyxy.cpu().numpy()
What I mean by this is that you access the first element of an iterable object two times - essentially, you do model(frame, agnostic_nms=True)[0][0]
.
Just replace corresponding lines with those:
results = model(frame, agnostic_nms=True)[0]
detections = Detections(
xyxy=results.boxes.xyxy.cpu().numpy(),
confidence=results.boxes.conf.cpu().numpy(),
class_id=results.boxes.cls.cpu().numpy().astype(int)
)