Spaces:
Sleeping
Sleeping
File size: 1,457 Bytes
58cde1d c5578a5 58cde1d b2b6006 58cde1d b2b6006 c5578a5 b2b6006 c5578a5 b2b6006 58cde1d b2b6006 c5578a5 58cde1d b2b6006 c5578a5 58cde1d c5578a5 58cde1d |
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 |
import cv2
from ultralytics import YOLO
from deep_sort_realtime.deepsort_tracker import DeepSort
import os
# Load YOLOv8 model
model = YOLO('yolov8n.pt')
tracker = DeepSort(max_age=30)
# Local video file
video_path = r'C:\Users\alyhi\Desktop\Football Player Tracker\video\vid.mp4'
if not os.path.exists(video_path):
raise FileNotFoundError(f"Video file not found: {video_path}")
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise IOError(f"Could not open video: {video_path}")
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)[0]
detections = []
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if int(class_id) == 0 and score > 0.4:
bbox = [x1, y1, x2 - x1, y2 - y1]
detections.append((bbox, score, 'player'))
tracks = tracker.update_tracks(detections, frame=frame)
for track in tracks:
if not track.is_confirmed():
continue
track_id = track.track_id
l, t, r, b = track.to_ltrb()
cv2.rectangle(frame, (int(l), int(t)), (int(r), int(b)), (0, 255, 0), 2)
cv2.putText(frame, f'Player {track_id}', (int(l), int(t - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.imshow("⚽ Football Player Tracker", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
|