SkalskiP / yolov8-native-tracking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error when change to video

moahaimen opened this issue · comments

hi thank you for your work
the work is excellent on webcam , but when i make changes on the code to make it work on video
this is the modified code

import cv2

from ultralytics import YOLO
import supervision as sv
import numpy as np

LINE_START = sv.Point(320, 0)
LINE_END = sv.Point(320, 480)


def main():
    line_counter = sv.LineZone(start=LINE_START, end=LINE_END)
    line_annotator = sv.LineZoneAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
    box_annotator = sv.BoxAnnotator(
        thickness=2,
        text_thickness=1,
        text_scale=0.5
    )

    model = YOLO("yolov8l.pt")

    # Define the codec and create a VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
    out = cv2.VideoWriter('Outputvideo.mp4', fourcc, 20.0, (640, 480))

    for result in model.track(source='your_video_file.mp4', show=False, stream=False, 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)
        
        detections = detections[(detections.class_id != 60) & (detections.class_id != 0)]

        labels = [
            f"{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
            for _, confidence, class_id, tracker_id
            in detections
        ]

        frame = box_annotator.annotate(
            scene=frame, 
            detections=detections,
            labels=labels
        )

        line_counter.trigger(detections=detections)
        line_annotator.annotate(frame=frame, line_counter=line_counter)

        # Write the frame to the output file
        out.write(frame)

    # Release the VideoWriter and VideoCapture objects
    out.release()

if __name__ == "__main__":
    main()

but when i change it to video i have this error

ValueError                                Traceback (most recent call last)
<ipython-input-10-fe39b167ccdf> in <cell line: 58>()
     57 
     58 if __name__ == "__main__":
---> 59     main()

1 frames
<ipython-input-10-fe39b167ccdf> in <listcomp>(.0)
     37         labels = [
     38             f"{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
---> 39             for _, confidence, class_id, tracker_id
     40             in detections
     41         ]

ValueError: too many values to unpack (expected 4)

i wish you can help me solving the problem

Duplicate: #2