dschandra commited on
Commit
20f24a7
·
verified ·
1 Parent(s): 5cca8ca

Update utils/video_processing.py

Browse files
Files changed (1) hide show
  1. utils/video_processing.py +24 -2
utils/video_processing.py CHANGED
@@ -4,14 +4,30 @@ import numpy as np
4
  from ultralytics import YOLO
5
  import os
6
 
7
- # Load YOLO model (assumed to be trained for cricket objects)
8
- model = YOLO('models/yolov8_model.pt')
 
 
 
 
 
 
 
 
 
 
9
 
10
  def track_ball(video_path: str) -> list:
11
  """
12
  Track the ball in the video and return its trajectory as a list of (x, y) coordinates.
13
  """
 
 
 
14
  cap = cv2.VideoCapture(video_path)
 
 
 
15
  tracker = cv2.TrackerKCF_create()
16
  trajectory = []
17
  init = False
@@ -45,7 +61,13 @@ def generate_replay(video_path: str, trajectory: list, decision: str) -> str:
45
  """
46
  Generate a slow-motion replay video with ball trajectory and decision overlay.
47
  """
 
 
 
48
  cap = cv2.VideoCapture(video_path)
 
 
 
49
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
50
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
51
  fps = cap.get(cv2.CAP_PROP_FPS) / 2 # Slow motion (half speed)
 
4
  from ultralytics import YOLO
5
  import os
6
 
7
+ # Path to the YOLO model
8
+ MODEL_PATH = 'models/yolov8_model.pt'
9
+
10
+ # Check if model file exists
11
+ if not os.path.exists(MODEL_PATH):
12
+ raise FileNotFoundError(f"YOLO model file not found at {MODEL_PATH}. Please ensure 'yolov8_model.pt' is in the 'models/' directory.")
13
+
14
+ # Load YOLO model
15
+ try:
16
+ model = YOLO(MODEL_PATH)
17
+ except Exception as e:
18
+ raise RuntimeError(f"Failed to load YOLO model from {MODEL_PATH}: {str(e)}")
19
 
20
  def track_ball(video_path: str) -> list:
21
  """
22
  Track the ball in the video and return its trajectory as a list of (x, y) coordinates.
23
  """
24
+ if not os.path.exists(video_path):
25
+ raise FileNotFoundError(f"Video file not found at {video_path}")
26
+
27
  cap = cv2.VideoCapture(video_path)
28
+ if not cap.isOpened():
29
+ raise ValueError(f"Failed to open video file: {video_path}")
30
+
31
  tracker = cv2.TrackerKCF_create()
32
  trajectory = []
33
  init = False
 
61
  """
62
  Generate a slow-motion replay video with ball trajectory and decision overlay.
63
  """
64
+ if not os.path.exists(video_path):
65
+ raise FileNotFoundError(f"Video file not found at {video_path}")
66
+
67
  cap = cv2.VideoCapture(video_path)
68
+ if not cap.isOpened():
69
+ raise ValueError(f"Failed to open video file: {video_path}")
70
+
71
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
72
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
73
  fps = cap.get(cv2.CAP_PROP_FPS) / 2 # Slow motion (half speed)