使用supervision追踪

import supervision as sv
from ultralytics import YOLO


model_path=r'F:\python\yolov8\yolov8n.pt'
model=YOLO(model_path)
box_annotator=sv.BoundingBoxAnnotator()
label_annotator=sv.LabelAnnotator()
trace_annotator=sv.TraceAnnotator()
tracker=sv.ByteTrack()
def callback(frame,index):
    result=model(frame)[0]
    detections=sv.Detections.from_ultralytics(result)
    # 追踪
    detections=tracker.update_with_detections(detections)
    # 显示跟踪id
    labels=[
        f'{tracker_id} {result.names[class_id]}'
        for class_id,tracker_id in zip(detections.class_id,detections.tracker_id)
    ]
    # 注释边框
    annotated_frame=box_annotator.annotate(scene=frame.copy(),detections=detections)
    # 注释文本
    annotated_frame=label_annotator.annotate(scene=annotated_frame,detections=detections,labels=labels)
    # 注释轨迹
    annotated_frame=trace_annotator.annotate(scene=annotated_frame,detections=detections)
    return annotated_frame

# 处理视频
sv.process_video(source_path='people-walking.mp4',target_path='people_walking_tracked.mp4',callback=callback)

 

 posted on 2024-07-21 18:06  会飞的金鱼  阅读(16)  评论(0)    收藏  举报