Repository navigation
Expand file tree
/
Copy pathtrack.py
More file actions
56 lines (39 loc) · 1.56 KB
/
Copy pathtrack.py
File metadata and controls
56 lines (39 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# from ultralytics import YOLO
# import matplotlib.pyplot as plt
# # Load an official or custom model
# model = YOLO("best.pt") # Load an official Detect model
# # Perform tracking with the model
# results = model.track(r"E:\Muqadas\projects\plant diseases prediction\YOLO\code\5243389-hd_1920_1080_25fps.mp4", show=True)
# for result in results:
# print(result.boxes) # Boxes object for bbox outputs
# print(result.masks) # Masks object for segm outputs
# print(result.keypoints) # Keypoints object for keypoint outputs
# print(result.probs) # Class probabilities for cls outputs
# plt.show()
import cv2
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("best.pt")
# Open the video file
video_path = r"apple.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 YOLO11 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("YOLO8 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()