Spaces:
Sleeping
Sleeping
alihaider1001
commited on
Commit
·
b2b6006
1
Parent(s):
c5578a5
Updated app.py for Football Player Tracker
Browse files- app.py +20 -13
- main.py +8 -16
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -3,22 +3,31 @@ import gradio as gr
|
|
| 3 |
import tempfile
|
| 4 |
from ultralytics import YOLO
|
| 5 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
|
|
|
| 6 |
|
| 7 |
-
model
|
|
|
|
| 8 |
tracker = DeepSort(max_age=30)
|
| 9 |
|
| 10 |
def track_players(video):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
cap = cv2.VideoCapture(video
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
while True:
|
| 24 |
ret, frame = cap.read()
|
|
@@ -48,15 +57,13 @@ def track_players(video):
|
|
| 48 |
|
| 49 |
cap.release()
|
| 50 |
out.release()
|
| 51 |
-
|
| 52 |
return output_path
|
| 53 |
|
| 54 |
-
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=track_players,
|
| 57 |
inputs=gr.Video(label="Upload Match Video"),
|
| 58 |
outputs=gr.Video(label="Tracked Output"),
|
| 59 |
-
title="Football Player Tracker",
|
| 60 |
description="Upload a match video and get players tracked with consistent IDs."
|
| 61 |
)
|
| 62 |
|
|
|
|
| 3 |
import tempfile
|
| 4 |
from ultralytics import YOLO
|
| 5 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Load model and tracker
|
| 9 |
+
model = YOLO("yolov8n.pt")
|
| 10 |
tracker = DeepSort(max_age=30)
|
| 11 |
|
| 12 |
def track_players(video):
|
| 13 |
+
# Create temp output path
|
| 14 |
+
temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 15 |
+
output_path = temp_out.name
|
| 16 |
|
| 17 |
+
cap = cv2.VideoCapture(video)
|
| 18 |
+
if not cap.isOpened():
|
| 19 |
+
raise IOError("Video file could not be opened.")
|
|
|
|
| 20 |
|
| 21 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 22 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 23 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
|
| 24 |
+
|
| 25 |
+
out = cv2.VideoWriter(
|
| 26 |
+
output_path,
|
| 27 |
+
cv2.VideoWriter_fourcc(*"avc1"), # 'avc1' for Linux compatibility
|
| 28 |
+
fps,
|
| 29 |
+
(width, height)
|
| 30 |
+
)
|
| 31 |
|
| 32 |
while True:
|
| 33 |
ret, frame = cap.read()
|
|
|
|
| 57 |
|
| 58 |
cap.release()
|
| 59 |
out.release()
|
|
|
|
| 60 |
return output_path
|
| 61 |
|
|
|
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=track_players,
|
| 64 |
inputs=gr.Video(label="Upload Match Video"),
|
| 65 |
outputs=gr.Video(label="Tracked Output"),
|
| 66 |
+
title="🏟️ Football Player Tracker",
|
| 67 |
description="Upload a match video and get players tracked with consistent IDs."
|
| 68 |
)
|
| 69 |
|
main.py
CHANGED
|
@@ -1,50 +1,42 @@
|
|
| 1 |
import cv2
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
import numpy as np
|
| 4 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Load YOLOv8 model
|
| 8 |
-
|
| 9 |
-
model = YOLO(model_path)
|
| 10 |
-
|
| 11 |
-
# Initialize DeepSORT Tracker
|
| 12 |
tracker = DeepSort(max_age=30)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
video_path = r'C:\Users\alyhi\Desktop\Football Player Tracker\video\vid.mp4'
|
| 16 |
if not os.path.exists(video_path):
|
| 17 |
-
raise FileNotFoundError(f"Video file not found
|
| 18 |
|
| 19 |
-
# Load video
|
| 20 |
cap = cv2.VideoCapture(video_path)
|
| 21 |
if not cap.isOpened():
|
| 22 |
-
raise IOError(f"
|
| 23 |
|
| 24 |
while True:
|
| 25 |
ret, frame = cap.read()
|
| 26 |
if not ret:
|
| 27 |
break
|
| 28 |
|
| 29 |
-
# YOLOv8 detection
|
| 30 |
results = model(frame)[0]
|
| 31 |
detections = []
|
| 32 |
|
| 33 |
for result in results.boxes.data.tolist():
|
| 34 |
x1, y1, x2, y2, score, class_id = result
|
| 35 |
-
if int(class_id) == 0 and score > 0.4:
|
| 36 |
-
bbox = [x1, y1, x2 - x1, y2 - y1]
|
| 37 |
detections.append((bbox, score, 'player'))
|
| 38 |
|
| 39 |
-
# DeepSORT tracking
|
| 40 |
tracks = tracker.update_tracks(detections, frame=frame)
|
| 41 |
|
| 42 |
for track in tracks:
|
| 43 |
if not track.is_confirmed():
|
| 44 |
continue
|
| 45 |
-
|
| 46 |
track_id = track.track_id
|
| 47 |
-
l, t, r, b = track.to_ltrb()
|
| 48 |
cv2.rectangle(frame, (int(l), int(t)), (int(r), int(b)), (0, 255, 0), 2)
|
| 49 |
cv2.putText(frame, f'Player {track_id}', (int(l), int(t - 10)),
|
| 50 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
|
|
|
| 1 |
import cv2
|
| 2 |
from ultralytics import YOLO
|
|
|
|
| 3 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Load YOLOv8 model
|
| 7 |
+
model = YOLO('yolov8n.pt')
|
|
|
|
|
|
|
|
|
|
| 8 |
tracker = DeepSort(max_age=30)
|
| 9 |
|
| 10 |
+
# Local video file
|
| 11 |
video_path = r'C:\Users\alyhi\Desktop\Football Player Tracker\video\vid.mp4'
|
| 12 |
if not os.path.exists(video_path):
|
| 13 |
+
raise FileNotFoundError(f"Video file not found: {video_path}")
|
| 14 |
|
|
|
|
| 15 |
cap = cv2.VideoCapture(video_path)
|
| 16 |
if not cap.isOpened():
|
| 17 |
+
raise IOError(f"Could not open video: {video_path}")
|
| 18 |
|
| 19 |
while True:
|
| 20 |
ret, frame = cap.read()
|
| 21 |
if not ret:
|
| 22 |
break
|
| 23 |
|
|
|
|
| 24 |
results = model(frame)[0]
|
| 25 |
detections = []
|
| 26 |
|
| 27 |
for result in results.boxes.data.tolist():
|
| 28 |
x1, y1, x2, y2, score, class_id = result
|
| 29 |
+
if int(class_id) == 0 and score > 0.4:
|
| 30 |
+
bbox = [x1, y1, x2 - x1, y2 - y1]
|
| 31 |
detections.append((bbox, score, 'player'))
|
| 32 |
|
|
|
|
| 33 |
tracks = tracker.update_tracks(detections, frame=frame)
|
| 34 |
|
| 35 |
for track in tracks:
|
| 36 |
if not track.is_confirmed():
|
| 37 |
continue
|
|
|
|
| 38 |
track_id = track.track_id
|
| 39 |
+
l, t, r, b = track.to_ltrb()
|
| 40 |
cv2.rectangle(frame, (int(l), int(t)), (int(r), int(b)), (0, 255, 0), 2)
|
| 41 |
cv2.putText(frame, f'Player {track_id}', (int(l), int(t - 10)),
|
| 42 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|