Deep sort code path
xxxxxxxxxx
/home/yahboom/YBAMR-COBOT-EDU-00001/soft/yolov8
As in the previous section, we can see the file path of our Deep sort from the figure below.
The following project directory code class diagram
We start the demo program. The target tracking startup file is the yolov8_track.py program. We enter the command in the terminal:
xxxxxxxxxx
cd /home/yahboom/YBAMR-COBOT-EDU-00001/soft/yolov8
python yolov8_track.py
Then the interface pops up as shown below.
The demo program started above uses yolov8 as the target detection model, and then uses deep sort for target tracking. We can see that each target label will have an ID for the target. If the target is always within the detection range, the ID will remain unchanged.
Code running flow chart:
Code:
xxxxxxxxxx
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('./weights/yolov8n.pt')
# Open the video file
video_path = "demo.mp4"
cap = cv2.VideoCapture(video_path)
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("YOLOv8 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()