First, See The Video below then see the code to do Object Detection + Segmentation and Tracking in that
Now, See The Python Code for Video above
import numpy as np
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolov9e-seg.pt")
tracker = sv.ByteTrack()
box_annotator = sv.MaskAnnotator()
label_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK)
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update_with_detections(detections)
labels = [
f"#{tracker_id} {results.names[class_id]}"
for class_id, tracker_id
in zip(detections.class_id, detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
annotated_frame = label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="1.mp4",
target_path="1-result_2.mp4",
callback=callback
)