AjaykumarPilla commited on
Commit
002e9aa
·
verified ·
1 Parent(s): ef9f7bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -12,7 +12,7 @@ import tempfile
12
  model = YOLO("best.pt")
13
  model.to('cuda' if torch.cuda.is_available() else 'cpu')
14
 
15
- # Resolve class index for cricket ball
16
  ball_class_index = None
17
  for k, v in model.names.items():
18
  if v.lower() == "cricketball":
@@ -25,7 +25,7 @@ if ball_class_index is None:
25
  STUMPS_WIDTH = 0.2286
26
  BALL_DIAMETER = 0.073
27
  FRAME_RATE = 20
28
- SLOW_MOTION_FACTOR = 2 # Set to 1 for normal speed replay
29
  CONF_THRESHOLD = 0.2
30
  IMPACT_ZONE_Y = 0.85
31
  IMPACT_DELTA_Y = 50
@@ -48,7 +48,10 @@ def process_video(video_path):
48
  break
49
  frame_count += 1
50
  frames.append(frame.copy())
51
- results = model.predict(frame, conf=CONF_THRESHOLD, imgsz=(frame_height, frame_width), iou=0.5, max_det=1)
 
 
 
52
  detections = 0
53
  for detection in results[0].boxes:
54
  if int(detection.cls) == ball_class_index:
@@ -58,10 +61,11 @@ def process_video(video_path):
58
  ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
59
  detection_frames.append(frame_count - 1)
60
  cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
 
61
  frames[-1] = frame
62
  debug_log.append(f"Frame {frame_count}: {detections} ball detections")
63
- cap.release()
64
 
 
65
  return frames, ball_positions, detection_frames, "\n".join(debug_log)
66
 
67
  def find_bounce_point(ball_coords):
 
12
  model = YOLO("best.pt")
13
  model.to('cuda' if torch.cuda.is_available() else 'cpu')
14
 
15
+ # Resolve ball class index
16
  ball_class_index = None
17
  for k, v in model.names.items():
18
  if v.lower() == "cricketball":
 
25
  STUMPS_WIDTH = 0.2286
26
  BALL_DIAMETER = 0.073
27
  FRAME_RATE = 20
28
+ SLOW_MOTION_FACTOR = 1 # Normal speed; increase to slow down
29
  CONF_THRESHOLD = 0.2
30
  IMPACT_ZONE_Y = 0.85
31
  IMPACT_DELTA_Y = 50
 
48
  break
49
  frame_count += 1
50
  frames.append(frame.copy())
51
+
52
+ # 🚀 Faster inference with fixed optimized size
53
+ results = model.predict(frame, conf=CONF_THRESHOLD, imgsz=640, iou=0.5, max_det=1)
54
+
55
  detections = 0
56
  for detection in results[0].boxes:
57
  if int(detection.cls) == ball_class_index:
 
61
  ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
62
  detection_frames.append(frame_count - 1)
63
  cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
64
+
65
  frames[-1] = frame
66
  debug_log.append(f"Frame {frame_count}: {detections} ball detections")
 
67
 
68
+ cap.release()
69
  return frames, ball_positions, detection_frames, "\n".join(debug_log)
70
 
71
  def find_bounce_point(ball_coords):