import cv2 import csv import os import numpy as np from ultralytics import YOLO import torch import torch.nn as nn from torchvision import transforms from torchvision.models import resnet18 from PIL import Image import warnings import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from tqdm import tqdm from collections import deque, defaultdict, Counter from sklearn.linear_model import LinearRegression, RANSACRegressor from collections import defaultdict import random seed = 42 torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) np.random.seed(seed) random.seed(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False print(f"✅ Set random seed {seed} for reproducibility") warnings.filterwarnings('ignore') def setup_gpu(): """Configure GPU settings with fallback to CPU""" import torch gpu_info = {} if torch.cuda.is_available(): device = 'cuda' gpu_name = torch.cuda.get_device_name(0) gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3 torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False torch.backends.cudnn.benchmark = True torch.set_float32_matmul_precision('highest') gpu_info = { 'device': device, 'name': gpu_name, 'memory_gb': gpu_memory, 'cuda_version': torch.version.cuda, 'optimizations': ['FP32', 'cuDNN-benchmark'] } print(f"✅ GPU DETECTED: {gpu_name}") print(f" CUDA Version: {torch.version.cuda}") print(f" Memory: {gpu_memory:.2f} GB") print(f" Optimizations: FP32={not torch.backends.cuda.matmul.allow_tf32}, " f"cuDNN-benchmark={torch.backends.cudnn.benchmark}") OPENCV_CUDA_AVAILABLE = False try: opencv_cuda_devices = cv2.cuda.getCudaEnabledDeviceCount() if opencv_cuda_devices > 0: OPENCV_CUDA_AVAILABLE = True print(f" OpenCV CUDA: Available ({opencv_cuda_devices} devices)") else: print(" OpenCV CUDA: Not available (CPU fallback)") except: print(" OpenCV CUDA: Not available (CPU fallback)") gpu_info['opencv_cuda_available'] = OPENCV_CUDA_AVAILABLE else: device = 'cpu' if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): device = 'mps' print(f"✅ Apple Silicon MPS detected (M-series chip)") else: print(f"⚠️ No GPU detected, using CPU") gpu_info = { 'device': device, 'name': 'CPU', 'memory_gb': 0, 'cuda_version': None, 'optimizations': [], 'opencv_cuda_available': False } return gpu_info GPU_INFO = setup_gpu() DEVICE = GPU_INFO['device'] if DEVICE == 'cuda': torch.cuda.amp.autocast(enabled=False) print("✅ Disabled AMP/autocast on CUDA for FP32 consistency") id_frame_count = defaultdict(int) OPENCV_CUDA_AVAILABLE = GPU_INFO.get('opencv_cuda_available', False) def configure_multithreading(): if DEVICE == 'cuda': cpu_threads = min(4, os.cpu_count() // 2) cv2.setNumThreads(cpu_threads) os.environ['OMP_NUM_THREADS'] = str(cpu_threads) os.environ['MKL_NUM_THREADS'] = str(cpu_threads) print(f" CPU Threads for GPU: {cpu_threads}") elif DEVICE == 'mps': cpu_threads = os.cpu_count() cv2.setNumThreads(cpu_threads) os.environ['OMP_NUM_THREADS'] = str(cpu_threads) os.environ['MKL_NUM_THREADS'] = str(cpu_threads) print(f" CPU Threads for M-series: {cpu_threads}") else: cpu_threads = os.cpu_count() cv2.setNumThreads(cpu_threads) os.environ['OMP_NUM_THREADS'] = str(cpu_threads) os.environ['MKL_NUM_THREADS'] = str(cpu_threads) print(f" CPU Threads: {cpu_threads}") configure_multithreading() def create_player_folders(base_dir="clear_detected_players_1"): import os players_dir = os.path.join(base_dir, "players") referees_dir = os.path.join(base_dir, "refree") os.makedirs(players_dir, exist_ok=True) os.makedirs(referees_dir, exist_ok=True) return players_dir, referees_dir def get_available_field_lines(virtual_hash_data, lower_line=None): available_lines = [] detected_count = 0 if virtual_hash_data.get('upper_line_params'): m, b = virtual_hash_data['upper_line_params'] available_lines.append(('virtual_upper', m, b, 12.0)) detected_count += 1 if virtual_hash_data.get('upper_hash_line_params'): m, b = virtual_hash_data['upper_hash_line_params'] available_lines.append(('hash_upper', m, b, 18.33)) detected_count += 1 if virtual_hash_data.get('lower_hash_line_params'): m, b = virtual_hash_data['lower_hash_line_params'] available_lines.append(('hash_lower', m, b, 32.33)) detected_count += 1 if virtual_hash_data.get('lower_line_params'): m, b = virtual_hash_data['lower_line_params'] available_lines.append(('virtual_lower', m, b, 38.66)) detected_count += 1 if lower_line is not None: m = lower_line.get('slope') b = lower_line.get('intercept') if m is not None and b is not None: available_lines.append(('sideline_lower', m, b, 53.33)) return available_lines, detected_count def find_closest_yard_line_slope(player_x, player_y, yard_line_assignments): if not yard_line_assignments: return None min_distance = float('inf') closest_slope = None for assignment in yard_line_assignments: line_data = assignment.get('line_data', {}) extended_line = line_data.get('extended_line', []) if len(extended_line) < 4: continue x1, y1, x2, y2 = extended_line line_length = np.sqrt((x2 - x1)**2 + (y2 - y1)**2) if line_length == 0: continue t = max(0, min(1, ((player_x - x1) * (x2 - x1) + (player_y - y1) * (y2 - y1)) / (line_length**2))) projection_x = x1 + t * (x2 - x1) projection_y = y1 + t * (y2 - y1) distance = np.sqrt((player_x - projection_x)**2 + (player_y - projection_y)**2) if distance < min_distance: min_distance = distance closest_slope = line_data.get('slope') return closest_slope def calculate_intersection_with_line_at_angle(player_x, player_y, target_slope, line_m, line_b): if target_slope is None or abs(target_slope) > 1e6: return line_m * player_x + line_b player_line_b = player_y - target_slope * player_x if abs(target_slope - line_m) < 1e-6: return player_y intersection_x = (line_b - player_line_b) / (target_slope - line_m) intersection_y = target_slope * intersection_x + player_line_b return intersection_y def calculate_line_y_at_player_x_perspective(available_lines, player_x, player_y, yard_line_slope): line_references = [] for line_type, m, b, field_y in available_lines: if yard_line_slope is not None: pixel_y = calculate_intersection_with_line_at_angle(player_x, player_y, yard_line_slope, m, b) else: pixel_y = m * player_x + b line_references.append({ 'type': line_type, 'pixel_y': pixel_y, 'field_y': field_y, 'slope': m, 'intercept': b }) return sorted(line_references, key=lambda x: x['field_y']) def get_adjacent_line(line_references_sorted, current_line, direction): try: current_idx = line_references_sorted.index(current_line) if direction == 'above' and current_idx > 0: return line_references_sorted[current_idx - 1] elif direction == 'below' and current_idx < len(line_references_sorted) - 1: return line_references_sorted[current_idx + 1] except ValueError: pass return None def calculate_player_y_coordinate_perspective(player_x, player_y, line_references, yard_line_slope): if len(line_references) < 2: return None line_references_sorted = sorted(line_references, key=lambda x: x['field_y']) weighted_sum = 0.0 total_weight = 0.0 max_pixel_distance = max(abs(player_y - line['pixel_y']) for line in line_references_sorted) for line in line_references_sorted: pixel_distance = abs(player_y - line['pixel_y']) if max_pixel_distance > 0: normalized_distance = pixel_distance / max_pixel_distance weight = 1.0 - normalized_distance else: weight = 1.0 weight = max(weight, 0.1) if player_y <= line['pixel_y']: above_line = get_adjacent_line(line_references_sorted, line, 'above') if above_line: pixel_range = max(line['pixel_y'] - above_line['pixel_y'], 1) field_range = max(line['field_y'] - above_line['field_y'], 1) pixel_ratio = (line['pixel_y'] - player_y) / pixel_range field_delta = pixel_ratio * field_range estimated_field_y = line['field_y'] - field_delta else: estimated_field_y = line['field_y'] - ((line['pixel_y'] - player_y) / 50) * 10 else: below_line = get_adjacent_line(line_references_sorted, line, 'below') if below_line: pixel_range = max(below_line['pixel_y'] - line['pixel_y'], 1) field_range = max(below_line['field_y'] - line['field_y'], 1) pixel_ratio = (player_y - line['pixel_y']) / pixel_range field_delta = pixel_ratio * field_range estimated_field_y = line['field_y'] + field_delta else: estimated_field_y = line['field_y'] + ((player_y - line['pixel_y']) / 50) * 10 estimated_field_y = max(0.0, min(53.33, estimated_field_y)) weighted_sum += estimated_field_y * weight total_weight += weight final_y = weighted_sum / total_weight if total_weight > 0 else None if final_y is not None: lowest_line = line_references_sorted[-1] second_lowest = line_references_sorted[-2] if len(line_references_sorted) >= 2 else None if player_y > lowest_line['pixel_y'] + 20: if second_lowest: pixel_range = lowest_line['pixel_y'] - second_lowest['pixel_y'] field_range = lowest_line['field_y'] - second_lowest['field_y'] if pixel_range > 0 and field_range > 0: pixels_per_yard = pixel_range / field_range if pixels_per_yard > 0: yards_below = (player_y - lowest_line['pixel_y']) / pixels_per_yard extrapolated_y = lowest_line['field_y'] + yards_below blend_factor = min(1.0, (player_y - lowest_line['pixel_y']) / 100.0) final_y = (1 - blend_factor) * final_y + blend_factor * extrapolated_y final_y = min(53.33, final_y) return final_y def map_player_to_field_enhanced_perspective(foot_x, foot_y, virtual_hash_data, transform_matrix, field_params, yard_line_assignments, lower_line=None): all_assignments = yard_line_assignments.copy() available_lines, detected_line_count = get_available_field_lines(virtual_hash_data, lower_line) if detected_line_count < 1 and not yard_line_assignments: return None yard_line_slope = find_closest_yard_line_slope(foot_x, foot_y, all_assignments) line_references = [] if detected_line_count >= 1: line_references = calculate_line_y_at_player_x_perspective(available_lines, foot_x, foot_y, yard_line_slope) player_y_field = None if line_references: player_y_field = calculate_player_y_coordinate_perspective(foot_x, foot_y, line_references, yard_line_slope) if player_y_field is None and transform_matrix is not None: point_video = np.array([[[foot_x, foot_y]]], dtype=np.float32) point_field = cv2.perspectiveTransform(point_video, transform_matrix) player_x_field = point_field[0][0][0] player_y_field = point_field[0][0][1] elif player_y_field is None: return None if transform_matrix is not None and (player_y_field is None or not line_references): point_video = np.array([[[foot_x, foot_y]]], dtype=np.float32) point_field = cv2.perspectiveTransform(point_video, transform_matrix) player_x_field = point_field[0][0][0] if player_y_field is None: player_y_field = point_field[0][0][1] elif transform_matrix is None: player_x_field = (foot_x / 640) * 120.0 else: point_video = np.array([[[foot_x, foot_y]]], dtype=np.float32) point_field = cv2.perspectiveTransform(point_video, transform_matrix) player_x_field = point_field[0][0][0] player_x_field = max(0.0, min(120.0, player_x_field)) player_y_field = max(0.0, min(53.33, player_y_field)) return (player_x_field, player_y_field) def is_player_on_field(foot_x, foot_y, upper_line, lower_line, tolerance=10): if upper_line is None or lower_line is None: return True try: upper_y = upper_line['slope'] * foot_x + upper_line['intercept'] lower_y = lower_line['slope'] * foot_x + lower_line['intercept'] is_between_sidelines = (upper_y - tolerance <= foot_y <= lower_y + tolerance) return is_between_sidelines except Exception as e: return True def save_player_crop(frame, bbox, player_type, player_id, frame_index, players_dir, referees_dir): try: x1, y1, x2, y2 = bbox x1 = max(0, x1) y1 = max(0, y1) x2 = min(frame.shape[1], x2) y2 = min(frame.shape[0], y2) if x2 <= x1 or y2 <= y1: return False player_crop = frame[y1:y2, x1:x2] if player_crop.size == 0: return False if player_type == 'player': save_dir = players_dir else: save_dir = referees_dir filename = f"frame_{frame_index:06d}_player_{player_id:03d}.jpg" filepath = os.path.join(save_dir, filename) cv2.imwrite(filepath, player_crop) return True except Exception as e: print(f"Error saving player crop: {e}") return False def debug_player_coordinates(frame_index, player_positions): if not player_positions: print(f"Frame {frame_index}: No players detected") return print(f"\n=== Frame {frame_index} - Player Coordinates Debug ===") for i, player in enumerate(player_positions): video_coords = player.get('video_coords') field_coords = player.get('field_coords') player_type = player.get('player_type', 'unknown') if video_coords: vx, vy = video_coords print(f" Player {i} ({player_type}):") print(f" Video coords: ({vx}, {vy}) pixels") if field_coords: fx, fy = field_coords print(f" Field coords: ({fx:.1f}, {fy:.1f}) yards") if fx < 10.0 or fx > 110.0: print(f" WARNING: In end zone! (x={fx:.1f})") else: print(f" WARNING: No field coordinates calculated") print("=" * 50) def detect_players_and_referees(frame, player_model, person_count, refree_count, upper_line, lower_line, transform_matrix=None, field_params=None, virtual_hash_data=None, yard_line_assignments=None, draw_boxes=True, frame_index=0, save_crops=False, players_dir=None, referees_dir=None, overlay_image=None, id_frame_count=None, top_player_id=None, draw_all_labels=False): on_field_players = 0 bench_players = 0 player_positions = [] perspective_aware_status = "Perspective-Aware: Not Available" player_crops_saved = 0 referee_crops_saved = 0 target_frame = overlay_image if overlay_image is not None else frame player_color = (255, 0, 0) referee_color = (0, 0, 255) top_player_color = (0, 255, 255) results = player_model.track( frame, persist=True, conf=0.3, iou=0.5, verbose=False, tracker="bytetrack.yaml" ) for result in results: boxes = result.boxes if boxes is not None: if boxes.id is not None: track_ids = boxes.id.cpu().numpy().astype(int) else: track_ids = list(range(len(boxes))) bboxes = boxes.xyxy.cpu().numpy().astype(int) for i, (bbox, track_id) in enumerate(zip(bboxes, track_ids)): x1, y1, x2, y2 = bbox conf = float(boxes.conf[i].cpu().numpy()) cls = int(boxes.cls[i].cpu().numpy()) class_name = player_model.names[cls] foot_x = (x1 + x2) // 2 foot_y = y2 is_on_field = is_player_on_field(foot_x, foot_y, upper_line, lower_line) if not is_on_field: bench_players += 1 continue if save_crops and players_dir and referees_dir: if class_name.lower() in ["person", "player"]: crop_saved = save_player_crop(frame, bbox, 'player', person_count, frame_index, players_dir, referees_dir) if crop_saved: player_crops_saved += 1 elif class_name.lower() in ["refree", "referee"]: crop_saved = save_player_crop(frame, bbox, 'refree', refree_count, frame_index, players_dir, referees_dir) if crop_saved: referee_crops_saved += 1 if class_name.lower() in ["person", "player"]: current_color = top_player_color if track_id == top_player_id else player_color else: current_color = referee_color if class_name.lower() in ["person", "player"]: player_positions.append({ 'video_coords': (foot_x, foot_y), 'field_coords': None, 'player_type': 'player', 'confidence': conf, 'bbox': (x1, y1, x2, y2), 'class_name': class_name, 'id': person_count, 'track_id': track_id, 'color': current_color }) person_count += 1 on_field_players += 1 elif class_name.lower() == "refree" or class_name.lower() == "referee": player_positions.append({ 'video_coords': (foot_x, foot_y), 'field_coords': None, 'player_type': 'refree', 'confidence': conf, 'bbox': (x1, y1, x2, y2), 'class_name': class_name, 'id': refree_count, 'track_id': track_id, 'color': current_color }) refree_count += 1 on_field_players += 1 if id_frame_count is not None and player_positions: for player in player_positions: if 'track_id' in player: id_frame_count[player['track_id']] += 1 if draw_boxes: for player in player_positions: foot_x, foot_y = player['video_coords'] bbox = player['bbox'] conf = player['confidence'] player_type = player['player_type'] player_id = player['id'] track_id = player.get('track_id', -1) class_name = player['class_name'] player_color_to_use = player.get('color', (255, 0, 0)) is_top_player = (track_id == top_player_id) inner_radius = 8 if is_top_player else 5 outer_radius = 10 if is_top_player else 7 if overlay_image is not None: cv2.circle(target_frame, (foot_x, foot_y), inner_radius, (*player_color_to_use, 200), -1) cv2.circle(target_frame, (foot_x, foot_y), outer_radius, (255, 255, 255, 200), 2) else: # BGR for direct frame cv2.circle(target_frame, (foot_x, foot_y), inner_radius, player_color_to_use, -1) cv2.circle(target_frame, (foot_x, foot_y), outer_radius, (255, 255, 255), 2) # ========== MODIFIED: Draw ID label based on draw_all_labels flag ========== # Draw ID label if: # 1. We're in draw_all_labels mode (first 5 frames) OR # 2. This is the top player with valid track_id should_draw_label = False label_text = "" if track_id != -1: if draw_all_labels: # First 5 frames: show all IDs should_draw_label = True label_text = f"ID:{track_id}" # Use normal size for all players font_scale = 0.7 thickness = 2 elif is_top_player: # After selection: only show top player ID should_draw_label = True label_text = f"ID:{track_id}" # Use normal size for top player font_scale = 0.7 thickness = 2 if should_draw_label: (text_width, text_height), baseline = cv2.getTextSize( label_text, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness ) label_x = foot_x - text_width // 2 label_y = foot_y - 15 if label_y < 30: label_y = foot_y + 25 # Draw background rect padding = 5 if overlay_image is not None: cv2.rectangle(target_frame, (label_x - padding, label_y - text_height - padding), (label_x + text_width + padding, label_y + padding), (*player_color_to_use, 200), -1) else: cv2.rectangle(target_frame, (label_x - padding, label_y - text_height - padding), (label_x + text_width + padding, label_y + padding), player_color_to_use, -1) # Draw text text_color = (255, 255, 255, 255) if overlay_image is not None else (255, 255, 255) cv2.putText(target_frame, label_text, (label_x, label_y), cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_color, thickness) # Calculate field coordinates for display using PERSPECTIVE-AWARE method field_coords = None if transform_matrix is not None and field_params is not None: field_coords = map_player_to_field_enhanced_perspective( foot_x, foot_y, virtual_hash_data, transform_matrix, field_params, yard_line_assignments ) # NEW: Round field coordinates to 2 decimal places if field_coords is not None: field_coords = (round(field_coords[0], 2), round(field_coords[1], 2)) # Adjust coordinates for template field area if field_coords is not None: x_field, y_field = field_coords # Clip to playing field bounds x_field_adjusted = max(0.0, min(120.0, x_field)) y_field_adjusted = max(0.0, min(53.33, y_field)) # Only show players in the playing field area (10-110 yards) field_coords = (x_field_adjusted, y_field_adjusted) # Update field coordinates in player positions player['field_coords'] = field_coords # Add crop saving info to the frame if crops were saved if save_crops and (player_crops_saved > 0 or referee_crops_saved > 0): crop_info = f"Crops: P{player_crops_saved} R{referee_crops_saved}" cv2.putText(target_frame, crop_info, (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255, 255) if overlay_image is not None else (255, 255, 255), 2) return frame, person_count, refree_count, on_field_players, bench_players, \ player_positions, player_crops_saved, referee_crops_saved def calculate_perspective_transform_with_intermediate(yard_line_assignments, upper_line, lower_line, frame_width, frame_height): if not yard_line_assignments or upper_line is None or lower_line is None: return None, None FIELD_LENGTH = 120.0 FIELD_WIDTH = 53.33 src_points = [] dst_points = [] for assignment in yard_line_assignments: yard_num = assignment.get('yard_line_num') side = assignment.get('side', 'unknown') line_data = assignment.get('line_data', {}) extended_line = line_data.get('extended_line', []) if len(extended_line) < 4 or side == 'unknown' or yard_num is None: continue x1_video, y1_video = extended_line[0], extended_line[1] x2_video, y2_video = extended_line[2], extended_line[3] upper_intersection = calculate_line_intersection((x1_video, y1_video, x2_video, y2_video), upper_line) lower_intersection = calculate_line_intersection((x1_video, y1_video, x2_video, y2_video), lower_line) if upper_intersection is None or lower_intersection is None: continue # Calculate x_template based on side and yard number if side == 'L': x_template = 10 + yard_num elif side == 'R': x_template = 110 - yard_num elif side == 'C': x_template = 60 else: continue y_template_top = 0.0 y_template_bottom = FIELD_WIDTH src_points.append(upper_intersection) dst_points.append((x_template, y_template_top)) src_points.append(lower_intersection) dst_points.append((x_template, y_template_bottom)) # We need at least 4 points for homography, but can use as few as 2 lines if len(src_points) < 4: # Try with fewer points (minimum 4 points = 2 lines) return None, None src_points = np.array(src_points, dtype=np.float32) dst_points = np.array(dst_points, dtype=np.float32) try: transform_matrix, mask = cv2.findHomography(src_points, dst_points, method=cv2.RANSAC, ransacReprojThreshold=10.0) # Increased threshold for robustness except Exception as e: print(f"Homography calculation failed: {e}") return None, None if transform_matrix is None: return None, None field_params = { 'field_length': FIELD_LENGTH, 'field_width': FIELD_WIDTH, 'src_points': src_points, 'dst_points': dst_points, 'inliers': mask, 'inlier_count': int(np.sum(mask)) if mask is not None else 0, 'total_points': len(src_points), 'includes_intermediate': any(a.get('is_intermediate', False) for a in yard_line_assignments) } return transform_matrix, field_params def calculate_line_intersection(line1, line2_dict): """Intersection between a line segment and a sideline (y = m x + b).""" if line2_dict is None: return None x1, y1, x2, y2 = line1 m2 = line2_dict.get('slope') b2 = line2_dict.get('intercept') if m2 is None or b2 is None: return None # Vertical case if abs(x2 - x1) < 1e-6: x_intersect = x1 y_intersect = m2 * x_intersect + b2 return (float(x_intersect), float(y_intersect)) m1 = (y2 - y1) / (x2 - x1) b1 = y1 - m1 * x1 if abs(m1 - m2) < 1e-6: return None x_intersect = (b2 - b1) / (m1 - m2) y_intersect = m1 * x_intersect + b1 return (float(x_intersect), float(y_intersect)) def draw_players_on_field_template(field_image, player_positions, dpi=100): # This function is kept for compatibility but is now handled by FieldTemplateManager return field_image def bgr_to_rgb_float(bgr_color): if isinstance(bgr_color, tuple) and len(bgr_color) == 3: return (bgr_color[2] / 255.0, bgr_color[1] / 255.0, bgr_color[0] / 255.0) return bgr_color def create_football_field_image(yard_line_assignments=[], frame_width=640, frame_height=480): # Create a blank white background field_template = np.full((frame_height, frame_width, 3), (255, 255, 255), dtype=np.uint8) # Field dimensions in yards field_length_yards = 120.0 field_width_yards = 53.3 # Calculate scaling factors (template has 5-yard padding on all sides) # Total template: 130 yards wide (-5 to 125), 63.3 yards tall (-5 to 58.3) scale_x = frame_width / 130.0 # pixels per yard (total width in yards) scale_y = frame_height / 63.3 # pixels per yard (total height in yards) # Helper function to convert yard coordinates to pixel coordinates def yard_to_pixel(x_yard, y_yard): # Add 5 to shift from field coordinates (-5 to 125) to template coordinates (0 to 130) x_pixel = int((x_yard + 5) * scale_x) y_pixel = int((y_yard + 5) * scale_y) return x_pixel, y_pixel # Draw green field field_top_left = yard_to_pixel(0, 0) field_bottom_right = yard_to_pixel(field_length_yards, field_width_yards) cv2.rectangle(field_template, field_top_left, field_bottom_right, (34, 139, 34), -1) # Add white border around the field cv2.rectangle(field_template, field_top_left, field_bottom_right, (255, 255, 255), 1) # Draw end zones # Left endzone (blue) left_endzone_top_left = yard_to_pixel(0, 0) left_endzone_bottom_right = yard_to_pixel(10, field_width_yards) cv2.rectangle(field_template, left_endzone_top_left, left_endzone_bottom_right, (255, 0, 0), -1) # Blue in BGR cv2.rectangle(field_template, left_endzone_top_left, left_endzone_bottom_right, (255, 255, 255), 1) # White border # Right endzone (red) right_endzone_top_left = yard_to_pixel(110, 0) right_endzone_bottom_right = yard_to_pixel(120, field_width_yards) cv2.rectangle(field_template, right_endzone_top_left, right_endzone_bottom_right, (0, 0, 255), -1) # Red in BGR cv2.rectangle(field_template, right_endzone_top_left, right_endzone_bottom_right, (255, 255, 255), 1) # White border # Define yard line positions yard_lines = [ (10, 0, 'left', 'L'), # Goal line (0-yard line) (20, 10, 'left', 'L'), # 10-yard line (30, 20, 'left', 'L'), # 20-yard line (40, 30, 'left', 'L'), # 30-yard line (50, 40, 'left', 'L'), # 40-yard line (60, 50, 'center', 'C'), # 50-yard line (center) (70, 40, 'right', 'R'), # 40-yard line (80, 30, 'right', 'R'), # 30-yard line (90, 20, 'right', 'R'), # 20-yard line (100, 10, 'right', 'R'), # 10-yard line (110, 0, 'right', 'R') # Goal line (0-yard line) ] # Intermediate yard lines (5-yard increments) intermediate_yard_lines = [ (15, 5, 'left', 'L'), (25, 15, 'left', 'L'), (35, 25, 'left', 'L'), (45, 35, 'left', 'L'), (55, 45, 'left', 'L'), (65, 45, 'right', 'R'), (75, 35, 'right', 'R'), (85, 25, 'right', 'R'), (95, 15, 'right', 'R'), (105, 5, 'right', 'R') ] # Create color mapping for yard line highlighting yard_line_color_map = {} for assignment in yard_line_assignments: yard_line_num = assignment.get('yard_line_num') color = assignment.get('color', (0, 0, 139)) # Default dark red side = assignment.get('side', 'unknown') yard_line_color_map[(yard_line_num, side)] = color # Draw main yard lines for x_pos, yard, yard_side, side_code in yard_lines: # Get color for this line line_color = (255, 255, 255) # Default white line_width = 1 if (yard, side_code) in yard_line_color_map: line_color = yard_line_color_map[(yard, side_code)] line_width = 2 elif any(a.get('yard_line_num') == yard for a in yard_line_assignments): line_color = (0, 255, 255) # Yellow for highlighted but not assigned line_width = 1 # Convert yard position to pixel position start_x, start_y = yard_to_pixel(x_pos, 0) end_x, end_y = yard_to_pixel(x_pos, field_width_yards) # Draw the yard line cv2.line(field_template, (start_x, start_y), (end_x, end_y), line_color, line_width, cv2.LINE_AA) # Add yard numbers (skip 0-yard lines) if yard != 0: # Format label with space between digits yard_str = str(yard) if len(yard_str) == 2: label = f"{yard_str[0]} {yard_str[1]}" else: label = yard_str # Top label text_x, text_y = yard_to_pixel(x_pos, 12) text_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)[0] text_x -= text_size[0] // 2 cv2.putText(field_template, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2) # Bottom label (not rotated in OpenCV) text_x, text_y = yard_to_pixel(x_pos, field_width_yards - 12) text_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)[0] text_x -= text_size[0] // 2 cv2.putText(field_template, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2) # Draw intermediate yard lines (dashed) for x_pos, yard, yard_side, side_code in intermediate_yard_lines: line_color = (200, 200, 200) # Light gray line_width = 1 if (yard, side_code) in yard_line_color_map: line_color = yard_line_color_map[(yard, side_code)] line_width = 1 # Convert yard position to pixel position start_x, start_y = yard_to_pixel(x_pos, 0) end_x, end_y = yard_to_pixel(x_pos, field_width_yards) # Draw dashed line dash_length = 8 gap_length = 4 current_y = start_y while current_y < end_y: segment_end = min(current_y + dash_length, end_y) if segment_end > current_y: cv2.line(field_template, (start_x, current_y), (start_x, segment_end), line_color, line_width, cv2.LINE_AA) current_y = segment_end + gap_length # Draw hash marks (light gray thin lines at each yard from 10 to 110) for yard in range(10, 111): x_pixel = yard_to_pixel(yard, 0)[0] # Top hash marks (18.5 to 20 yards) top_start = yard_to_pixel(yard, 18.5) top_end = yard_to_pixel(yard, 20) cv2.line(field_template, top_start, top_end, (200, 200, 200), 1, cv2.LINE_AA) # Bottom hash marks (33.3 to 34.8 yards) bottom_start = yard_to_pixel(yard, 33.3) bottom_end = yard_to_pixel(yard, 34.8) cv2.line(field_template, bottom_start, bottom_end, (200, 200, 200), 1, cv2.LINE_AA) return field_template # ============================================================================= # ENHANCED LINE FUNCTIONS FROM CODE 1 # ============================================================================= def enforce_parallel_lines(virtual_hash_data, slope_tolerance=0.05, debug=False): if debug: print("\n=== PARALLEL LINE ENFORCEMENT ===") # Extract line parameters vl_top = virtual_hash_data.get('upper_line_params') vl_bottom = virtual_hash_data.get('lower_line_params') hl_top = virtual_hash_data.get('upper_hash_line_params') hl_bottom = virtual_hash_data.get('lower_hash_line_params') adjustments_made = [] # ========== VIRTUAL LINES GROUP ========== virtual_lines = [] if vl_top: virtual_lines.append(('VL_top', vl_top)) if vl_bottom: virtual_lines.append(('VL_bottom', vl_bottom)) if len(virtual_lines) >= 2: virtual_slopes = [params[0] for name, params in virtual_lines] avg_virtual_slope = np.mean(virtual_slopes) for name, params in virtual_lines: current_slope = params[0] deviation = abs(current_slope - avg_virtual_slope) if deviation > slope_tolerance: new_params = (avg_virtual_slope, params[1]) if name == 'VL_top': virtual_hash_data['upper_line_params'] = new_params else: virtual_hash_data['lower_line_params'] = new_params adjustments_made.append(f"{name}: {current_slope:.4f} -> {avg_virtual_slope:.4f}") # ========== HASH LINES GROUP ========== hash_lines = [] if hl_top: hash_lines.append(('HL_top', hl_top)) if hl_bottom: hash_lines.append(('HL_bottom', hl_bottom)) if len(hash_lines) >= 2: hash_slopes = [params[0] for name, params in hash_lines] avg_hash_slope = np.mean(hash_slopes) for name, params in hash_lines: current_slope = params[0] deviation = abs(current_slope - avg_hash_slope) if deviation > slope_tolerance: new_params = (avg_hash_slope, params[1]) if name == 'HL_top': virtual_hash_data['upper_hash_line_params'] = new_params else: virtual_hash_data['lower_hash_line_params'] = new_params adjustments_made.append(f"{name}: {current_slope:.4f} -> {avg_hash_slope:.4f}") return virtual_hash_data, adjustments_made def validate_line_distances(virtual_hash_data, min_distance_pixels=20, debug=False): if debug: print("\n=== LINE DISTANCE VALIDATION ===") vl_top = virtual_hash_data.get('upper_line_params') vl_bottom = virtual_hash_data.get('lower_line_params') hl_top = virtual_hash_data.get('upper_hash_line_params') hl_bottom = virtual_hash_data.get('lower_hash_line_params') adjustments_made = [] image_center_x = 320 # ========== VALIDATE VIRTUAL LINES DISTANCE ========== if vl_top and vl_bottom: m1, b1 = vl_top m2, b2 = vl_bottom y1 = m1 * image_center_x + b1 y2 = m2 * image_center_x + b2 distance = abs(y2 - y1) if distance < min_distance_pixels: target_y2 = y1 + min_distance_pixels if y2 > y1 else y1 - min_distance_pixels new_b2 = target_y2 - m2 * image_center_x virtual_hash_data['lower_line_params'] = (m2, new_b2) adjustments_made.append(f"Virtual lines: increased distance from {distance:.1f} to {min_distance_pixels}") # ========== VALIDATE HASH LINES DISTANCE ========== if hl_top and hl_bottom: m1, b1 = hl_top m2, b2 = hl_bottom y1 = m1 * image_center_x + b1 y2 = m2 * image_center_x + b2 distance = abs(y2 - y1) if distance < min_distance_pixels: target_y2 = y1 + min_distance_pixels if y2 > y1 else y1 - min_distance_pixels new_b2 = target_y2 - m2 * image_center_x virtual_hash_data['lower_hash_line_params'] = (m2, new_b2) adjustments_made.append(f"Hash lines: increased distance from {distance:.1f} to {min_distance_pixels}") return virtual_hash_data, adjustments_made def enhanced_line_fitting_with_validation(virtual_hash_data, frame_width=640, frame_height=480, debug=False): validation_summary = [] if debug: print("\n" + "="*60) print("ENHANCED LINE FITTING WITH VALIDATION") print("="*60) # Step 1: Perform parallel line enforcement virtual_hash_data, parallel_adjustments = enforce_parallel_lines( virtual_hash_data, slope_tolerance=0.05, debug=debug ) if parallel_adjustments: validation_summary.extend([f"Parallel: {adj}" for adj in parallel_adjustments]) # Step 2: Perform distance validation virtual_hash_data, distance_adjustments = validate_line_distances( virtual_hash_data, min_distance_pixels=25, debug=debug ) if distance_adjustments: validation_summary.extend([f"Distance: {adj}" for adj in distance_adjustments]) # Add validation summary to data for reporting virtual_hash_data['validation_summary'] = validation_summary virtual_hash_data['validation_adjustments'] = len(validation_summary) return virtual_hash_data, validation_summary def fit_line_with_robust_method(points, frame_width, frame_height, line_type=None, existing_lines=None, debug=False): if len(points) < 2: return None, None x_vals = np.array([p[0] for p in points]) y_vals = np.array([p[1] for p in points]) # Method 1: Try RANSAC for outlier rejection if len(points) >= 3: try: ransac = RANSACRegressor( LinearRegression(), residual_threshold=10.0, max_trials=100, random_state=42 ) X = x_vals.reshape(-1, 1) ransac.fit(X, y_vals) m_ransac = ransac.estimator_.coef_[0] b_ransac = ransac.estimator_.intercept_ inlier_mask = ransac.inlier_mask_ inlier_count = np.sum(inlier_mask) if inlier_count >= max(2, len(points) * 0.5): return m_ransac, b_ransac except Exception as e: if debug: print(f" RANSAC failed: {e}") # Method 3: Standard polyfit (fallback) coeffs = np.polyfit(x_vals, y_vals, 1) return coeffs[0], coeffs[1] try: from filterpy.kalman import KalmanFilter from filterpy.common import Q_discrete_white_noise FILTERPY_AVAILABLE = True print("✅ FilterPy Kalman filter available") except ImportError: FILTERPY_AVAILABLE = False print("⚠️ FilterPy not available - falling back to EMA smoothing") class PlayerStatsTracker: def __init__(self, history_window=5, fps=60.0, stats_interval_frames=6): self.history_window = history_window self.fps = fps self.stats_interval_frames = stats_interval_frames self.time_delta = 1.0 / fps # EMA smoothing parameters for positions self.ema_alpha = 0.05 self.ema_x = None self.ema_y = None # Kalman filter for velocity/acceleration self.kalman_initialized = False self.kf = None # History deques: (frame_index, x_smoothed, y_smoothed, x_raw, y_raw, timestamp, vx, vy, ax, ay) self.position_history = deque(maxlen=history_window) # Current smoothed values self.smoothed_x = 0.0 self.smoothed_y = 0.0 self.smoothed_vx = 0.0 self.smoothed_vy = 0.0 self.smoothed_ax = 0.0 self.smoothed_ay = 0.0 # Current raw values (before EMA) self.raw_x = 0.0 self.raw_y = 0.0 # Current stats self.current_speed_yds = 0.0 self.current_accel_yds = 0.0 # Tracking state self.last_position = None self.last_velocity = None self.last_frame_index = -1 self.last_export_frame = -1 # Statistics self.stats = { 'frames_tracked': 0, 'missing_detections': 0, 'kalman_updates': 0, 'ema_updates': 0, 'interval_updates': 0, 'velocity_spikes_mitigated': 0, 'acceleration_spikes_mitigated': 0, 'stats_interval_frames': stats_interval_frames } def _initialize_kalman(self, x, y): """Initialize Kalman filter with initial position.""" if not FILTERPY_AVAILABLE: return self.kf = KalmanFilter(dim_x=6, dim_z=2) dt = self.time_delta self.kf.F = np.array([ [1, dt, 0.5*dt*dt, 0, 0, 0], [0, 1, dt, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, dt, 0.5*dt*dt], [0, 0, 0, 0, 1, dt], [0, 0, 0, 0, 0, 1] ]) self.kf.H = np.array([ [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0] ]) # MODIFIED: Axis-specific measurement noise (higher for Y to handle noise/spikes) position_noise_x = 10.0 # Original for X (keep responsive) position_noise_y = 25.0 # Higher for Y (smooth more due to potential noise) self.kf.R = np.array([ [position_noise_x, 0], [0, position_noise_y] ]) # MODIFIED: Axis-specific process noise (slightly higher for Y to allow variability) q_var_x = 0.01 # Original for X q_var_y = 0.005 # Slightly higher for Y (prevents over-smoothing during real fast Y-moves) q_x = Q_discrete_white_noise(dim=3, dt=dt, var=q_var_x) q_y = Q_discrete_white_noise(dim=3, dt=dt, var=q_var_y) self.kf.Q = np.block([ [q_x, np.zeros((3,3))], [np.zeros((3,3)), q_y] ]) self.kf.x = np.array([x, 0.0, 0.0, y, 0.0, 0.0]) # MODIFIED: Higher initial uncertainty (P) for Y components self.kf.P = np.diag([10.0, 5.0, 2.0, 20.0, 10.0, 4.0]) # Doubled for Y pos/vel/accel self.kalman_initialized = True def _apply_ema_smoothing(self, x, y): """Apply EMA smoothing to raw positions and store raw values.""" # Store raw values self.raw_x = x self.raw_y = y if self.ema_x is None or self.ema_y is None: self.ema_x = x self.ema_y = y else: self.ema_x = self.ema_alpha * x + (1 - self.ema_alpha) * self.ema_x self.ema_y = self.ema_alpha * y + (1 - self.ema_alpha) * self.ema_y self.stats['ema_updates'] += 1 return self.ema_x, self.ema_y def _update_kalman(self, x, y): """Update Kalman filter with new smoothed position.""" if not FILTERPY_AVAILABLE or not self.kalman_initialized: return x, y, 0.0, 0.0, 0.0, 0.0 try: self.kf.predict() z = np.array([x, y]) self.kf.update(z) kf_x = float(self.kf.x[0]) kf_vx = float(self.kf.x[1]) kf_ax = float(self.kf.x[2]) kf_y = float(self.kf.x[3]) kf_vy = float(self.kf.x[4]) kf_ay = float(self.kf.x[5]) self.stats['kalman_updates'] += 1 return kf_x, kf_y, kf_vx, kf_vy, kf_ax, kf_ay except Exception as e: return x, y, 0.0, 0.0, 0.0, 0.0 def _validate_velocity_acceleration(self, vx, vy, ax, ay): """Validate velocity and acceleration with softened caps.""" MAX_VELOCITY = 8.0 MAX_ACCELERATION = 4.0 vel_mag = np.sqrt(vx**2 + vy**2) acc_mag = np.sqrt(ax**2 + ay**2) if vel_mag > MAX_VELOCITY * 0.8: if vel_mag > MAX_VELOCITY: excess = vel_mag - MAX_VELOCITY target_mag = MAX_VELOCITY + excess * 0.5 scale = min(target_mag, MAX_VELOCITY * 1.2) / vel_mag self.stats['velocity_spikes_mitigated'] += 1 else: ratio = (vel_mag - MAX_VELOCITY * 0.8) / (MAX_VELOCITY * 0.2) blend = 1.0 - (ratio * 0.2) scale = (MAX_VELOCITY * 0.8 + (vel_mag - MAX_VELOCITY * 0.8) * blend) / vel_mag vx *= scale vy *= scale vel_mag = np.sqrt(vx**2 + vy**2) if acc_mag > MAX_ACCELERATION * 0.8: if acc_mag > MAX_ACCELERATION: excess = acc_mag - MAX_ACCELERATION target_mag = MAX_ACCELERATION + excess * 0.5 scale = min(target_mag, MAX_ACCELERATION * 1.2) / acc_mag self.stats['acceleration_spikes_mitigated'] += 1 else: ratio = (acc_mag - MAX_ACCELERATION * 0.8) / (MAX_ACCELERATION * 0.2) blend = 1.0 - (ratio * 0.2) scale = (MAX_ACCELERATION * 0.8 + (acc_mag - MAX_ACCELERATION * 0.8) * blend) / acc_mag ax *= scale ay *= scale return vx, vy, ax, ay def update_position(self, frame_index, field_coords): self.stats['frames_tracked'] += 1 if field_coords is None: self.stats['missing_detections'] += 1 if FILTERPY_AVAILABLE and self.kalman_initialized: try: self.kf.predict() self.smoothed_x = float(self.kf.x[0]) self.smoothed_y = float(self.kf.x[3]) self.smoothed_vx = float(self.kf.x[1]) self.smoothed_vy = float(self.kf.x[4]) self.smoothed_ax = float(self.kf.x[2]) self.smoothed_ay = float(self.kf.x[5]) except: pass return # Round input coordinates (RAW POSITIONS) x_raw = round(field_coords[0], 2) y_raw = round(field_coords[1], 2) # Step 1: Apply EMA smoothing to raw position x_ema, y_ema = self._apply_ema_smoothing(x_raw, y_raw) # Step 2: Initialize Kalman filter if needed if not self.kalman_initialized and FILTERPY_AVAILABLE: self._initialize_kalman(x_ema, y_ema) # Step 3: Update Kalman filter with EMA-smoothed position if FILTERPY_AVAILABLE and self.kalman_initialized: kf_x, kf_y, kf_vx, kf_vy, kf_ax, kf_ay = self._update_kalman(x_ema, y_ema) kf_vx, kf_vy, kf_ax, kf_ay = self._validate_velocity_acceleration( kf_vx, kf_vy, kf_ax, kf_ay ) self.smoothed_x = kf_x self.smoothed_y = kf_y self.smoothed_vx = kf_vx self.smoothed_vy = kf_vy self.smoothed_ax = kf_ax self.smoothed_ay = kf_ay else: self.smoothed_x = x_ema self.smoothed_y = y_ema if self.last_position is not None and self.last_frame_index != -1: dt = (frame_index - self.last_frame_index) * self.time_delta if dt > 0: self.smoothed_vx = (x_ema - self.last_position[0]) / dt self.smoothed_vy = (y_ema - self.last_position[1]) / dt self.current_speed_yds = np.sqrt(self.smoothed_vx**2 + self.smoothed_vy**2) self.current_accel_yds = np.sqrt(self.smoothed_ax**2 + self.smoothed_ay**2) current_timestamp = frame_index * self.time_delta self.position_history.append(( frame_index, self.smoothed_x, # Smoothed x self.smoothed_y, # Smoothed y x_raw, # Raw x (before EMA) y_raw, # Raw y (before EMA) current_timestamp, self.smoothed_vx, # Kalman velocity x self.smoothed_vy, # Kalman velocity y self.smoothed_ax, # Kalman acceleration x self.smoothed_ay # Kalman acceleration y )) self.last_position = (self.smoothed_x, self.smoothed_y) self.last_velocity = (self.smoothed_vx, self.smoothed_vy) self.last_frame_index = frame_index if frame_index % self.stats_interval_frames == 0: self.stats['interval_updates'] += 1 self.last_export_frame = frame_index def get_speed_all_units(self): """Get current speed in all units (updated every frame, smoothed by Kalman).""" speed_yds = self.current_speed_yds if len(self.position_history) >= 3: recent_speeds = [] for entry in list(self.position_history)[-3:]: vx, vy = entry[6], entry[7] recent_speeds.append(np.sqrt(vx**2 + vy**2)) speed_yds = np.mean(recent_speeds) mph = (speed_yds * 3600) / 1760 m_per_s = speed_yds * 0.9144 km_per_h = m_per_s * 3.6 return { 'yards_per_sec': round(speed_yds, 1), 'mph': round(mph, 1), 'meters_per_sec': round(m_per_s, 1), 'km_per_hour': round(km_per_h, 1) } def get_acceleration_all_units(self): """Get current acceleration in all units.""" accel_yds = self.current_accel_yds if len(self.position_history) >= 3: recent_accels = [] for entry in list(self.position_history)[-3:]: ax, ay = entry[8], entry[9] recent_accels.append(np.sqrt(ax**2 + ay**2)) accel_yds = np.mean(recent_accels) m_per_s_sq = accel_yds * 0.9144 ft_per_s_sq = accel_yds * 3 return { 'yards_per_sec_sq': round(accel_yds, 1), 'meters_per_sec_sq': round(m_per_s_sq, 1), 'feet_per_sec_sq': round(ft_per_s_sq, 1) } def get_current_state(self): """Get complete current state of the tracked player.""" return { 'position': { 'x_yards': round(self.smoothed_x, 2), 'y_yards': round(self.smoothed_y, 2), 'x_meters': round(self.smoothed_x * 0.9144, 2), 'y_meters': round(self.smoothed_y * 0.9144, 2) }, 'raw_position': { 'x_raw': self.raw_x, 'y_raw': self.raw_y }, 'velocity': { 'vx_yards': round(self.smoothed_vx, 2), 'vy_yards': round(self.smoothed_vy, 2), 'speed': self.get_speed_all_units() }, 'acceleration': { 'ax_yards': round(self.smoothed_ax, 2), 'ay_yards': round(self.smoothed_ay, 2), 'magnitude': self.get_acceleration_all_units() }, 'timestamp': round(self.last_frame_index * self.time_delta, 2) if self.last_frame_index != -1 else 0.0, 'kalman_velocity': (self.smoothed_vx, self.smoothed_vy) } def get_raw_position(self): """Get raw position (before EMA smoothing).""" return (self.raw_x, self.raw_y) def get_kalman_velocity(self): """Get Kalman filter velocity components.""" return (self.smoothed_vx, self.smoothed_vy) def reset(self): """Reset tracker history and filters.""" self.position_history.clear() self.ema_x = None self.ema_y = None self.kalman_initialized = False self.kf = None self.last_position = None self.last_velocity = None self.last_frame_index = -1 self.last_export_frame = -1 self.smoothed_x = 0.0 self.smoothed_y = 0.0 self.smoothed_vx = 0.0 self.smoothed_vy = 0.0 self.smoothed_ax = 0.0 self.smoothed_ay = 0.0 self.raw_x = 0.0 self.raw_y = 0.0 self.current_speed_yds = 0.0 self.current_accel_yds = 0.0 self.stats = { 'frames_tracked': 0, 'missing_detections': 0, 'kalman_updates': 0, 'ema_updates': 0, 'interval_updates': 0, 'velocity_spikes_mitigated': 0, 'acceleration_spikes_mitigated': 0 } def get_stats(self): """Get tracking statistics.""" stats_copy = self.stats.copy() if self.stats['frames_tracked'] > 0: stats_copy['missing_rate'] = self.stats['missing_detections'] / self.stats['frames_tracked'] stats_copy['kalman_usage_rate'] = self.stats['kalman_updates'] / max(1, self.stats['frames_tracked'] - self.stats['missing_detections']) return stats_copy class SidelinePositionValidator3D: def __init__(self, upper_buffer_percentage=0.075, lower_buffer_percentage=0.15): self.upper_buffer_percentage = upper_buffer_percentage self.lower_buffer_percentage = lower_buffer_percentage # Store previous frame data self.prev_upper_sideline = None self.prev_lower_sideline = None self.prev_upper_buffer_line = None self.prev_lower_buffer_line = None # Statistics self.stats = { 'frames_validated': 0, 'upper_adjustments': 0, 'lower_adjustments': 0, 'upper_rejected': 0, 'lower_rejected': 0 } # Visualization settings self.show_no_go_zone = True self.buffer_zone_alpha = 0.15 # Transparency for buffer zone def calculate_no_go_zone(self, virtual_hash_data, frame_width, debug=False): if not virtual_hash_data: if debug: print(" No virtual hash data for no-go zone") return None # Get virtual line parameters upper_virtual = virtual_hash_data.get('upper_line_params') lower_virtual = virtual_hash_data.get('lower_line_params') if not upper_virtual or not lower_virtual: if debug: print(f" Missing virtual lines: upper={upper_virtual is not None}, lower={lower_virtual is not None}") return None m_upper, b_upper = upper_virtual m_lower, b_lower = lower_virtual # Calculate distance between virtual lines at center center_x = frame_width / 2 y_upper_virtual = m_upper * center_x + b_upper y_lower_virtual = m_lower * center_x + b_lower # Ensure upper is above lower (smaller y value) if y_upper_virtual > y_lower_virtual: # Swap if needed y_upper_virtual, y_lower_virtual = y_lower_virtual, y_upper_virtual m_upper, m_lower = m_lower, m_upper b_upper, b_lower = b_lower, b_upper virtual_distance = abs(y_lower_virtual - y_upper_virtual) # Calculate separate buffer distances upper_buffer_distance = virtual_distance * self.upper_buffer_percentage lower_buffer_distance = virtual_distance * self.lower_buffer_percentage # Create buffer lines (same slope as virtual lines) # Upper buffer line: above upper virtual line (subtract for smaller y) b_upper_buffer = b_upper - upper_buffer_distance # Lower buffer line: below lower virtual line (add for larger y) b_lower_buffer = b_lower + lower_buffer_distance result = { 'upper_virtual_line': (m_upper, b_upper), 'lower_virtual_line': (m_lower, b_lower), 'upper_buffer_line': (m_upper, b_upper_buffer), 'lower_buffer_line': (m_lower, b_lower_buffer), 'virtual_distance': virtual_distance, 'upper_buffer_distance': upper_buffer_distance, 'lower_buffer_distance': lower_buffer_distance, 'upper_buffer_percentage': self.upper_buffer_percentage, 'lower_buffer_percentage': self.lower_buffer_percentage, 'center_x': center_x, 'y_upper_virtual': y_upper_virtual, 'y_lower_virtual': y_lower_virtual, 'y_upper_buffer': m_upper * center_x + b_upper_buffer, 'y_lower_buffer': m_lower * center_x + b_lower_buffer } # Store for next frame self.prev_upper_buffer_line = (m_upper, b_upper_buffer) self.prev_lower_buffer_line = (m_lower, b_lower_buffer) if debug: print(f" No-Go Zone (3D): Virtual distance = {virtual_distance:.1f}px") print(f" Upper buffer: {upper_buffer_distance:.1f}px ({self.upper_buffer_percentage*100:.0f}%)") print(f" Lower buffer: {lower_buffer_distance:.1f}px ({self.lower_buffer_percentage*100:.0f}%)") print(f" Upper virtual at y={y_upper_virtual:.1f}, buffer at y={result['y_upper_buffer']:.1f}") print(f" Lower virtual at y={y_lower_virtual:.1f}, buffer at y={result['y_lower_buffer']:.1f}") return result def is_sideline_in_no_go_zone(self, sideline, no_go_zone, is_upper=True, debug=False): if not sideline or not no_go_zone: return False, 0.0 slope = sideline.get('slope') intercept = sideline.get('intercept') if slope is None or intercept is None: return False, 0.0 center_x = no_go_zone['center_x'] sideline_y = slope * center_x + intercept if is_upper: # Upper sideline should be ABOVE upper buffer line (smaller y value) buffer_y = no_go_zone['y_upper_buffer'] buffer_distance = no_go_zone['upper_buffer_distance'] if sideline_y > buffer_y: # Below buffer line = in no-go zone distance_into_buffer = (sideline_y - buffer_y) / buffer_distance return True, min(1.0, distance_into_buffer) else: distance_from_buffer = (buffer_y - sideline_y) / buffer_distance return False, max(0.0, distance_from_buffer) else: # Lower sideline should be BELOW lower buffer line (larger y value) buffer_y = no_go_zone['y_lower_buffer'] buffer_distance = no_go_zone['lower_buffer_distance'] if sideline_y < buffer_y: # Above buffer line = in no-go zone distance_into_buffer = (buffer_y - sideline_y) / buffer_distance return True, min(1.0, distance_into_buffer) else: distance_from_buffer = (sideline_y - buffer_y) / buffer_distance return False, max(0.0, distance_from_buffer) def validate_and_adjust_sidelines(self, upper_line, lower_line, virtual_hash_data, frame_width, frame_height, debug=False): self.stats['frames_validated'] += 1 adjustments_made = [] # Calculate no-go zone no_go_zone = self.calculate_no_go_zone(virtual_hash_data, frame_width, debug) if not no_go_zone: # Can't validate without virtual lines if debug: print(" No no-go zone calculated, skipping validation") return upper_line, lower_line, adjustments_made # Validate upper sideline validated_upper = upper_line.copy() if upper_line else None if upper_line: in_no_go, ratio = self.is_sideline_in_no_go_zone(upper_line, no_go_zone, is_upper=True, debug=debug) if debug: print(f" Upper sideline validation: in_no_go={in_no_go}, ratio={ratio:.2f}") # Check if sideline is in no-go zone if in_no_go: self.stats['upper_rejected'] += 1 # Try to use previous frame if available and better if self.prev_upper_sideline: prev_in_no_go, prev_ratio = self.is_sideline_in_no_go_zone( self.prev_upper_sideline, no_go_zone, is_upper=True, debug=debug ) # If previous frame is outside no-go zone or better positioned if not prev_in_no_go: validated_upper = self.prev_upper_sideline.copy() adjustments_made.append("Upper: Used previous frame (current in no-go)") self.stats['upper_adjustments'] += 1 if debug: print(f" Using previous upper sideline (was outside no-go)") else: # Both current and previous are in no-go zone, adjust to buffer line validated_upper['slope'] = no_go_zone['upper_buffer_line'][0] validated_upper['intercept'] = no_go_zone['upper_buffer_line'][1] adjustments_made.append("Upper: Adjusted to buffer line") self.stats['upper_adjustments'] += 1 if debug: print(f" Adjusting upper to buffer line") else: # No previous frame, adjust to buffer line validated_upper['slope'] = no_go_zone['upper_buffer_line'][0] validated_upper['intercept'] = no_go_zone['upper_buffer_line'][1] adjustments_made.append("Upper: Adjusted to buffer line (no previous)") self.stats['upper_adjustments'] += 1 # Validate lower sideline validated_lower = lower_line.copy() if lower_line else None if lower_line: in_no_go, ratio = self.is_sideline_in_no_go_zone(lower_line, no_go_zone, is_upper=False, debug=debug) if debug: print(f" Lower sideline validation: in_no_go={in_no_go}, ratio={ratio:.2f}") # Check if sideline is in no-go zone if in_no_go: self.stats['lower_rejected'] += 1 # Try to use previous frame if available and better if self.prev_lower_sideline: prev_in_no_go, prev_ratio = self.is_sideline_in_no_go_zone( self.prev_lower_sideline, no_go_zone, is_upper=False, debug=debug ) # If previous frame is outside no-go zone or better positioned if not prev_in_no_go: validated_lower = self.prev_lower_sideline.copy() adjustments_made.append("Lower: Used previous frame (current in no-go)") self.stats['lower_adjustments'] += 1 if debug: print(f" Using previous lower sideline (was outside no-go)") else: # Both current and previous are in no-go zone, adjust to buffer line validated_lower['slope'] = no_go_zone['lower_buffer_line'][0] validated_lower['intercept'] = no_go_zone['lower_buffer_line'][1] adjustments_made.append("Lower: Adjusted to buffer line") self.stats['lower_adjustments'] += 1 if debug: print(f" Adjusting lower to buffer line") else: # No previous frame, adjust to buffer line validated_lower['slope'] = no_go_zone['lower_buffer_line'][0] validated_lower['intercept'] = no_go_zone['lower_buffer_line'][1] adjustments_made.append("Lower: Adjusted to buffer line (no previous)") self.stats['lower_adjustments'] += 1 # Store current frame as previous (using validated lines) if validated_upper: self.prev_upper_sideline = validated_upper.copy() if validated_lower: self.prev_lower_sideline = validated_lower.copy() return validated_upper, validated_lower, adjustments_made def get_stats(self): """Get validation statistics.""" total_validations = self.stats['frames_validated'] return { 'frames_validated': total_validations, 'upper_adjustment_rate': self.stats['upper_adjustments'] / max(1, total_validations), 'lower_adjustment_rate': self.stats['lower_adjustments'] / max(1, total_validations), 'upper_rejection_rate': self.stats['upper_rejected'] / max(1, total_validations), 'lower_rejection_rate': self.stats['lower_rejected'] / max(1, total_validations), 'upper_buffer_percentage': self.upper_buffer_percentage, 'lower_buffer_percentage': self.lower_buffer_percentage } # ============================================================================= # SIMPLIFIED VIRTUAL LINE VERIFICATION SYSTEM # ============================================================================= # ============================================================================= # NEW: SIDELINE VERIFIER (SAME LOGIC AS VIRTUAL LINE VERIFIER) # ============================================================================= class SidelineVerifier: def __init__(self, slope_change_threshold=0.15): # Store previous frame's sideline data self.prev_upper_sideline = None self.prev_lower_sideline = None # Slope change threshold self.slope_change_threshold = slope_change_threshold # Frame counters self.frame_count = 0 self.missing_frames = { 'upper_sideline': 0, 'lower_sideline': 0 } # Slope change counters self.slope_changes_rejected = { 'upper_sideline': 0, 'lower_sideline': 0 } # Statistics self.stats = { 'frames_processed': 0, 'upper_sideline_recovered': 0, 'lower_sideline_recovered': 0, 'upper_sideline_slope_rejected': 0, 'lower_sideline_slope_rejected': 0 } def validate_sideline_slope_change(self, current_line, prev_line, line_type, debug=False): if current_line is None or prev_line is None: return True # Can't validate # Extract slopes m_current = current_line.get('slope') m_prev = prev_line.get('slope') if m_current is None or m_prev is None: return True # Can't validate # Check for NaN or infinite values if np.isnan(m_current) or np.isinf(m_current) or np.isnan(m_prev) or np.isinf(m_prev): return True # Calculate absolute slope change slope_change = abs(m_current - m_prev) if debug: print(f" Sideline slope change validation for {line_type}:") print(f" Current slope: {m_current:.4f}, Previous slope: {m_prev:.4f}") print(f" Slope change: {slope_change:.4f}, Threshold: {self.slope_change_threshold}") # Check if slope change exceeds threshold if slope_change > self.slope_change_threshold: self.slope_changes_rejected[line_type] += 1 stat_key = f"{line_type}_slope_rejected" if stat_key in self.stats: self.stats[stat_key] += 1 if debug: print(f" ❌ Slope change too drastic - rejecting current sideline") return False return True def verify_and_fill_missing_sidelines(self, upper_line, lower_line, frame_width, frame_height, debug=False): self.frame_count += 1 # Helper function to check if sideline data is valid def is_valid_sideline(sideline): if sideline is None: return False # Check for required fields if 'slope' not in sideline or 'intercept' not in sideline: return False # Check for NaN or infinite values m, b = sideline['slope'], sideline['intercept'] if np.isnan(m) or np.isnan(b) or np.isinf(m) or np.isinf(b): return False return True # Check and validate upper sideline if is_valid_sideline(upper_line): # Check if we have previous frame to compare with if is_valid_sideline(self.prev_upper_sideline): # Validate slope change if not self.validate_sideline_slope_change(upper_line, self.prev_upper_sideline, 'upper_sideline', debug): # Slope change is too drastic - use previous frame's sideline upper_line = self.prev_upper_sideline.copy() self.stats['upper_sideline_recovered'] += 1 self.stats['upper_sideline_slope_rejected'] += 1 if debug: print(f" Upper sideline: Rejected due to drastic slope change, using previous frame") else: # Slope change is acceptable, update previous frame self.prev_upper_sideline = upper_line.copy() self.missing_frames['upper_sideline'] = 0 else: # No previous frame, store current one self.prev_upper_sideline = upper_line.copy() self.missing_frames['upper_sideline'] = 0 else: # Upper sideline is invalid self.missing_frames['upper_sideline'] += 1 if self.prev_upper_sideline is not None and is_valid_sideline(self.prev_upper_sideline): # Use previous frame's sideline upper_line = self.prev_upper_sideline.copy() self.stats['upper_sideline_recovered'] += 1 if debug: print(f" Upper sideline: Recovered from previous frame (missing for {self.missing_frames['upper_sideline']} frames)") else: if debug: print(f" Upper sideline: No valid previous frame available") # Check and validate lower sideline if is_valid_sideline(lower_line): # Check if we have previous frame to compare with if is_valid_sideline(self.prev_lower_sideline): # Validate slope change if not self.validate_sideline_slope_change(lower_line, self.prev_lower_sideline, 'lower_sideline', debug): # Slope change is too drastic - use previous frame's sideline lower_line = self.prev_lower_sideline.copy() self.stats['lower_sideline_recovered'] += 1 self.stats['lower_sideline_slope_rejected'] += 1 if debug: print(f" Lower sideline: Rejected due to drastic slope change, using previous frame") else: # Slope change is acceptable, update previous frame self.prev_lower_sideline = lower_line.copy() self.missing_frames['lower_sideline'] = 0 else: # No previous frame, store current one self.prev_lower_sideline = lower_line.copy() self.missing_frames['lower_sideline'] = 0 else: # Lower sideline is invalid self.missing_frames['lower_sideline'] += 1 if self.prev_lower_sideline is not None and is_valid_sideline(self.prev_lower_sideline): # Use previous frame's sideline lower_line = self.prev_lower_sideline.copy() self.stats['lower_sideline_recovered'] += 1 if debug: print(f" Lower sideline: Recovered from previous frame (missing for {self.missing_frames['lower_sideline']} frames)") else: if debug: print(f" Lower sideline: No valid previous frame available") # Update statistics self.stats['frames_processed'] = self.frame_count return upper_line, lower_line def get_stats(self): """Get verification statistics.""" return { 'total_frames': self.stats['frames_processed'], 'upper_sideline_recovery_rate': self.stats['upper_sideline_recovered'] / max(1, self.stats['frames_processed']), 'lower_sideline_recovery_rate': self.stats['lower_sideline_recovered'] / max(1, self.stats['frames_processed']), 'upper_sideline_slope_rejection_rate': self.stats['upper_sideline_slope_rejected'] / max(1, self.stats['frames_processed']), 'lower_sideline_slope_rejection_rate': self.stats['lower_sideline_slope_rejected'] / max(1, self.stats['frames_processed']), 'current_missing_counts': self.missing_frames.copy(), 'slope_change_threshold': self.slope_change_threshold, 'slope_changes_rejected': self.slope_changes_rejected.copy() } class SimpleVirtualLineVerifier: def __init__(self, slope_change_threshold=0.1): # Store previous frame's line parameters self.prev_upper_line_params = None self.prev_lower_line_params = None self.prev_upper_hash_params = None self.prev_lower_hash_params = None # Slope change threshold self.slope_change_threshold = slope_change_threshold # Frame counters self.frame_count = 0 self.missing_frames = { 'upper_line': 0, 'lower_line': 0, 'upper_hash': 0, 'lower_hash': 0 } # Slope change counters self.slope_changes_rejected = { 'upper_line': 0, 'lower_line': 0, 'upper_hash': 0, 'lower_hash': 0 } # Statistics self.stats = { 'frames_processed': 0, 'upper_line_recovered': 0, 'lower_line_recovered': 0, 'upper_hash_recovered': 0, 'lower_hash_recovered': 0, 'upper_line_slope_rejected': 0, 'lower_line_slope_rejected': 0, 'upper_hash_slope_rejected': 0, 'lower_hash_slope_rejected': 0 } def validate_slope_change(self, current_params, prev_params, line_name, debug=False): if current_params is None or prev_params is None: return True # No previous frame or no current frame, can't validate m_current, b_current = current_params m_prev, b_prev = prev_params # Check for NaN or infinite values if np.isnan(m_current) or np.isinf(m_current) or np.isnan(m_prev) or np.isinf(m_prev): return True # Can't validate with invalid values # Calculate absolute slope change slope_change = abs(m_current - m_prev) if debug: print(f" Slope change validation for {line_name}:") print(f" Current slope: {m_current:.4f}, Previous slope: {m_prev:.4f}") print(f" Slope change: {slope_change:.4f}, Threshold: {self.slope_change_threshold}") # Check if slope change exceeds threshold if slope_change > self.slope_change_threshold: self.slope_changes_rejected[line_name] += 1 stat_key = f"{line_name}_slope_rejected" if stat_key in self.stats: self.stats[stat_key] += 1 if debug: print(f" ❌ Slope change too drastic - rejecting current parameters") return False return True def verify_and_fill_missing_lines(self, virtual_hash_data, frame_width, frame_height, debug=False): self.frame_count += 1 # Helper function to check if line parameters are valid def is_valid_line_params(params): if params is None: return False m, b = params # Check for NaN or infinite values if np.isnan(m) or np.isnan(b) or np.isinf(m) or np.isinf(b): return False return True # Check each line type and fill if missing lines_to_check = [ ('upper_line_params', 'prev_upper_line_params', 'upper_line'), ('lower_line_params', 'prev_lower_line_params', 'lower_line'), ('upper_hash_line_params', 'prev_upper_hash_params', 'upper_hash'), ('lower_hash_line_params', 'prev_lower_hash_params', 'lower_hash') ] recovery_used = False slope_rejection_used = False for param_key, prev_param_key, line_name in lines_to_check: current_params = virtual_hash_data.get(param_key) prev_params = getattr(self, prev_param_key) # If current line is valid if is_valid_line_params(current_params): # Check if we have previous frame to compare with if is_valid_line_params(prev_params): # Validate slope change between frames if not self.validate_slope_change(current_params, prev_params, line_name, debug): # Slope change is too drastic - use previous frame parameters virtual_hash_data[param_key] = prev_params # Update stats stat_key = f"{line_name}_recovered" if stat_key in self.stats: self.stats[stat_key] += 1 recovery_used = True slope_rejection_used = True if debug: print(f" {line_name}: Rejected due to drastic slope change, using previous frame") # Update previous frame storage with the accepted (previous) parameters setattr(self, prev_param_key, prev_params) self.missing_frames[line_name] = 0 continue # Slope change is acceptable or no previous frame for comparison setattr(self, prev_param_key, current_params) self.missing_frames[line_name] = 0 else: # Line is missing or invalid in current frame self.missing_frames[line_name] += 1 # Try to recover from previous frame if is_valid_line_params(prev_params): # Use previous frame's parameters virtual_hash_data[param_key] = prev_params # Update stats stat_key = f"{line_name}_recovered" if stat_key in self.stats: self.stats[stat_key] += 1 recovery_used = True if debug: print(f" {line_name}: Recovered from previous frame (missing for {self.missing_frames[line_name]} frames)") else: if debug: print(f" {line_name}: No valid previous frame available") # Update statistics self.stats['frames_processed'] = self.frame_count # Add verification info to virtual_hash_data virtual_hash_data['verification_info'] = { 'frame': self.frame_count, 'missing_counts': self.missing_frames.copy(), 'recovery_used': recovery_used, 'slope_rejection_used': slope_rejection_used, 'slope_changes_rejected': self.slope_changes_rejected.copy() } return virtual_hash_data def get_stats(self): """Get verification statistics.""" total_rejections = ( self.stats.get('upper_line_slope_rejected', 0) + self.stats.get('lower_line_slope_rejected', 0) + self.stats.get('upper_hash_slope_rejected', 0) + self.stats.get('lower_hash_slope_rejected', 0) ) return { 'total_frames': self.stats['frames_processed'], 'upper_line_recovery_rate': self.stats['upper_line_recovered'] / max(1, self.stats['frames_processed']), 'lower_line_recovery_rate': self.stats['lower_line_recovered'] / max(1, self.stats['frames_processed']), 'upper_hash_recovery_rate': self.stats.get('upper_hash_recovered', 0) / max(1, self.stats['frames_processed']), 'lower_hash_recovery_rate': self.stats.get('lower_hash_recovered', 0) / max(1, self.stats['frames_processed']), 'total_slope_changes_rejected': total_rejections, 'current_missing_counts': self.missing_frames.copy(), 'slope_change_threshold': self.slope_change_threshold, 'slope_changes_rejected': self.slope_changes_rejected.copy() } def detect_virtual_and_hash_lines_with_validation(frame, number_model, hash_model, number_class_index, hash_confidence_threshold=0.3, hash_iou_threshold=0.45, frame_width=640, frame_height=480, debug=False, line_verifier=None, prev_upper_line_params=None, prev_lower_line_params=None): img_height, img_width = frame.shape[:2] # ========== STEP 1: Detect Numbers for Virtual Lines ========== results = number_model(frame, verbose=False) boxes = results[0].boxes if results and len(results) > 0 else None mid_y = img_height // 2 upper_centers = [] lower_centers = [] if boxes is not None: for box in boxes: cls = int(box.cls[0]) if cls != number_class_index: continue x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 if center_y < mid_y: upper_centers.append((center_x, center_y)) else: lower_centers.append((center_x, center_y)) # ========== STEP 2: Detect Hash Lines (only if hash_model is available) ========== filtered_hash_centers = [] filtered_hash_bboxes = [] upper_hash_centers = [] lower_hash_centers = [] upper_hash_bboxes = [] lower_hash_bboxes = [] # ONLY run hash detection if hash_model is provided AND is not None if hash_model is not None: try: results = hash_model(frame, verbose=False, conf=hash_confidence_threshold, iou=hash_iou_threshold) boxes = results[0].boxes if results and len(results) > 0 else None if boxes is not None: for box in boxes: x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 conf = float(box.conf[0]) if box.conf is not None else 0.0 filtered_hash_centers.append((center_x, center_y, conf)) filtered_hash_bboxes.append((x1, y1, x2, y2, conf)) # Split hash centers for i, (cx, cy, conf) in enumerate(filtered_hash_centers): bbox = filtered_hash_bboxes[i] if cy < mid_y: upper_hash_centers.append((cx, cy, conf)) upper_hash_bboxes.append(bbox) else: lower_hash_centers.append((cx, cy, conf)) lower_hash_bboxes.append(bbox) except Exception as e: if debug: print(f"Hash detection failed: {e}") # ========== STEP 3: Initial Line Fitting ========== upper_line_params = None lower_line_params = None upper_hash_line_params = None lower_hash_line_params = None # Try to fit upper virtual line if len(upper_centers) >= 2: try: upper_line_params = fit_line_with_robust_method( upper_centers, frame_width, img_height, 'virtual_top', debug=debug ) except: upper_line_params = None # If no current detection, use previous frame's parameters elif prev_upper_line_params is not None: upper_line_params = prev_upper_line_params if debug: print(f"Using previous frame's upper line params: {upper_line_params}") # Try to fit lower virtual line if len(lower_centers) >= 2: try: lower_line_params = fit_line_with_robust_method( lower_centers, frame_width, img_height, 'virtual_bottom', debug=debug ) except: lower_line_params = None # If no current detection, use previous frame's parameters elif prev_lower_line_params is not None: lower_line_params = prev_lower_line_params if debug: print(f"Using previous frame's lower line params: {lower_line_params}") # Try to fit hash lines if len(upper_hash_centers) >= 2: try: upper_hash_points = [(cx, cy) for cx, cy, _ in upper_hash_centers] upper_hash_line_params = fit_line_with_robust_method( upper_hash_points, frame_width, img_height, 'hash_top', debug=debug ) except: upper_hash_line_params = None if len(lower_hash_centers) >= 2: try: lower_hash_points = [(cx, cy) for cx, cy, _ in lower_hash_centers] lower_hash_line_params = fit_line_with_robust_method( lower_hash_points, frame_width, img_height, 'hash_bottom', debug=debug ) except: lower_hash_line_params = None # ========== STEP 4: Create initial virtual_hash_data with ALL REQUIRED KEYS ========== virtual_hash_data = { 'upper_centers': upper_centers, 'lower_centers': lower_centers, 'upper_hash_centers': [(cx, cy) for cx, cy, _ in upper_hash_centers], 'lower_hash_centers': [(cx, cy) for cx, cy, _ in lower_hash_centers], 'upper_hash_centers_with_conf': upper_hash_centers, 'lower_hash_centers_with_conf': lower_hash_centers, 'upper_hash_bboxes': upper_hash_bboxes, 'lower_hash_bboxes': lower_hash_bboxes, 'upper_line_params': upper_line_params, 'lower_line_params': lower_line_params, 'upper_hash_line_params': upper_hash_line_params, 'lower_hash_line_params': lower_hash_line_params, 'hash_detections_count': len(filtered_hash_centers), # ADD ALL LEGACY KEYS 'upper_line': upper_line_params, 'lower_line': lower_line_params, 'upper_hash_line': upper_hash_line_params, 'lower_hash_line': lower_hash_line_params, } # ========== STEP 5: Apply Enhanced Validation ========== try: validated_virtual_hash_data, validation_summary = enhanced_line_fitting_with_validation( virtual_hash_data, frame_width, img_height, debug=debug ) except Exception as e: if debug: print(f"Enhanced validation failed: {e}") # If validation fails, use the original data validated_virtual_hash_data = virtual_hash_data.copy() # ========== STEP 6: Apply Line Verification ========== if line_verifier is not None: try: validated_virtual_hash_data = line_verifier.verify_and_fill_missing_lines( validated_virtual_hash_data, frame_width, img_height, debug=debug ) except Exception as e: if debug: print(f"Line verification failed: {e}") # ========== STEP 7: Force hash lines to use virtual line slope ========== final_virtual_slope = None # Check both virtual lines and use the most reliable slope if validated_virtual_hash_data.get('upper_line_params'): m, b = validated_virtual_hash_data['upper_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): final_virtual_slope = m if validated_virtual_hash_data.get('lower_line_params'): m, b = validated_virtual_hash_data['lower_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): # If we already have a slope, average them for better stability if final_virtual_slope is not None: final_virtual_slope = (final_virtual_slope + m) / 2 else: final_virtual_slope = m # If we have a valid virtual slope, force hash lines to use it if final_virtual_slope is not None and abs(final_virtual_slope) < 10: # Fix upper hash line if validated_virtual_hash_data.get('upper_hash_centers') and len(validated_virtual_hash_data['upper_hash_centers']) >= 1: try: points = validated_virtual_hash_data['upper_hash_centers'] x_vals = np.array([p[0] for p in points]) y_vals = np.array([p[1] for p in points]) # Calculate intercept using virtual slope b_new = np.mean(y_vals - final_virtual_slope * x_vals) validated_virtual_hash_data['upper_hash_line_params'] = (final_virtual_slope, b_new) validated_virtual_hash_data['upper_hash_line'] = (final_virtual_slope, b_new) except: pass # Fix lower hash line if validated_virtual_hash_data.get('lower_hash_centers') and len(validated_virtual_hash_data['lower_hash_centers']) >= 1: try: points = validated_virtual_hash_data['lower_hash_centers'] x_vals = np.array([p[0] for p in points]) y_vals = np.array([p[1] for p in points]) # Calculate intercept using virtual slope b_new = np.mean(y_vals - final_virtual_slope * x_vals) validated_virtual_hash_data['lower_hash_line_params'] = (final_virtual_slope, b_new) validated_virtual_hash_data['lower_hash_line'] = (final_virtual_slope, b_new) except: pass # FINAL SAFETY CHECK: Ensure all required keys exist and update legacy keys final_required_keys = [ 'upper_line_params', 'lower_line_params', 'upper_hash_line_params', 'lower_hash_line_params', 'upper_line', 'lower_line', 'upper_hash_line', 'lower_hash_line' ] for key in final_required_keys: if key not in validated_virtual_hash_data: # Map params keys to legacy keys if key == 'upper_line' and 'upper_line_params' in validated_virtual_hash_data: validated_virtual_hash_data[key] = validated_virtual_hash_data['upper_line_params'] elif key == 'lower_line' and 'lower_line_params' in validated_virtual_hash_data: validated_virtual_hash_data[key] = validated_virtual_hash_data['lower_line_params'] elif key == 'upper_hash_line' and 'upper_hash_line_params' in validated_virtual_hash_data: validated_virtual_hash_data[key] = validated_virtual_hash_data['upper_hash_line_params'] elif key == 'lower_hash_line' and 'lower_hash_line_params' in validated_virtual_hash_data: validated_virtual_hash_data[key] = validated_virtual_hash_data['lower_hash_line_params'] else: validated_virtual_hash_data[key] = None return validated_virtual_hash_data def draw_enhanced_lines(frame, virtual_hash_data, near_sideline_points=None, overlay_image=None): img_height, img_width = frame.shape[:2] # Determine target for drawing target = overlay_image if overlay_image is not None else frame # Extract line parameters SAFELY using .get() method line_params = { 'VL_top': virtual_hash_data.get('upper_line_params'), 'VL_bottom': virtual_hash_data.get('lower_line_params'), 'HL_top': virtual_hash_data.get('upper_hash_line_params'), 'HL_bottom': virtual_hash_data.get('lower_hash_line_params') } # Consistent colors for each line type (with alpha for overlay) colors = { 'VL_top': (203, 192, 255, 200), # PINK for virtual lines with alpha 'VL_bottom': (203, 192, 255, 200), # PINK for virtual lines with alpha 'HL_top': (139, 0, 0, 200), # DARK BLUE for hash lines with alpha 'HL_bottom': (139, 0, 0, 200) # DARK BLUE for hash lines with alpha } centers = { 'VL_top': virtual_hash_data.get('upper_centers', []), 'VL_bottom': virtual_hash_data.get('lower_centers', []), 'HL_top': virtual_hash_data.get('upper_hash_centers', []), 'HL_bottom': virtual_hash_data.get('lower_hash_centers', []) } drawn_params = {} # Draw near-sideline points first (so they're in the background) if near_sideline_points: for point_data in near_sideline_points: if len(point_data) >= 3: cx, cy, conf = point_data[:3] # Draw near-sideline points with YELLOW color and cross marker if overlay_image is not None: # For overlay, use RGBA with alpha cv2.drawMarker(target, (int(cx), int(cy)), (0, 255, 255, 200), markerType=cv2.MARKER_TILTED_CROSS, markerSize=10, thickness=2) else: cv2.drawMarker(target, (int(cx), int(cy)), (0, 255, 255), markerType=cv2.MARKER_TILTED_CROSS, markerSize=10, thickness=2) # Add text to indicate near-sideline cv2.putText(target, "NS", (int(cx) + 10, int(cy)), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255, 255) if overlay_image is not None else (0, 255, 255), 1) # Draw verification status if available verification_info = virtual_hash_data.get('verification_info', {}) if verification_info.get('recovery_used', False): cv2.putText(target, "VIRTUAL LINES RECOVERED", (img_width // 2 - 100, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0, 255) if overlay_image is not None else (0, 255, 0), 2) # Draw hash lines FIRST (so virtual lines are on top if needed) for line_name in ['HL_top', 'HL_bottom']: if line_name in line_params and line_params[line_name] is not None: m, b = line_params[line_name] color = colors[line_name][:3] if overlay_image is None else colors[line_name] # Draw line across frame x_start, x_end = 0, img_width - 1 y_start = int(m * x_start + b) y_end = int(m * x_end + b) # Clip y values y_start = max(0, min(img_height - 1, y_start)) y_end = max(0, min(img_height - 1, y_end)) cv2.line(target, (x_start, y_start), (x_end, y_end), color, 2) drawn_params[line_name] = (m, b) # Add label label = "UPPER HASH" if line_name == 'HL_top' else "LOWER HASH" label_y = y_start + 15 if line_name == 'HL_top' else y_start - 15 cv2.putText(target, label, (img_width - 150, label_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) # Draw virtual lines for line_name in ['VL_top', 'VL_bottom']: if line_name in line_params and line_params[line_name] is not None: m, b = line_params[line_name] color = colors[line_name][:3] if overlay_image is None else colors[line_name] # Draw line across frame x_start, x_end = 0, img_width - 1 y_start = int(m * x_start + b) y_end = int(m * x_end + b) # Clip y values y_start = max(0, min(img_height - 1, y_start)) y_end = max(0, min(img_height - 1, y_end)) cv2.line(target, (x_start, y_start), (x_end, y_end), color, 2) drawn_params[line_name] = (m, b) # Draw centers for virtual lines for line_name, points in centers.items(): if line_name.startswith('VL'): # Only draw centers for virtual lines for (cx, cy) in points: color = colors[line_name][:3] if overlay_image is None else colors[line_name] cv2.circle(target, (cx, cy), 3, color, -1) return drawn_params # ============================================================================= # MODIFIED SIDELINE ESTIMATION FUNCTIONS WITH VALIDATION FILTER # ============================================================================= def extract_yard_line_endpoints(yard_line_data, debug=False): if not yard_line_data: return [], [] top_points = [] bottom_points = [] for line_dict in yard_line_data: extended_line = line_dict.get('extended_line', []) if len(extended_line) >= 4: x1, y1, x2, y2 = extended_line # Determine which endpoint is higher (top) and which is lower (bottom) if y1 < y2: top_points.append((x1, y1)) bottom_points.append((x2, y2)) else: top_points.append((x2, y2)) bottom_points.append((x1, y1)) if debug: print(f"Extracted {len(top_points)} top points and {len(bottom_points)} bottom points") return top_points, bottom_points def get_virtual_line_slope(virtual_hash_data): """ Get median slope from virtual lines only (not hash lines). """ slopes = [] # Only consider virtual lines (from numbers) if virtual_hash_data.get('upper_line_params'): m, _ = virtual_hash_data['upper_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): slopes.append(m) if virtual_hash_data.get('lower_line_params'): m, _ = virtual_hash_data['lower_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): slopes.append(m) if not slopes: return None # Use median for robustness return np.median(slopes) def remove_outliers_from_points(points, slope, max_distance=30): if not points or slope is None: return points, [] if len(points) < 3: return points, [True] * len(points) # Calculate temporary line through mean point x_vals = np.array([p[0] for p in points]) y_vals = np.array([p[1] for p in points]) mean_x = np.mean(x_vals) mean_y = np.mean(y_vals) # Line equation: y = slope * x + b b_temp = mean_y - slope * mean_x # Calculate distances distances = [] for x, y in points: y_line = slope * x + b_temp distance = abs(y - y_line) distances.append(distance) # Find median and median absolute deviation median_dist = np.median(distances) mad = np.median([abs(d - median_dist) for d in distances]) # Threshold for outliers (using 2.5 * MAD) threshold = 2.5 * mad if mad > 0 else max_distance # Filter points filtered_points = [] inlier_mask = [] for i, (point, distance) in enumerate(zip(points, distances)): if distance <= threshold: filtered_points.append(point) inlier_mask.append(True) else: inlier_mask.append(False) return filtered_points, inlier_mask # ============================================================================= # ORIGINAL CODE 2 FUNCTIONS (MODIFIED) # ============================================================================= class EnhancedNumberTracker: """Enhanced number tracker with classification stabilization""" def __init__(self, max_age=15, max_distance=50, min_confidence=0.5): """ Initialize enhanced number tracker with classification stabilization """ self.next_id = 0 self.tracks = [] # List of active tracks self.max_age = max_age self.max_distance = max_distance self.min_confidence = min_confidence self.frame_count = 0 # Enhanced stabilization parameters self.min_history_length = 8 self.history_confidence_threshold = 0.7 self.streak_threshold = 3 def calculate_center(self, bbox): """Calculate center of bounding box""" x1, y1, x2, y2 = bbox return ((x1 + x2) / 2, (y1 + y2) / 2) def calculate_distance(self, center1, center2): """Calculate Euclidean distance between two centers""" return np.sqrt((center1[0] - center2[0])**2 + (center1[1] - center2[1])**2) def get_stabilized_classification(self, track): """ Get stabilized classification based on track history with enhanced logic """ history = track['classification_history'] if len(history) < self.min_history_length: return track['class_name'], track['cls_conf'], 'current' # Count occurrences of each class in history class_counts = defaultdict(int) class_conf_sum = defaultdict(float) recent_class_counts = defaultdict(int) for entry in history: class_name = entry['class_name'] class_counts[class_name] += 1 class_conf_sum[class_name] += entry['cls_conf'] recent_window = min(10, len(history)) for entry in history[-recent_window:]: class_name = entry['class_name'] recent_class_counts[class_name] += 1 most_common_class = max(class_counts.items(), key=lambda x: x[1])[0] most_common_count = class_counts[most_common_class] total_count = len(history) consistency_ratio = most_common_count / total_count avg_confidence = class_conf_sum[most_common_class] / most_common_count current_class = track['class_name'] recent_current_count = recent_class_counts.get(current_class, 0) recent_ratio = recent_current_count / recent_window current_streak = 0 for i in range(1, min(self.streak_threshold + 2, len(history))): if history[-i]['class_name'] == current_class: current_streak += 1 else: break if consistency_ratio >= 0.8 and current_streak < 3: return most_common_class, avg_confidence, 'strong_history' if (current_streak >= self.streak_threshold and consistency_ratio < 0.7 and recent_ratio >= 0.6): return current_class, track['cls_conf'], 'recent_trend' if consistency_ratio >= self.history_confidence_threshold: return most_common_class, avg_confidence, 'historical_majority' return current_class, track['cls_conf'], 'current' def update(self, detections): """ Update tracks with new detections using stabilized classification """ self.frame_count += 1 # Remove old tracks self.tracks = [track for track in self.tracks if self.frame_count - track['last_seen'] <= self.max_age] for track in self.tracks: track['matched'] = False # Process each detection for detection in detections: if detection['det_conf'] < self.min_confidence: continue detection_center = self.calculate_center(detection['bbox']) best_match = None min_distance = float('inf') for i, track in enumerate(self.tracks): if track['matched']: continue distance = self.calculate_distance(detection_center, track['center']) if distance < min_distance and distance < self.max_distance: min_distance = distance best_match = i if best_match is not None: # Update existing track track = self.tracks[best_match] track['classification_history'].append({ 'frame': self.frame_count, 'class_name': detection['class_name'], 'cls_conf': detection['cls_conf'], 'center': detection_center }) max_history_length = 50 if len(track['classification_history']) > max_history_length: track['classification_history'] = track['classification_history'][-max_history_length:] # Update track track['bbox'] = detection['bbox'] track['center'] = detection_center track['det_conf'] = detection['det_conf'] track['current_cls_conf'] = detection['cls_conf'] track['current_class'] = detection['class_name'] track['last_seen'] = self.frame_count track['age'] = self.frame_count - track['first_seen'] track['matched'] = True # Get stabilized classification stabilized_class, stabilized_conf, method = self.get_stabilized_classification(track) track['class_name'] = stabilized_class track['cls_conf'] = stabilized_conf track['classification_method'] = method # CRITICAL: Add final_class and final_conf for yard line assignment track['final_class'] = stabilized_class track['final_conf'] = stabilized_conf # Update position history track['position_history'].append(detection_center) if len(track['position_history']) > 20: track['position_history'] = track['position_history'][-20:] # Update detection with track info detection['track_id'] = track['id'] detection['track_age'] = track['age'] detection['stabilized_class'] = stabilized_class detection['stabilized_conf'] = stabilized_conf detection['classification_method'] = method detection['track_confidence'] = detection['det_conf'] # CRITICAL: Add final_class and final_conf to detection detection['final_class'] = stabilized_class detection['final_conf'] = stabilized_conf else: # Create new track new_track = { 'id': self.next_id, 'bbox': detection['bbox'], 'center': detection_center, 'class_name': detection['class_name'], 'current_class': detection['class_name'], 'final_class': detection['class_name'], # Initialize final_class 'final_conf': detection['cls_conf'], # Initialize final_conf 'det_conf': detection['det_conf'], 'cls_conf': detection['cls_conf'], 'current_cls_conf': detection['cls_conf'], 'first_seen': self.frame_count, 'last_seen': self.frame_count, 'age': 0, 'matched': True, 'position_history': [detection_center], 'classification_history': [{ 'frame': self.frame_count, 'class_name': detection['class_name'], 'cls_conf': detection['cls_conf'], 'center': detection_center }], 'classification_method': 'new' } self.tracks.append(new_track) detection['track_id'] = self.next_id detection['track_age'] = 0 detection['track_confidence'] = detection['det_conf'] detection['stabilized_class'] = detection['class_name'] detection['stabilized_conf'] = detection['cls_conf'] detection['classification_method'] = 'new' # CRITICAL: Add final_class and final_conf to detection detection['final_class'] = detection['class_name'] detection['final_conf'] = detection['cls_conf'] self.next_id += 1 # ============================================================================= # MODIFIED YARD LINE TRACKER WITH LABEL STABILIZATION # ============================================================================= class YardLineTracker: """Tracks and stabilizes yard lines across frames WITH LABEL CONSISTENCY""" def __init__(self, max_age=15, max_distance=100, min_confidence=0.4): """ Initialize yard line tracker with label stabilization """ self.next_id = 0 self.tracks = [] self.max_age = max_age self.max_distance = max_distance self.min_confidence = min_confidence self.frame_count = 0 # Yard line stabilization parameters self.slope_history_length = 10 self.length_history_length = 10 self.slope_variation_threshold = 20 self.length_variation_threshold = 0.5 # Label stabilization self.label_history_length = 8 # Keep last 8 label assignments self.label_consistency_threshold = 0.7 # Need 70% consistency to change label self.min_label_confidence = 0.6 # Minimum confidence to accept label # Assignment tracking self.assignment_history = deque(maxlen=10) self.field_side_history = deque(maxlen=5) # Statistics self.stats = { 'labels_stabilized': 0, 'labels_rejected': 0, 'frames_processed': 0 } def calculate_line_properties(self, line): """Calculate properties of a line (x1, y1, x2, y2)""" x1, y1, x2, y2 = line # Calculate center center = ((x1 + x2) / 2, (y1 + y2) / 2) # Calculate slope angle dx = x2 - x1 dy = y2 - y1 if dx == 0: angle = 90.0 else: angle = np.degrees(np.arctan2(abs(dy), abs(dx))) if dy < 0: angle = 180 - angle # Calculate length length = np.sqrt(dx**2 + dy**2) # Calculate slope (m) and intercept (b) if abs(dx) < 1e-6: m = float('inf') b = x1 # Store x-intercept for vertical lines else: m = dy / dx b = y1 - m * x1 return { 'center': center, 'angle': angle, 'length': length, 'slope': m, 'intercept': b, 'is_vertical': self.is_vertical(angle), 'x_center': center[0] } def is_vertical(self, angle): """Check if angle is closer to vertical (90°) than horizontal (0° or 180°)""" dist_from_vertical = min(abs(angle - 90), abs(angle - 90)) dist_from_horizontal = min(abs(angle), abs(angle - 180), abs(angle + 180)) return dist_from_vertical < dist_from_horizontal def calculate_line_distance(self, line1_props, line2_props): """Calculate distance between two lines based on their centers""" center1 = line1_props['center'] center2 = line2_props['center'] return np.sqrt((center1[0] - center2[0])**2 + (center1[1] - center2[1])**2) def is_good_update(self, track, new_line_props): """Check if the new line is a good update for the track""" if not new_line_props['is_vertical']: return False, "Not vertical enough" if len(track['slope_history']) > 0: avg_slope = np.mean(track['slope_history']) avg_length = np.mean(track['length_history']) slope_diff = abs(new_line_props['angle'] - avg_slope) if slope_diff > self.slope_variation_threshold: return False, f"Slope variation too high: {slope_diff:.1f}°" if avg_length > 0: length_ratio = new_line_props['length'] / avg_length if abs(1 - length_ratio) > self.length_variation_threshold: return False, f"Length variation too high: {length_ratio:.2f}" return True, "Good update" def verify_and_stabilize_label(self, track, new_assignment): """ Verify and stabilize label for a track using historical consistency """ if not track or not new_assignment: return new_assignment current_yard_num = new_assignment.get('yard_line_num') current_confidence = new_assignment.get('confidence', 0.5) current_side = new_assignment.get('side', 'unknown') # Initialize label history if not exists if 'label_history' not in track: track['label_history'] = deque(maxlen=self.label_history_length) # If this is the first time seeing a label for this track if len(track['label_history']) == 0: track['label_history'].append({ 'yard_num': current_yard_num, 'side': current_side, 'confidence': current_confidence, 'frame': self.frame_count }) return new_assignment # Get the most consistent historical label yard_counts = {} side_counts = {} total_weight = 0 for label_entry in track['label_history']: yard_num = label_entry['yard_num'] side = label_entry['side'] confidence = label_entry['confidence'] frame_age = self.frame_count - label_entry['frame'] # Weight recent entries more heavily weight = confidence * (1.0 - min(1.0, frame_age / 30.0)) if yard_num not in yard_counts: yard_counts[yard_num] = 0 yard_counts[yard_num] += weight if side not in side_counts: side_counts[side] = 0 side_counts[side] += weight total_weight += weight if total_weight > 0 and yard_counts: # Find the most weighted historical yard number historical_yard = max(yard_counts.items(), key=lambda x: x[1])[0] historical_side = max(side_counts.items(), key=lambda x: x[1])[0] if side_counts else 'unknown' # Calculate consistency score historical_weight = yard_counts.get(historical_yard, 0) / total_weight # Check if current label differs from historical label if current_yard_num != historical_yard or current_side != historical_side: # Need to verify if the change is justified # Rule 1: High confidence current label with low historical consistency if current_confidence >= self.min_label_confidence and historical_weight < 0.5: # Accept the change self.stats['labels_stabilized'] += 1 # Rule 2: Strong historical consistency (above threshold) elif historical_weight >= self.label_consistency_threshold: # Reject the change, use historical label corrected_assignment = new_assignment.copy() corrected_assignment['yard_line_num'] = historical_yard corrected_assignment['side'] = historical_side corrected_assignment['original_yard_num'] = current_yard_num corrected_assignment['label_corrected'] = True corrected_assignment['confidence'] = max(current_confidence * 0.9, 0.5) # Reduce confidence slightly self.stats['labels_rejected'] += 1 return corrected_assignment # Rule 3: Low confidence current label elif current_confidence < self.min_label_confidence: # Reject low confidence change corrected_assignment = new_assignment.copy() corrected_assignment['yard_line_num'] = historical_yard corrected_assignment['side'] = historical_side corrected_assignment['original_yard_num'] = current_yard_num corrected_assignment['label_corrected'] = True corrected_assignment['confidence'] = 0.6 # Set to medium confidence self.stats['labels_rejected'] += 1 return corrected_assignment # Add current label to history track['label_history'].append({ 'yard_num': current_yard_num, 'side': current_side, 'confidence': current_confidence, 'frame': self.frame_count }) return new_assignment def update(self, yard_line_detections): """ Update yard line tracks with new detections """ self.frame_count += 1 self.stats['frames_processed'] += 1 # Remove old tracks self.tracks = [track for track in self.tracks if self.frame_count - track['last_seen'] <= self.max_age] for track in self.tracks: track['matched'] = False # Process each detection for detection in yard_line_detections: confidence = detection['confidence'] if confidence < self.min_confidence: continue extended_line = detection['extended_line'] new_line_props = self.calculate_line_properties(extended_line) if not new_line_props['is_vertical']: continue best_match = None min_distance = float('inf') for i, track in enumerate(self.tracks): if track['matched']: continue distance = self.calculate_line_distance(track['line_props'], new_line_props) if distance < min_distance and distance < self.max_distance: min_distance = distance best_match = i if best_match is not None: track = self.tracks[best_match] is_good_update, reason = self.is_good_update(track, new_line_props) if is_good_update: track['extended_line'] = extended_line track['line_props'] = new_line_props track['confidence'] = confidence track['last_seen'] = self.frame_count track['age'] = self.frame_count - track['first_seen'] track['matched'] = True track['slope_history'].append(new_line_props['angle']) track['length_history'].append(new_line_props['length']) if len(track['slope_history']) > self.slope_history_length: track['slope_history'] = track['slope_history'][-self.slope_history_length:] if len(track['length_history']) > self.length_history_length: track['length_history'] = track['length_history'][-self.length_history_length:] detection['track_id'] = track['id'] detection['is_good_update'] = True else: track['last_seen'] = self.frame_count track['matched'] = True detection['track_id'] = track['id'] detection['is_good_update'] = False else: if new_line_props['is_vertical']: new_track = { 'id': self.next_id, 'extended_line': extended_line, 'best_line': detection.get('best_line', extended_line), 'line_props': new_line_props, 'confidence': confidence, 'first_seen': self.frame_count, 'last_seen': self.frame_count, 'age': 0, 'matched': True, 'slope_history': [new_line_props['angle']], 'length_history': [new_line_props['length']], 'label_history': deque(maxlen=self.label_history_length) } self.tracks.append(new_track) detection['track_id'] = self.next_id detection['is_good_update'] = True self.next_id += 1 def stabilize_labels_in_assignments(self, yard_line_assignments): """ Apply label stabilization to yard line assignments using track history """ if not yard_line_assignments: return yard_line_assignments stabilized_assignments = [] for assignment in yard_line_assignments: # Get the track associated with this assignment track_id = None line_data = assignment.get('line_data', {}) extended_line = line_data.get('extended_line', []) if len(extended_line) >= 4: assignment_center = ((extended_line[0] + extended_line[2]) / 2, (extended_line[1] + extended_line[3]) / 2) # Find matching track by center position for track in self.tracks: if 'line_props' in track and 'center' in track['line_props']: track_center = track['line_props']['center'] distance = self.calculate_line_distance( {'center': assignment_center}, {'center': track_center} ) if distance < 30: # Within 30 pixels track_id = track['id'] # Apply label stabilization stabilized_assignment = self.verify_and_stabilize_label(track, assignment) stabilized_assignments.append(stabilized_assignment) break # If no matching track found, use assignment as-is if track_id is None: stabilized_assignments.append(assignment) return stabilized_assignments def get_tracks(self): """Get all active tracks""" return self.tracks def get_stats(self): """Get tracking and stabilization statistics""" return self.stats.copy() # Yard Line Assignment Functions from Code 1 def get_line_equation_from_points(x1, y1, x2, y2): """Get line equation parameters from two points""" if abs(x2 - x1) < 1e-6: return None, None, x1 else: m = (y2 - y1) / (x2 - x1) b = y1 - m * x1 return m, b, None def point_side_of_line(m, b, x, y, vertical_x=None): if vertical_x is not None: if abs(x - vertical_x) < 5: return 0 return -1 if x < vertical_x else 1 else: line_y = m * x + b if abs(y - line_y) < 5: return 0 return -1 if y < line_y else 1 def is_line_associated_with_number(line_points, number_bbox): x1_line, y1_line, x2_line, y2_line = line_points x1_num, y1_num, x2_num, y2_num = number_bbox m, b, vertical_x = get_line_equation_from_points(x1_line, y1_line, x2_line, y2_line) top_left = (x1_num, y1_num) top_right = (x2_num, y1_num) bottom_left = (x1_num, y2_num) bottom_right = (x2_num, y2_num) sides = [] for point in [top_left, top_right, bottom_left, bottom_right]: side = point_side_of_line(m, b, point[0], point[1], vertical_x) sides.append(side) left_side_top = sides[0] right_side_top = sides[1] left_side_bottom = sides[2] right_side_bottom = sides[3] left_consistent = (left_side_top * left_side_bottom >= 0) and abs(left_side_top + left_side_bottom) > 0 right_consistent = (right_side_top * right_side_bottom >= 0) and abs(right_side_top + right_side_bottom) > 0 sides_opposite = (left_side_top * right_side_top <= 0) and (left_side_bottom * right_side_bottom <= 0) return left_consistent and right_consistent and sides_opposite class FieldTemplateTracker: """ Tracks field numbering pattern and resolves conflicts using American football field logic. """ def __init__(self, max_history=10): self.max_history = max_history self.assignment_history = deque(maxlen=max_history) self.confidence_threshold = 0.8 self.field_side_history = deque(maxlen=5) # Slope thresholds for side detection self.slope_threshold_acute = -0.3 self.slope_threshold_obtuse = 0.3 self.slope_threshold_vertical = 5.0 def classify_line_by_slope(self, slope): """Classify yard line side based on slope characteristics.""" if slope is None: return 'unknown' if abs(slope) > self.slope_threshold_vertical: return 'C' if slope < self.slope_threshold_acute: return 'L' elif slope > self.slope_threshold_obtuse: return 'R' else: return 'unknown' def analyze_field_trend(self, current_assignments): """Analyze field trend with slope-based side verification.""" if not current_assignments: return {'side': 'unknown', 'trend': 'unknown', 'center_x': None} valid_assignments = [] for assignment in current_assignments: if 'x_center' in assignment: valid_assignments.append(assignment) else: if 'line_data' in assignment: line_data = assignment.get('line_data', {}) extended_line = line_data.get('extended_line', []) if len(extended_line) >= 4: x_center = (extended_line[0] + extended_line[2]) / 2 assignment['x_center'] = x_center valid_assignments.append(assignment) elif 'line_index' in assignment: assignment['x_center'] = assignment['line_index'] * 100 valid_assignments.append(assignment) if not valid_assignments: return {'side': 'unknown', 'trend': 'unknown', 'center_x': None} slope_sides = [] for assignment in valid_assignments: line_data = assignment.get('line_data', {}) slope = line_data.get('slope') if slope is not None: slope_side = self.classify_line_by_slope(slope) slope_sides.append(slope_side) slope_side_counts = Counter(slope_sides) sorted_assignments = sorted(valid_assignments, key=lambda x: x['x_center']) yard_numbers = [assn['yard_line_num'] for assn in sorted_assignments] if len(yard_numbers) < 2: return { 'side': 'unknown', 'trend': 'unknown', 'center_x': None, 'slope_analysis': slope_side_counts } is_increasing = all(yard_numbers[i] <= yard_numbers[i+1] for i in range(len(yard_numbers)-1)) is_decreasing = all(yard_numbers[i] >= yard_numbers[i+1] for i in range(len(yard_numbers)-1)) center_assignment = None for assn in sorted_assignments: if assn['yard_line_num'] == 50: center_assignment = assn break if is_increasing and not is_decreasing: side = 'left' trend = 'increasing' elif is_decreasing and not is_increasing: side = 'right' trend = 'decreasing' else: if center_assignment: left_assignments = [a for a in sorted_assignments if a['x_center'] < center_assignment['x_center']] right_assignments = [a for a in sorted_assignments if a['x_center'] > center_assignment['x_center']] left_trend = all(left_assignments[i]['yard_line_num'] <= left_assignments[i+1]['yard_line_num'] for i in range(len(left_assignments)-1)) if len(left_assignments) > 1 else True right_trend = all(right_assignments[i]['yard_line_num'] >= right_assignments[i+1]['yard_line_num'] for i in range(len(right_assignments)-1)) if len(right_assignments) > 1 else True if left_trend and right_trend: side = 'both' trend = 'normal' else: side = 'unknown' trend = 'mixed' else: side = 'unknown' trend = 'mixed' return { 'side': side, 'trend': trend, 'center_x': center_assignment['x_center'] if center_assignment else None, 'sorted_assignments': sorted_assignments, 'slope_analysis': slope_side_counts } def detect_field_side_for_line(self, line_assignment, field_analysis): """Determine line side with strict rule enforcement.""" yard_num = line_assignment['yard_line_num'] x_center = line_assignment['x_center'] line_data = line_assignment.get('line_data', {}) slope = line_data.get('slope') if yard_num == 50: return 'C' current_side = line_assignment.get('side', 'unknown') if current_side == 'C' and yard_num != 50: line_assignment['needs_side_correction'] = True if slope is not None: slope_side = self.classify_line_by_slope(slope) if slope_side != 'unknown': if slope_side == 'C' and yard_num != 50: pass else: return slope_side if field_analysis['center_x'] is not None: if x_center < field_analysis['center_x']: return 'L' else: return 'R' if field_analysis['trend'] == 'increasing': same_number_lines = [a for a in field_analysis['sorted_assignments'] if a['yard_line_num'] == yard_num] if len(same_number_lines) == 1: return 'L' else: same_number_lines.sort(key=lambda x: x['x_center']) line_position = same_number_lines.index(line_assignment) if line_assignment in same_number_lines else 0 if line_position == 0: return 'L' else: return 'R' elif field_analysis['trend'] == 'decreasing': same_number_lines = [a for a in field_analysis['sorted_assignments'] if a['yard_line_num'] == yard_num] if len(same_number_lines) == 1: return 'R' else: same_number_lines.sort(key=lambda x: x['x_center']) line_position = same_number_lines.index(line_assignment) if line_assignment in same_number_lines else 0 if line_position == 0: return 'L' else: return 'R' if self.field_side_history: most_common_side = Counter(self.field_side_history).most_common(1)[0][0] if most_common_side == 'left': return 'L' elif most_common_side == 'right': return 'R' return 'L' if x_center < 640 else 'R' def assign_sides_to_yard_lines(self, assignments, field_analysis): """Assign sides to yard lines.""" if not assignments: return assignments if field_analysis['side'] != 'unknown': self.field_side_history.append(field_analysis['side']) for assignment in assignments: side = self.detect_field_side_for_line(assignment, field_analysis) assignment['side'] = side line_data = assignment.get('line_data', {}) slope = line_data.get('slope') if slope is not None: assignment['slope'] = slope assignment['slope_side'] = self.classify_line_by_slope(slope) confidence = assignment.get('confidence', 0.5) if confidence > 0.7: assignment['side_color'] = (0, 0, 139) assignment['side_name'] = 'high_confidence' else: assignment['side_color'] = (0, 0, 139) assignment['side_name'] = side if side == 'L': assignment['side_full_name'] = 'left' elif side == 'R': assignment['side_full_name'] = 'right' else: assignment['side_full_name'] = 'center' return assignments def update_history(self, assignments): """Update assignment history.""" self.assignment_history.append(assignments) def predict_expected_yard(current_index, yard_lines, previous_assignments, direction): """Predict expected yard number based on pattern.""" if not previous_assignments: return None if direction == 'ltr': if len(previous_assignments) == 1: first_yard = previous_assignments[0]['yard_line_num'] if first_yard == 10: return 20 elif first_yard == 20: return 30 elif first_yard == 30: return 40 elif first_yard == 40: return 50 elif first_yard == 50: return 40 if len(previous_assignments) >= 2: prev_yards = [a['yard_line_num'] for a in previous_assignments[-2:]] if all(prev_yards[i] <= prev_yards[i+1] for i in range(len(prev_yards)-1)): return min(50, prev_yards[-1] + 10) else: return max(10, prev_yards[-1] - 10) elif direction == 'rtl': if len(previous_assignments) == 1: first_yard = previous_assignments[0]['yard_line_num'] if first_yard == 10: return 20 elif first_yard == 20: return 30 elif first_yard == 30: return 40 elif first_yard == 40: return 50 elif first_yard == 50: return 40 if len(previous_assignments) >= 2: prev_yards = [a['yard_line_num'] for a in previous_assignments[-2:]] if all(prev_yards[i] >= prev_yards[i+1] for i in range(len(prev_yards)-1)): return max(10, prev_yards[-1] - 10) else: return min(50, prev_yards[-1] + 10) return None def resolve_assignment_with_slope_guidance(yard_line, upper_markers, lower_markers, current_assignments, expected_yard, direction, slope_side, number_detections=None): """Resolve yard line assignment with probabilistic approach using ALL top predictions.""" # Helper function to get all predictions with confidence def get_all_predictions_with_confidence(markers_list): """Extract all predictions with their confidences from marker data.""" if not markers_list: return None # Handle the case where markers_list is a list of tuples (yard_number, confidence) if isinstance(markers_list[0], tuple) and len(markers_list[0]) == 2: # This is the old format: list of (yard_number, confidence) marker_counts = {} marker_confidences = {} for yard_num, conf in markers_list: if yard_num not in marker_counts: marker_counts[yard_num] = 0 marker_confidences[yard_num] = [] marker_counts[yard_num] += 1 marker_confidences[yard_num].append(conf) if not marker_counts: return None most_common_marker = max(marker_counts.items(), key=lambda x: x[1])[0] avg_confidence = sum(marker_confidences[most_common_marker]) / len(marker_confidences[most_common_marker]) return { 'marker': most_common_marker, 'avg_confidence': avg_confidence, 'count': marker_counts[most_common_marker], 'all_predictions': {most_common_marker: avg_confidence} # Simplified for old format } else: # New format: list of detection dictionaries marker_counts = {} marker_confidences = {} all_predictions_dict = {} # Store ALL predictions with confidences for detection in markers_list: # Get the final (stabilized) classification yard_num = int(detection.get('final_class', detection.get('class_name', '0'))) final_conf = detection.get('final_conf', detection.get('cls_conf', 0.5)) if yard_num not in marker_counts: marker_counts[yard_num] = 0 marker_confidences[yard_num] = [] all_predictions_dict[yard_num] = 0.0 marker_counts[yard_num] += 1 marker_confidences[yard_num].append(final_conf) # Also consider top3_predictions if available if 'top3_predictions' in detection: for pred_class, pred_conf in detection['top3_predictions']: pred_yard = int(pred_class) if isinstance(pred_class, str) and pred_class.isdigit() else pred_class # Store the maximum confidence for each yard if pred_yard not in all_predictions_dict: all_predictions_dict[pred_yard] = 0.0 all_predictions_dict[pred_yard] = max(all_predictions_dict[pred_yard], pred_conf) else: # If no top3, use the final classification all_predictions_dict[yard_num] = max(all_predictions_dict[yard_num], final_conf) if not marker_counts: return None most_common_marker = max(marker_counts.items(), key=lambda x: x[1])[0] avg_confidence = sum(marker_confidences[most_common_marker]) / len(marker_confidences[most_common_marker]) return { 'marker': most_common_marker, 'avg_confidence': avg_confidence, 'count': marker_counts[most_common_marker], 'all_predictions': all_predictions_dict } # Get marker data upper_marker_data = get_all_predictions_with_confidence(upper_markers) lower_marker_data = get_all_predictions_with_confidence(lower_markers) if not upper_marker_data and not lower_marker_data: return None # Extract data upper_marker = upper_marker_data['marker'] if upper_marker_data else None upper_avg_conf = upper_marker_data['avg_confidence'] if upper_marker_data else 0.0 upper_all_preds = upper_marker_data.get('all_predictions', {}) if upper_marker_data else {} lower_marker = lower_marker_data['marker'] if lower_marker_data else None lower_avg_conf = lower_marker_data['avg_confidence'] if lower_marker_data else 0.0 lower_all_preds = lower_marker_data.get('all_predictions', {}) if lower_marker_data else {} left_side_numbers = [10, 20, 30, 40] right_side_numbers = [40, 30, 20, 10] center_numbers = [50] base_assignment = None slope_bonus = 0.0 confidence_bonus = 0.0 # ========== ENHANCED PROBABILISTIC APPROACH ========== if (upper_marker is not None and upper_all_preds) or (lower_marker is not None and lower_all_preds): # Combine all predictions from upper and lower markers all_yards = set(list(upper_all_preds.keys()) + list(lower_all_preds.keys())) if not all_yards: # Fallback to simple approach if upper_marker is not None and lower_marker is not None: chosen_yard = upper_marker if upper_avg_conf >= lower_avg_conf else lower_marker assignment_method = f'{direction}_simple_conflict' elif upper_marker is not None: chosen_yard = upper_marker assignment_method = f'{direction}_upper_only' else: chosen_yard = lower_marker assignment_method = f'{direction}_lower_only' else: # Calculate combined score for each yard number yard_scores = {} for yard in all_yards: score = 0.0 # Add upper prediction score if yard in upper_all_preds: upper_conf = upper_all_preds[yard] score += upper_conf * 0.5 # Upper contributes 50% # Add lower prediction score if yard in lower_all_preds: lower_conf = lower_all_preds[yard] score += lower_conf * 0.5 # Lower contributes 50% # Bonus if both upper and lower have predictions for this yard if yard in upper_all_preds and yard in lower_all_preds: score += 0.2 # Agreement bonus yard_scores[yard] = score # Add track history from nearby number detections if number_detections is not None: track_yard_counts = {} line_x_center = yard_line.get('x_center', 0) for num_det in number_detections: num_x_center = (num_det.get('x1', 0) + num_det.get('x2', 0)) / 2 # If number is close to this line (within 50 pixels) if abs(num_x_center - line_x_center) < 50: yard_num = int(num_det.get('final_class', num_det.get('class_name', '0'))) track_age = num_det.get('track_age', 0) track_conf = num_det.get('final_conf', num_det.get('cls_conf', 0.5)) # Older tracks with high confidence get more weight age_weight = min(1.0, track_age / 10.0) total_weight = age_weight * track_conf * 0.3 # Up to 30% weight if yard_num not in track_yard_counts: track_yard_counts[yard_num] = 0.0 track_yard_counts[yard_num] += total_weight # Add track history scores for yard, track_score in track_yard_counts.items(): if yard in yard_scores: yard_scores[yard] += track_score else: yard_scores[yard] = track_score # Apply expected yard guidance if available if expected_yard is not None and expected_yard in yard_scores: yard_scores[expected_yard] += 0.15 # Expected yard bonus # Check for previous assignments to this line (temporal consistency) if current_assignments: line_x_center = yard_line.get('x_center', 0) prev_yards = [] for prev_assn in current_assignments[-3:]: # Check last 3 assignments prev_line_center = prev_assn.get('x_center', 0) if abs(prev_line_center - line_x_center) < 30: # Same line prev_yards.append(prev_assn.get('yard_line_num')) if prev_yards: most_common_prev = max(set(prev_yards), key=prev_yards.count) if prev_yards else None if most_common_prev in yard_scores: yard_scores[most_common_prev] += 0.25 # Strong temporal consistency bonus # Choose yard with highest combined score chosen_yard = max(yard_scores.items(), key=lambda x: x[1])[0] max_score = yard_scores[chosen_yard] # Calculate confidence based on score difference second_best_score = 0 if len(yard_scores) > 1: # Get second highest score sorted_scores = sorted(yard_scores.items(), key=lambda x: x[1], reverse=True) second_best_score = sorted_scores[1][1] if len(sorted_scores) > 1 else 0 score_difference = max_score - second_best_score # Confidence is based on score difference and average marker confidence avg_marker_conf = (upper_avg_conf + lower_avg_conf) / 2 if upper_marker and lower_marker else (upper_avg_conf or lower_avg_conf or 0.5) confidence = min(1.0, 0.5 + (score_difference * 2) + (avg_marker_conf - 0.5)) assignment_method = f'{direction}_probabilistic' base_assignment = { 'yard_line_num': chosen_yard, 'confidence': confidence, 'assignment_method': assignment_method, 'markers': [upper_marker, lower_marker], 'marker_confidences': [upper_avg_conf, lower_avg_conf], 'yard_scores': yard_scores, 'score_difference': score_difference, 'upper_predictions': upper_all_preds, 'lower_predictions': lower_all_preds, 'probabilistic_choice': True } confidence_bonus = min(0.05, (confidence - 0.5) * 0.1) # ========== FALLBACK: SIMPLE APPROACH (if no detailed predictions) ========== elif upper_marker is not None or lower_marker is not None: # Simple weighted approach if upper_marker is not None and lower_marker is not None: upper_weight = upper_avg_conf * (upper_marker_data.get('count', 1) if upper_marker_data else 1) lower_weight = lower_avg_conf * (lower_marker_data.get('count', 1) if lower_marker_data else 1) if upper_marker == lower_marker: chosen_yard = upper_marker assignment_method = f'{direction}_both_match' else: if upper_weight >= lower_weight: chosen_yard = upper_marker assignment_method = f'{direction}_weighted_upper' else: chosen_yard = lower_marker assignment_method = f'{direction}_weighted_lower' avg_marker_conf = (upper_avg_conf + lower_avg_conf) / 2 confidence = min(1.0, (upper_weight + lower_weight) / 2) elif upper_marker is not None: chosen_yard = upper_marker assignment_method = f'{direction}_upper_only' confidence = upper_avg_conf avg_marker_conf = upper_avg_conf else: chosen_yard = lower_marker assignment_method = f'{direction}_lower_only' confidence = lower_avg_conf avg_marker_conf = lower_avg_conf base_assignment = { 'yard_line_num': chosen_yard, 'confidence': confidence, 'assignment_method': assignment_method, 'markers': [upper_marker, lower_marker], 'marker_confidences': [upper_avg_conf, lower_avg_conf], 'simple_fallback': True } confidence_bonus = min(0.05, (avg_marker_conf - 0.5) * 0.1) # ========== CASE 3: Use expected yard guidance as last resort ========== if base_assignment is None and expected_yard is not None: base_assignment = { 'yard_line_num': expected_yard, 'confidence': 0.6, 'assignment_method': f'{direction}_expected_only', 'markers': [upper_marker, lower_marker], 'marker_confidences': [upper_avg_conf, lower_avg_conf] } confidence_bonus = 0.02 # ========== APPLY SLOPE GUIDANCE AND FINAL CONFIDENCE ========== if base_assignment: chosen_yard = base_assignment['yard_line_num'] # Apply slope guidance if slope_side != 'unknown': if (slope_side == 'L' and chosen_yard in left_side_numbers) or \ (slope_side == 'R' and chosen_yard in right_side_numbers) or \ (slope_side == 'C' and chosen_yard in center_numbers): slope_bonus = 0.05 else: slope_bonus = -0.05 # Calculate final confidence final_confidence = base_assignment['confidence'] + slope_bonus + confidence_bonus base_assignment['confidence'] = max(0.1, min(1.0, final_confidence)) base_assignment['confidence_breakdown'] = { 'base_confidence': base_assignment['confidence'] - slope_bonus - confidence_bonus, 'slope_bonus': slope_bonus, 'confidence_bonus': confidence_bonus, 'upper_marker_confidence': upper_avg_conf, 'lower_marker_confidence': lower_avg_conf } if slope_side != 'unknown': base_assignment['slope_side'] = slope_side base_assignment['line_index'] = yard_line.get('index', 0) if 'line_data' in yard_line: base_assignment['line_data'] = yard_line['line_data'] else: base_assignment['line_data'] = { 'extended_line': yard_line.get('extended_line', [0, 0, 100, 100]), 'confidence': yard_line.get('confidence', 0.5), 'slope': yard_line.get('slope', 0), 'length': yard_line.get('length', 100), 'box': yard_line.get('box', [0, 0, 100, 100]), 'best_line': yard_line.get('best_line', [0, 0, 100, 100]) } extended_line = base_assignment['line_data'].get('extended_line', []) if len(extended_line) >= 4: base_assignment['x_center'] = (extended_line[0] + extended_line[2]) / 2 else: base_assignment['x_center'] = yard_line.get('x_center', yard_line.get('index', 0) * 100) base_assignment['upper_marker'] = base_assignment['markers'][0] if base_assignment['markers'][0] is not None else 'None' base_assignment['lower_marker'] = base_assignment['markers'][1] if base_assignment['markers'][1] is not None else 'None' # Add marker consistency check if len(base_assignment['markers']) == 2: marker1, marker2 = base_assignment['markers'][0], base_assignment['markers'][1] if marker1 is not None and marker2 is not None: base_assignment['markers_agree'] = (marker1 == marker2) base_assignment['marker_difference'] = abs(marker1 - marker2) if marker1 != marker2 else 0 else: base_assignment['markers_agree'] = False base_assignment['marker_difference'] = 0 return base_assignment def process_direction(yard_lines_sorted, number_detections, direction, tracker): """Process yard lines with slope-based side awareness.""" assignments = [] for i, yard_line in enumerate(yard_lines_sorted): upper_markers = [] lower_markers = [] extended_line = yard_line.get('extended_line', []) line_data = yard_line.get('line_data', {}) slope = line_data.get('slope') # Track all number detections associated with this line all_markers = [] for num_det in number_detections: number_bbox = (num_det['x1'], num_det['y1'], num_det['x2'], num_det['y2']) if is_line_associated_with_number(extended_line, number_bbox): # CRITICAL: Always use final_class (stabilized classification) yard_number = int(num_det.get('final_class', num_det.get('class_name', '0'))) classification_confidence = num_det.get('final_conf', num_det.get('cls_conf', 0.5)) marker_center_y = (num_det['y1'] + num_det['y2']) / 2 line_center_y = (extended_line[1] + extended_line[3]) / 2 if len(extended_line) >= 4 else marker_center_y # Store marker with all relevant info marker_info = { 'yard_number': yard_number, 'confidence': classification_confidence, 'is_upper': marker_center_y < line_center_y, 'center_y': marker_center_y, 'track_id': num_det.get('track_id', None), 'track_age': num_det.get('track_age', 0) } all_markers.append(marker_info) if marker_center_y < line_center_y: upper_markers.append((yard_number, classification_confidence)) else: lower_markers.append((yard_number, classification_confidence)) slope_side = tracker.classify_line_by_slope(slope) if slope is not None else 'unknown' expected_yard = predict_expected_yard(i, yard_lines_sorted, assignments, direction) # CRITICAL: Apply additional logic when multiple markers are present if len(all_markers) >= 2: # Check if we have consistent markers from the same track track_markers = {} for marker in all_markers: track_id = marker.get('track_id') if track_id is not None: if track_id not in track_markers: track_markers[track_id] = [] track_markers[track_id].append(marker) # If we have markers from the same track that disagree, use the most confident one for track_id, markers in track_markers.items(): if len(markers) > 1: # Sort by confidence markers.sort(key=lambda x: x['confidence'], reverse=True) # Use the most confident marker best_marker = markers[0] if best_marker['is_upper']: upper_markers = [(best_marker['yard_number'], best_marker['confidence'])] else: lower_markers = [(best_marker['yard_number'], best_marker['confidence'])] break assignment = resolve_assignment_with_slope_guidance( yard_line, upper_markers, lower_markers, assignments, expected_yard, direction, slope_side, number_detections ) if assignment: assignments.append(assignment) return assignments def assign_yard_lines_dual_direction(yard_line_data, number_detections, frame_width, tracker): assignments = [] if not yard_line_data or not number_detections: return assignments # Prepare yard line data yard_lines_prepared = [] for i, line_dict in enumerate(yard_line_data): extended_line = line_dict.get('extended_line', []) if len(extended_line) >= 4: x_center = (extended_line[0] + extended_line[2]) / 2 yard_lines_prepared.append({ 'index': i, 'x_center': x_center, 'line_data': line_dict, 'extended_line': extended_line, 'confidence': line_dict.get('confidence', 0.5) }) if not yard_lines_prepared: return assignments # Prepare number detections with final_class number_detections_for_assignment = [] for det in number_detections: # CRITICAL: Always use final_class for assignment final_class = det.get('final_class', det.get('class_name', '0')) final_conf = det.get('final_conf', det.get('cls_conf', 0.5)) number_detections_for_assignment.append({ 'x1': det['x1'], 'y1': det['y1'], 'x2': det['x2'], 'y2': det['y2'], 'class_name': final_class, 'cls_conf': final_conf, 'final_class': final_class, # Explicitly add final_class 'final_conf': final_conf, # Explicitly add final_conf 'track_id': det.get('track_id', None), 'track_age': det.get('track_age', 0), 'stabilized_class': final_class, # For compatibility 'stabilized_conf': final_conf # For compatibility }) # Sort number detections by x-coordinate for easier debugging number_detections_for_assignment.sort(key=lambda x: (x['x1'] + x['x2']) / 2) # Process in both directions ltr_yard_lines = sorted(yard_lines_prepared, key=lambda x: x['x_center']) rtl_yard_lines = sorted(yard_lines_prepared, key=lambda x: x['x_center'], reverse=True) ltr_assignments = process_direction(ltr_yard_lines, number_detections_for_assignment, 'ltr', tracker) rtl_assignments = process_direction(rtl_yard_lines, number_detections_for_assignment, 'rtl', tracker) # Choose the direction with more consistent assignments ltr_consistency = calculate_assignment_consistency(ltr_assignments) rtl_consistency = calculate_assignment_consistency(rtl_assignments) # Prefer direction with higher consistency if ltr_consistency >= rtl_consistency: final_assignments = ltr_assignments direction_used = 'ltr' else: final_assignments = rtl_assignments direction_used = 'rtl' # Analyze field trend and assign sides field_analysis = tracker.analyze_field_trend(final_assignments) final_assignments = tracker.assign_sides_to_yard_lines(final_assignments, field_analysis) # Ensure all assignments have required fields for assignment in final_assignments: if 'x_center' not in assignment: assignment['x_center'] = assignment.get('line_index', 0) * 100 if 'color' not in assignment: assignment['color'] = (0, 0, 139) if 'side' not in assignment: assignment['side'] = 'L' # Add direction info for debugging assignment['direction_used'] = direction_used # Update tracker history tracker.update_history(final_assignments) return final_assignments def calculate_assignment_consistency(assignments): """Calculate consistency score for assignments.""" if len(assignments) < 2: return 1.0 yard_numbers = [a['yard_line_num'] for a in assignments] # Check if numbers follow a reasonable pattern max_diff = max(yard_numbers) - min(yard_numbers) expected_max_diff = (len(assignments) - 1) * 10 if max_diff > expected_max_diff + 20: # Allow some flexibility return 0.5 # Calculate average confidence avg_confidence = sum(a.get('confidence', 0.5) for a in assignments) / len(assignments) # Check for duplicate yard numbers unique_numbers = set(yard_numbers) if len(unique_numbers) < len(yard_numbers): return 0.6 * avg_confidence return avg_confidence def assign_intermediate_yard_lines(yard_line_tracks, yard_line_assignments, transform_matrix, field_params, width, height, debug=False): intermediate_assignments = [] if not yard_line_tracks or not yard_line_assignments: return intermediate_assignments # Filter out tracks that are already assigned as red lines assigned_centers = [] for assignment in yard_line_assignments: if 'line_data' in assignment: line_data = assignment['line_data'] extended_line = line_data.get('extended_line', []) if len(extended_line) >= 4: center_x = (extended_line[0] + extended_line[2]) / 2 center_y = (extended_line[1] + extended_line[3]) / 2 assigned_centers.append((center_x, center_y, assignment)) # Get unassigned tracks unassigned_tracks = [] for track in yard_line_tracks: if 'extended_line' not in track: continue extended_line = track['extended_line'] if len(extended_line) < 4: continue center_x = (extended_line[0] + extended_line[2]) / 2 center_y = (extended_line[1] + extended_line[3]) / 2 # Check if this track is already assigned is_assigned = False for assigned_x, assigned_y, _ in assigned_centers: if abs(center_x - assigned_x) < 20 and abs(center_y - assigned_y) < 20: is_assigned = True break if not is_assigned: unassigned_tracks.append({ 'track': track, 'center_x': center_x, 'center_y': center_y, 'extended_line': extended_line, 'confidence': track.get('confidence', 0.5) }) if not unassigned_tracks: return intermediate_assignments # Sort red assignments by x-coordinate red_assignments_sorted = sorted(yard_line_assignments, key=lambda a: a.get('x_center', 0)) # Group by side for sequence logic left_red_assignments = [a for a in red_assignments_sorted if a.get('side') == 'L'] right_red_assignments = [a for a in red_assignments_sorted if a.get('side') == 'R'] # Sort unassigned tracks by x-coordinate unassigned_tracks.sort(key=lambda t: t['center_x']) # Helper function to find sequence gaps and assign intermediate lines def assign_intermediate_for_side(red_assignments, side, is_left_side=True): side_intermediate_assignments = [] if len(red_assignments) < 2: return side_intermediate_assignments # Sort red assignments for this side red_assignments_sorted = sorted(red_assignments, key=lambda a: a.get('x_center', 0), reverse=not is_left_side) # Define yard number sequences if is_left_side: # Left side: 10, 15, 20, 25, 30, 35, 40, 45, 50 yard_sequence = [10, 15, 20, 25, 30, 35, 40, 45, 50] else: # Right side: 50, 45, 40, 35, 30, 25, 20, 15, 10 yard_sequence = [50, 45, 40, 35, 30, 25, 20, 15, 10] # Create a map of red yard numbers to their positions red_yard_to_position = {} for assignment in red_assignments_sorted: yard_num = assignment.get('yard_line_num') if yard_num is not None: red_yard_to_position[yard_num] = assignment.get('x_center', 0) # Find gaps between red lines where intermediate lines should be for i in range(len(yard_sequence)): current_yard = yard_sequence[i] # Skip if this is already a red line if current_yard in red_yard_to_position: continue # Check if this is an intermediate yard (15, 25, 35, 45) if current_yard not in [15, 25, 35, 45]: continue # Find surrounding red lines prev_red_yard = None next_red_yard = None for j in range(i-1, -1, -1): if yard_sequence[j] in red_yard_to_position: prev_red_yard = yard_sequence[j] break for j in range(i+1, len(yard_sequence)): if yard_sequence[j] in red_yard_to_position: next_red_yard = yard_sequence[j] break if prev_red_yard is None or next_red_yard is None: continue # Calculate expected position (linear interpolation) prev_pos = red_yard_to_position[prev_red_yard] next_pos = red_yard_to_position[next_red_yard] # Yard distance between consecutive numbers yard_diff_total = abs(next_red_yard - prev_red_yard) yard_diff_current = abs(current_yard - prev_red_yard) if yard_diff_total == 0: continue expected_position = prev_pos + (next_pos - prev_pos) * (yard_diff_current / yard_diff_total) # Find unassigned track closest to expected position best_track = None min_distance = float('inf') for track_data in unassigned_tracks: # Skip if track already assigned as intermediate already_assigned = False for existing in side_intermediate_assignments: if abs(existing['x_center'] - track_data['center_x']) < 10: already_assigned = True break if already_assigned: continue distance = abs(track_data['center_x'] - expected_position) if distance < min_distance and distance < 50: # Within 50 pixels min_distance = distance best_track = track_data if best_track and min_distance < 50: # Create intermediate assignment intermediate_assignment = { 'yard_line_num': current_yard, 'confidence': best_track['confidence'] * 0.7, # Lower confidence for intermediate 'assignment_method': f'intermediate_{side}_sequence', 'side': side, 'line_data': { 'extended_line': best_track['extended_line'], 'confidence': best_track['confidence'], 'slope': best_track['track'].get('line_props', {}).get('slope', 0), 'length': best_track['track'].get('line_props', {}).get('length', 100), 'box': best_track['track'].get('box', [0, 0, 100, 100]), 'best_line': best_track['track'].get('best_line', best_track['extended_line']) }, 'x_center': best_track['center_x'], 'color': (255, 0, 0), # BLUE color in BGR 'side_full_name': 'left' if side == 'L' else 'right', 'is_intermediate': True, 'intermediate_confidence': 0.6 } side_intermediate_assignments.append(intermediate_assignment) if debug: print(f"Assigned intermediate line: {current_yard}{side} at x={best_track['center_x']:.1f}") return side_intermediate_assignments # Assign intermediate lines for both sides left_intermediate = assign_intermediate_for_side(left_red_assignments, 'L', is_left_side=True) right_intermediate = assign_intermediate_for_side(right_red_assignments, 'R', is_left_side=False) intermediate_assignments = left_intermediate + right_intermediate # Also check for center intermediate lines (45 on both sides of 50) center_red = [a for a in red_assignments_sorted if a.get('side') == 'C'] if center_red and len(center_red) > 0: center_assignment = center_red[0] center_x = center_assignment.get('x_center', width/2) # Look for lines near center that could be 45 yard lines for track_data in unassigned_tracks: distance_to_center = abs(track_data['center_x'] - center_x) # Check if it's reasonably close to center (within 80 pixels) if distance_to_center < 80: # Determine which side (left or right of center) side = 'L' if track_data['center_x'] < center_x else 'R' yard_num = 45 # Check if this intermediate line isn't already assigned already_assigned = False for existing in intermediate_assignments: if (abs(existing['x_center'] - track_data['center_x']) < 20 and existing['yard_line_num'] == yard_num and existing['side'] == side): already_assigned = True break if not already_assigned: intermediate_assignment = { 'yard_line_num': yard_num, 'confidence': track_data['confidence'] * 0.6, 'assignment_method': f'intermediate_center_{side}', 'side': side, 'line_data': { 'extended_line': track_data['extended_line'], 'confidence': track_data['confidence'], 'slope': track_data['track'].get('line_props', {}).get('slope', 0), 'length': track_data['track'].get('line_props', {}).get('length', 100), 'box': track_data['track'].get('box', [0, 0, 100, 100]), 'best_line': track_data['track'].get('best_line', track_data['extended_line']) }, 'x_center': track_data['center_x'], 'color': (255, 0, 0), # BLUE color in BGR 'side_full_name': 'left' if side == 'L' else 'right', 'is_intermediate': True, 'intermediate_confidence': 0.6 } intermediate_assignments.append(intermediate_assignment) return intermediate_assignments # ADD THIS GLOBAL VARIABLE AT THE TOP OF YOUR FILE (near other globals) OPENCV_CUDA_AVAILABLE = False def detect_cv_lines_cpu(frame): """CPU version of line detection (kept as fallback)""" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150) lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10) return lines if lines is not None else [] def detect_cv_lines_gpu(frame): global OPENCV_CUDA_AVAILABLE if not OPENCV_CUDA_AVAILABLE: return detect_cv_lines_cpu(frame) # Fallback to existing CPU version try: # Upload frame to GPU gpu_frame = cv2.cuda_GpuMat() gpu_frame.upload(frame) # Convert to grayscale on GPU gpu_gray = cv2.cuda.cvtColor(gpu_frame, cv2.COLOR_BGR2GRAY) # Apply Canny edge detection on GPU gpu_edges = cv2.cuda.createCannyEdgeDetector(low_threshold=50, high_threshold=150) gpu_edges_detected = gpu_edges.detect(gpu_gray) # Create Hough segment detector on GPU hough_detector = cv2.cuda.createHoughSegmentDetector( rho=1, theta=np.pi/180, minLineLength=100, maxLineGap=10 ) # Detect lines on GPU gpu_lines = hough_detector.detect(gpu_edges_detected) if gpu_lines is not None and gpu_lines.size() > 0: # Download lines from GPU to CPU lines_cpu = gpu_lines.download() # Reshape from (N, 1, 4) to (N, 4) if len(lines_cpu.shape) == 3 and lines_cpu.shape[1] == 1: lines_cpu = lines_cpu.reshape(-1, 4) return lines_cpu return [] except Exception as e: print(f"GPU line detection failed, falling back to CPU: {e}") return detect_cv_lines_cpu(frame) # KEEP THIS FUNCTION FOR COMPATIBILITY - it will call GPU or CPU version def detect_cv_lines(frame): return detect_cv_lines_gpu(frame) def line_midpoint(line): x1, y1, x2, y2 = line return ((x1 + x2) / 2, (y1 + y2) / 2) def box_contains_point(box, point): x1, y1, x2, y2 = box px, py = point return x1 <= px <= x2 and y1 <= py <= y2 def extend_line_to_box(line, box): x1, y1, x2, y2 = line bx1, by1, bx2, by2 = box dx = x2 - x1 dy = y2 - y1 if abs(dx) < 1e-6: x_avg = (x1 + x2) / 2 return (int(x_avg), by1, int(x_avg), by2) slope = dy / dx intercept = y1 - slope * x1 intersections = [] try: x_top = (by1 - intercept) / slope if bx1 <= x_top <= bx2: intersections.append((x_top, by1)) x_bottom = (by2 - intercept) / slope if bx1 <= x_bottom <= bx2: intersections.append((x_bottom, by2)) y_left = slope * bx1 + intercept if by1 <= y_left <= by2: intersections.append((bx1, y_left)) y_right = slope * bx2 + intercept if by1 <= y_right <= by2: intersections.append((bx2, y_right)) except ZeroDivisionError: x_avg = (x1 + x2) / 2 return (int(x_avg), by1, int(x_avg), by2) unique_intersections = [] for point in intersections: if not any(abs(point[0]-p[0]) < 1 and abs(point[1]-p[1]) < 1 for p in unique_intersections): unique_intersections.append(point) if len(unique_intersections) >= 2: unique_intersections.sort(key=lambda p: (p[0] - x1)**2 + (p[1] - y1)**2) p1, p2 = unique_intersections[0], unique_intersections[-1] return (int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1])) x_avg = (x1 + x2) / 2 return (int(x_avg), by1, int(x_avg), by2) def yolo_detect_yard_lines_frame(frame, model, confidence_threshold=0.5): """Run YOLO detection on a frame and return yard line data with confidence""" results = model(frame, verbose=False) boxes = results[0].boxes yard_line_data = [] if boxes is not None: for box in boxes: cls = int(box.cls[0]) label = model.names[cls] if hasattr(model, 'names') else str(cls) if label == 'yard_line': confidence = float(box.conf[0]) if confidence < confidence_threshold: continue x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) cv_lines = detect_cv_lines(frame) best_line = None best_dist = float('inf') for l in cv_lines: x1_line, y1_line, x2_line, y2_line = l[0] mid = line_midpoint((x1_line, y1_line, x2_line, y2_line)) box_cx, box_cy = (x1 + x2) / 2, (y1 + y2) / 2 dist = np.hypot(mid[0] - box_cx, mid[1] - box_cy) if box_contains_point((x1, y1, x2, y2), mid) or dist < 50: if dist < best_dist: best_dist = dist best_line = (x1_line, y1_line, x2_line, y2_line) if best_line: extended_line = extend_line_to_box(best_line, (x1, y1, x2, y2)) # Calculate slope x1_ext, y1_ext, x2_ext, y2_ext = extended_line dx = x2_ext - x1_ext dy = y2_ext - y1_ext if abs(dx) < 1e-6: slope = float('inf') else: slope = dy / dx yard_line_data.append({ 'box': [x1, y1, x2, y2], 'extended_line': extended_line, 'confidence': confidence, 'best_line': best_line, 'slope': slope, 'length': np.sqrt(dx**2 + dy**2) }) return yard_line_data def create_resnet_classifier(num_classes=5): """Create a ResNet18 model for digit classification""" model = resnet18(pretrained=False) model.fc = nn.Linear(model.fc.in_features, num_classes) return model def load_classifier(weights_path): """Load classifier with GPU support""" # Use GPU if available, otherwise CPU map_location = DEVICE if DEVICE != 'mps' else 'cpu' # MPS uses CPU for loading checkpoint = torch.load(weights_path, map_location=map_location) if isinstance(checkpoint, dict): if "model_state" in checkpoint: state_dict = checkpoint["model_state"] num_classes = checkpoint.get("num_classes", 5) image_size = checkpoint.get("image_size", 224) elif "state_dict" in checkpoint: state_dict = checkpoint["state_dict"] num_classes = checkpoint.get("num_classes", 5) image_size = checkpoint.get("image_size", 224) else: state_dict = checkpoint num_classes = 5 image_size = 224 model = create_resnet_classifier(num_classes=num_classes) try: model.load_state_dict(state_dict) except RuntimeError as e: raise # Move model to appropriate device model = model.to(DEVICE) model.eval() # Enable half precision for GPU (FP16) for faster inference if DEVICE == 'cuda': model.half() print(f" Classifier: FP16 enabled for GPU") return model, num_classes, image_size # ============================================================================= # NEW: HASH LINE TRACKER - Tracks hash marks and ensures stable hash lines # ============================================================================= class HashLineTracker: """ Tracks hash mark objects and ensures stable hash line drawing. """ def __init__(self, max_age=15, max_distance=50, min_confidence=0.25): """ Initialize hash line tracker with stabilization. """ self.next_id = 0 self.tracks = [] # List of active hash mark tracks self.max_age = max_age self.max_distance = max_distance self.min_confidence = min_confidence self.frame_count = 0 # Store previous frame's hash line parameters self.prev_upper_hash_params = None self.prev_lower_hash_params = None # Store current frame's hash line parameters self.upper_hash_params = None self.lower_hash_params = None # Store hash mark positions self.upper_hash_centers = [] self.lower_hash_centers = [] # Statistics self.stats = { 'frames_processed': 0, 'hash_marks_detected': 0, 'hash_marks_tracked': 0, 'upper_hash_recovered': 0, 'lower_hash_recovered': 0, 'hash_marks_filtered': 0, 'upper_line_fitted': 0, 'lower_line_fitted': 0 } def calculate_center(self, bbox): """Calculate center of bounding box""" x1, y1, x2, y2 = bbox return ((x1 + x2) / 2, (y1 + y2) / 2) def calculate_distance(self, center1, center2): """Calculate Euclidean distance between two centers""" return np.sqrt((center1[0] - center2[0])**2 + (center1[1] - center2[1])**2) def filter_hash_mark_by_position(self, center, virtual_hash_data, frame_height): x, y = center # Check if hash mark is within frame bounds if x < 0 or x >= 640 or y < 0 or y >= frame_height: self.stats['hash_marks_filtered'] += 1 return False # Check if too close to top/bottom edges (first and last 10%) if y < frame_height * 0.1 or y > frame_height * 0.9: self.stats['hash_marks_filtered'] += 1 return False return True def update(self, hash_detections, frame_height=480, virtual_slope=None, no_go_zone=None, upper_virtual_params=None, lower_virtual_params=None): self.frame_count += 1 self.stats['frames_processed'] += 1 # Clear current centers self.upper_hash_centers = [] self.lower_hash_centers = [] # Remove old tracks self.tracks = [track for track in self.tracks if self.frame_count - track['last_seen'] <= self.max_age] for track in self.tracks: track['matched'] = False # Helper function to calculate perpendicular distance from point to line def distance_to_line(point, line_params): """Calculate perpendicular distance from point to line.""" if line_params is None: return float('inf') x, y = point m, b = line_params # For vertical lines (infinite slope) if abs(m) > 1000: # Approaching vertical return abs(x - b) # b is x-intercept for vertical lines # Distance formula: |mx - y + b| / sqrt(m² + 1) return abs(m * x - y + b) / np.sqrt(m**2 + 1) # Process each detection with virtual line distance-based classification for detection in hash_detections: if detection['confidence'] < self.min_confidence: continue detection_center = self.calculate_center(detection['bbox']) # Filter by position (frame edges) if not self.filter_hash_mark_by_position(detection_center, None, frame_height): continue # NEW: Filter by no-go zone (reject hash marks near sidelines) if no_go_zone: in_no_go, zone_type = is_point_in_no_go_zone(detection_center, no_go_zone, debug=False) if in_no_go: self.stats['hash_marks_filtered'] += 1 # Skip this hash mark - it's in no-go zone continue self.stats['hash_marks_detected'] += 1 # NEW: Classify as upper or lower based on distance to virtual lines is_upper = False if upper_virtual_params and lower_virtual_params: # Calculate distances to both virtual lines dist_to_upper = distance_to_line(detection_center, upper_virtual_params) dist_to_lower = distance_to_line(detection_center, lower_virtual_params) # Assign to the closer virtual line is_upper = dist_to_upper < dist_to_lower # Store classification info detection['distance_to_upper'] = dist_to_upper detection['distance_to_lower'] = dist_to_lower detection['is_upper'] = is_upper else: # Fallback: use frame midpoint if virtual lines not available y = detection_center[1] is_upper = y < frame_height / 2 detection['is_upper'] = is_upper detection['distance_to_upper'] = float('inf') detection['distance_to_lower'] = float('inf') # Now find matching track based on this hash mark best_match = None min_distance = float('inf') for i, track in enumerate(self.tracks): if track['matched']: continue distance = self.calculate_distance(detection_center, track['center']) if distance < min_distance and distance < self.max_distance: min_distance = distance best_match = i if best_match is not None: # Update existing track track = self.tracks[best_match] track['bbox'] = detection['bbox'] track['center'] = detection_center track['confidence'] = detection['confidence'] track['last_seen'] = self.frame_count track['age'] = self.frame_count - track['first_seen'] track['matched'] = True track['position_history'].append(detection_center) track['is_upper'] = is_upper # Store classification if len(track['position_history']) > 20: track['position_history'] = track['position_history'][-20:] self.stats['hash_marks_tracked'] += 1 else: # Create new track new_track = { 'id': self.next_id, 'bbox': detection['bbox'], 'center': detection_center, 'confidence': detection['confidence'], 'first_seen': self.frame_count, 'last_seen': self.frame_count, 'age': 0, 'matched': True, 'position_history': [detection_center], 'is_upper': is_upper # Store classification } self.tracks.append(new_track) self.next_id += 1 self.stats['hash_marks_tracked'] += 1 # After updating tracks, separate into upper and lower hash marks # based on the classification stored during update for track in self.tracks: if track['matched'] or (self.frame_count - track['last_seen'] <= 2): center = track['center'] # Use the stored classification if track.get('is_upper', False): self.upper_hash_centers.append(center) else: self.lower_hash_centers.append(center) # Fit lines to hash mark centers return self.fit_hash_lines(virtual_slope, frame_height) def fit_hash_lines(self, virtual_slope=None, frame_height=480, debug=False): # Fit upper hash line if len(self.upper_hash_centers) >= 2: x_vals = np.array([c[0] for c in self.upper_hash_centers]) y_vals = np.array([c[1] for c in self.upper_hash_centers]) try: if virtual_slope is not None: # Force use of virtual slope for consistency b = np.mean(y_vals - virtual_slope * x_vals) current_upper_params = (virtual_slope, b) else: coeffs = np.polyfit(x_vals, y_vals, 1) current_upper_params = (coeffs[0], coeffs[1]) # Compare with previous frame best_upper_params = self.select_best_hash_line_based_on_distance( current_upper_params, self.prev_upper_hash_params, self.upper_hash_centers, is_upper=True, debug=debug ) self.upper_hash_params = best_upper_params self.prev_upper_hash_params = best_upper_params self.stats['upper_line_fitted'] += 1 except Exception as e: # Use previous frame if fitting fails self.upper_hash_params = self.prev_upper_hash_params if debug: print(f" Upper hash line fitting failed: {e}") elif self.prev_upper_hash_params is not None: # Use previous frame's parameters self.upper_hash_params = self.prev_upper_hash_params self.stats['upper_hash_recovered'] += 1 if debug: print(" Using previous upper hash line (not enough points)") else: self.upper_hash_params = None # Fit lower hash line if len(self.lower_hash_centers) >= 2: x_vals = np.array([c[0] for c in self.lower_hash_centers]) y_vals = np.array([c[1] for c in self.lower_hash_centers]) try: if virtual_slope is not None: # Force use of virtual slope for consistency b = np.mean(y_vals - virtual_slope * x_vals) current_lower_params = (virtual_slope, b) else: coeffs = np.polyfit(x_vals, y_vals, 1) current_lower_params = (coeffs[0], coeffs[1]) # Compare with previous frame best_lower_params = self.select_best_hash_line_based_on_distance( current_lower_params, self.prev_lower_hash_params, self.lower_hash_centers, is_upper=False, debug=debug ) self.lower_hash_params = best_lower_params self.prev_lower_hash_params = best_lower_params self.stats['lower_line_fitted'] += 1 except Exception as e: # Use previous frame if fitting fails self.lower_hash_params = self.prev_lower_hash_params if debug: print(f" Lower hash line fitting failed: {e}") elif self.prev_lower_hash_params is not None: # Use previous frame's parameters self.lower_hash_params = self.prev_lower_hash_params self.stats['lower_hash_recovered'] += 1 if debug: print(" Using previous lower hash line (not enough points)") else: self.lower_hash_params = None return self.upper_hash_params, self.lower_hash_params def select_best_hash_line_based_on_distance(self, current_params, previous_params, hash_centers, is_upper=True, debug=False): """ Select the best hash line (current or previous) based on minimum distance to hash points. """ if not hash_centers: return current_params if current_params else previous_params best_params = compare_hash_line_quality( current_params, previous_params, hash_centers, is_upper ) if debug and best_params != current_params: m_current, b_current = current_params if current_params else (None, None) m_best, b_best = best_params if best_params else (None, None) print(f" Selected {'upper' if is_upper else 'lower'} hash line from previous frame") print(f" Current: m={m_current:.4f}, b={b_current:.1f}") print(f" Best: m={m_best:.4f}, b={b_best:.1f}") return best_params def get_hash_line_params(self): return self.upper_hash_params, self.lower_hash_params def get_hash_centers(self): return self.upper_hash_centers, self.lower_hash_centers def get_tracked_hash_marks(self): hash_marks = [] for track in self.tracks: if track['matched'] or (self.frame_count - track['last_seen'] <= 2): hash_marks.append({ 'center': track['center'], 'track_id': track['id'], 'age': track['age'], 'confidence': track['confidence'] }) return hash_marks def get_stats(self): """Get tracking statistics.""" return self.stats.copy() # ============================================================================= # MODIFIED HASH LINE VALIDATOR # ============================================================================= class HashLineValidator: def __init__(self): # Store previous frame's hash line parameters self.prev_upper_hash_params = None self.prev_lower_hash_params = None # Frame counters self.frame_count = 0 self.missing_frames = { 'upper_hash_line': 0, 'lower_hash_line': 0 } # Statistics self.stats = { 'frames_processed': 0, 'upper_hash_recovered': 0, 'lower_hash_recovered': 0, 'upper_hash_validated': 0, 'lower_hash_validated': 0 } # Validation parameters self.max_slope_change = 0.1 # Maximum allowed slope change between frames self.min_hash_marks = 2 # Minimum hash marks needed for fitting def is_valid_hash_line(self, hash_params, virtual_slope=None): """ Check if hash line parameters are valid. """ if hash_params is None: return False m, b = hash_params # Check for NaN or infinite values if np.isnan(m) or np.isnan(b) or np.isinf(m) or np.isinf(b): return False # Check if slope is reasonable (not too steep) if abs(m) > 2.0: # Too steep for hash line return False # Check if slope is similar to virtual slope (if available) if virtual_slope is not None and abs(m - virtual_slope) > 0.5: return False return True def validate_and_fill_missing_hash_lines(self, upper_hash_params, lower_hash_params, virtual_slope=None, debug=False): self.frame_count += 1 self.stats['frames_processed'] += 1 # ========== VALIDATE UPPER HASH LINE ========== validated_upper = upper_hash_params if not self.is_valid_hash_line(upper_hash_params, virtual_slope): self.missing_frames['upper_hash_line'] += 1 if debug: print(f" Upper hash line invalid, missing for {self.missing_frames['upper_hash_line']} frames") # Try to use previous frame's parameters if self.prev_upper_hash_params is not None: if self.is_valid_hash_line(self.prev_upper_hash_params, virtual_slope): validated_upper = self.prev_upper_hash_params self.stats['upper_hash_recovered'] += 1 if debug: print(f" Using previous upper hash line (recovered)") else: # Previous frame also invalid, try to create from virtual slope if virtual_slope is not None: # Use virtual slope with reasonable intercept validated_upper = (virtual_slope, 100) # Default intercept if debug: print(f" Using virtual slope for upper hash line") else: if debug: print(f" No valid previous upper hash line available") else: # Current hash line is valid validated_upper = upper_hash_params self.prev_upper_hash_params = validated_upper self.missing_frames['upper_hash_line'] = 0 self.stats['upper_hash_validated'] += 1 # ========== VALIDATE LOWER HASH LINE ========== validated_lower = lower_hash_params if not self.is_valid_hash_line(lower_hash_params, virtual_slope): self.missing_frames['lower_hash_line'] += 1 if debug: print(f" Lower hash line invalid, missing for {self.missing_frames['lower_hash_line']} frames") # Try to use previous frame's parameters if self.prev_lower_hash_params is not None: if self.is_valid_hash_line(self.prev_lower_hash_params, virtual_slope): validated_lower = self.prev_lower_hash_params self.stats['lower_hash_recovered'] += 1 if debug: print(f" Using previous lower hash line (recovered)") else: # Previous frame also invalid, try to create from virtual slope if virtual_slope is not None: # Use virtual slope with reasonable intercept validated_lower = (virtual_slope, 300) # Default intercept if debug: print(f" Using virtual slope for lower hash line") else: if debug: print(f" No valid previous lower hash line available") else: # Current hash line is valid validated_lower = lower_hash_params self.prev_lower_hash_params = validated_lower self.missing_frames['lower_hash_line'] = 0 self.stats['lower_hash_validated'] += 1 return validated_upper, validated_lower def get_virtual_slope(self, virtual_hash_data): if not virtual_hash_data: return None slopes = [] if virtual_hash_data.get('upper_line_params'): m, _ = virtual_hash_data['upper_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): slopes.append(m) if virtual_hash_data.get('lower_line_params'): m, _ = virtual_hash_data['lower_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): slopes.append(m) if not slopes: return None return np.median(slopes) def get_stats(self): """Get validation statistics.""" return self.stats.copy() class GPUBlender: def __init__(self): self.gpu_available = OPENCV_CUDA_AVAILABLE self.gpu_frame = None self.gpu_overlay = None def upload_frame_to_gpu(self, frame): """Upload frame to GPU if available.""" if self.gpu_available: if self.gpu_frame is None: self.gpu_frame = cv2.cuda_GpuMat() self.gpu_frame.upload(frame) return self.gpu_available def upload_overlay_to_gpu(self, overlay): """Upload overlay to GPU if available.""" if self.gpu_available: if self.gpu_overlay is None: self.gpu_overlay = cv2.cuda_GpuMat() self.gpu_overlay.upload(overlay) return self.gpu_available def blend_gpu(self): """Blend frame and overlay on GPU.""" if not self.gpu_available or self.gpu_frame is None or self.gpu_overlay is None: return None try: # Split channels for alpha blending gpu_frame_bgr = self.gpu_frame gpu_overlay_bgra = self.gpu_overlay # Split overlay into BGR and alpha overlay_channels = cv2.cuda.split(gpu_overlay_bgra) gpu_overlay_bgr = cv2.cuda.merge([overlay_channels[0], overlay_channels[1], overlay_channels[2]]) gpu_overlay_alpha = overlay_channels[3] # Convert alpha to float for blending gpu_alpha_float = cv2.cuda_GpuMat(gpu_overlay_alpha.size(), cv2.CV_32FC1) gpu_overlay_alpha.convertTo(gpu_alpha_float, cv2.CV_32FC1, 1.0/255.0) # Blend using alpha composition gpu_result = cv2.cuda_GpuMat(gpu_frame_bgr.size(), gpu_frame_bgr.type()) # For each channel: result = frame * (1 - alpha) + overlay * alpha frame_channels = cv2.cuda.split(gpu_frame_bgr) overlay_channels_bgr = cv2.cuda.split(gpu_overlay_bgr) blended_channels = [] for i in range(3): # Convert to float for blending frame_channel_float = cv2.cuda_GpuMat(frame_channels[i].size(), cv2.CV_32FC1) overlay_channel_float = cv2.cuda_GpuMat(overlay_channels_bgr[i].size(), cv2.CV_32FC1) frame_channels[i].convertTo(frame_channel_float, cv2.CV_32FC1) overlay_channels_bgr[i].convertTo(overlay_channel_float, cv2.CV_32FC1) # Blend: frame*(1-alpha) + overlay*alpha blended = cv2.cuda.add( cv2.cuda.multiply(frame_channel_float, cv2.cuda.subtract(1.0, gpu_alpha_float)), cv2.cuda.multiply(overlay_channel_float, gpu_alpha_float) ) # Convert back to uint8 blended_uint8 = cv2.cuda_GpuMat(blended.size(), cv2.CV_8UC1) blended.convertTo(blended_uint8, cv2.CV_8UC1) blended_channels.append(blended_uint8) # Merge channels back gpu_result = cv2.cuda.merge(blended_channels) return gpu_result except Exception as e: print(f"GPU blending failed: {e}, falling back to CPU") return None def blend_cpu(self, frame, overlay): """Blend frame and overlay on CPU (fallback).""" # Extract BGR and alpha from overlay overlay_bgr = overlay[:, :, :3] overlay_alpha = overlay[:, :, 3] / 255.0 # Ensure overlay_alpha is 3D for broadcasting overlay_alpha = overlay_alpha[:, :, np.newaxis] # Blend: frame*(1-alpha) + overlay*alpha blended = frame.astype(np.float32) * (1 - overlay_alpha) + overlay_bgr.astype(np.float32) * overlay_alpha blended = np.clip(blended, 0, 255).astype(np.uint8) return blended def download_from_gpu(self, gpu_mat): """Download from GPU to CPU.""" if gpu_mat is not None: return gpu_mat.download() return None def cleanup(self): """Clean up GPU resources.""" self.gpu_frame = None self.gpu_overlay = None def draw_hash_points_with_colors(frame, upper_centers, lower_centers, overlay_image=None): target = overlay_image if overlay_image is not None else frame # Draw upper hash points in CYAN for center in upper_centers: if len(center) >= 2: x, y = int(center[0]), int(center[1]) if overlay_image is not None: cv2.circle(target, (x, y), 5, (255, 255, 0, 200), -1) # CYAN with alpha else: cv2.circle(target, (x, y), 5, (255, 255, 0), -1) # CYAN # Draw lower hash points in MAGENTA for center in lower_centers: if len(center) >= 2: x, y = int(center[0]), int(center[1]) if overlay_image is not None: cv2.circle(target, (x, y), 5, (255, 0, 255, 200), -1) # MAGENTA with alpha else: cv2.circle(target, (x, y), 5, (255, 0, 255), -1) # MAGENTA def compare_hash_line_quality(current_line_params, previous_line_params, hash_points, is_upper=True): if current_line_params is None and previous_line_params is None: return None if current_line_params is None: return previous_line_params if previous_line_params is None: return current_line_params if not hash_points: return current_line_params # Helper function to calculate distance from point to line def point_to_line_distance(point, slope, intercept): x, y = point # Line equation: y = mx + b # Distance formula: |mx - y + b| / sqrt(m^2 + 1) return abs(slope * x - y + intercept) / np.sqrt(slope**2 + 1) # Unpack parameters m_current, b_current = current_line_params m_prev, b_prev = previous_line_params # Calculate total distances current_total_dist = 0 prev_total_dist = 0 count = 0 for point in hash_points: # Extract x, y from point (could be (x, y) or (x, y, conf)) if len(point) >= 2: x, y = point[0], point[1] current_total_dist += point_to_line_distance((x, y), m_current, b_current) prev_total_dist += point_to_line_distance((x, y), m_prev, b_prev) count += 1 if count == 0: return current_line_params # Calculate average distances current_avg_dist = current_total_dist / count prev_avg_dist = prev_total_dist / count # Choose the line with minimum average distance if current_avg_dist <= prev_avg_dist: return current_line_params else: return previous_line_params def detect_hash_marks_with_tracker(frame, hash_model, hash_tracker, hash_validator, virtual_hash_data=None, frame_width=640, frame_height=480, hash_confidence_threshold=0.25, hash_iou_threshold=0.45, debug=False, no_go_zone=None, output_frame=None): if hash_model is None: # No hash detector available, use previous frame's parameters upper_params, lower_params = hash_tracker.get_hash_line_params() upper_centers, lower_centers = hash_tracker.get_hash_centers() tracked_hash_marks = hash_tracker.get_tracked_hash_marks() return upper_params, lower_params, upper_centers, lower_centers, tracked_hash_marks try: # Run hash detection results = hash_model(frame, verbose=False, conf=hash_confidence_threshold, iou=hash_iou_threshold) boxes = results[0].boxes if results and len(results) > 0 else None hash_detections = [] if boxes is not None: for box in boxes: x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) conf = float(box.conf[0]) if box.conf is not None else 0.0 hash_detections.append({ 'bbox': (x1, y1, x2, y2), 'confidence': conf }) # Get virtual slope for line fitting virtual_slope = None if hash_validator: virtual_slope = hash_validator.get_virtual_slope(virtual_hash_data) # Get virtual line parameters for distance-based classification upper_virtual_params = None lower_virtual_params = None if virtual_hash_data: upper_virtual_params = virtual_hash_data.get('upper_line_params') lower_virtual_params = virtual_hash_data.get('lower_line_params') # NEW: Update hash tracker with new detections AND no-go zone filtering # AND distance-based classification using virtual lines upper_hash_params, lower_hash_params = hash_tracker.update( hash_detections, frame_height, virtual_slope, no_go_zone, upper_virtual_params, lower_virtual_params # Pass virtual line parameters ) # Get tracked hash marks and centers tracked_hash_marks = hash_tracker.get_tracked_hash_marks() upper_centers, lower_centers = hash_tracker.get_hash_centers() # VISUALIZATION: If output_frame is provided, draw on it if output_frame is not None: # Visualize hash points with different colors draw_hash_points_with_colors(output_frame, upper_centers, lower_centers) # CRITICAL: Update virtual_hash_data with tracked hash line parameters if virtual_hash_data is not None: # Store hash line parameters virtual_hash_data['upper_hash_line_params'] = upper_hash_params virtual_hash_data['lower_hash_line_params'] = lower_hash_params # Store hash mark centers virtual_hash_data['upper_hash_centers'] = [(int(c[0]), int(c[1])) for c in upper_centers] virtual_hash_data['lower_hash_centers'] = [(int(c[0]), int(c[1])) for c in lower_centers] virtual_hash_data['upper_hash_centers_with_conf'] = [(int(c[0]), int(c[1]), 0.5) for c in upper_centers] virtual_hash_data['lower_hash_centers_with_conf'] = [(int(c[0]), int(c[1]), 0.5) for c in lower_centers] virtual_hash_data['tracked_hash_marks'] = tracked_hash_marks virtual_hash_data['hash_detections_count'] = len(tracked_hash_marks) # Add hash validation info virtual_hash_data['hash_validation_info'] = { 'upper_hash_params': upper_hash_params, 'lower_hash_params': lower_hash_params, 'num_upper_centers': len(upper_centers), 'num_lower_centers': len(lower_centers), 'hash_stats': hash_tracker.get_stats(), 'validation_stats': hash_validator.get_stats() if hash_validator else {}, 'classification_method': 'virtual_line_distance' if (upper_virtual_params and lower_virtual_params) else 'frame_midpoint' } except Exception as e: print(f"Error in hash mark detection: {e}") # Return previous frame's parameters upper_hash_params, lower_hash_params = hash_tracker.get_hash_line_params() upper_centers, lower_centers = hash_tracker.get_hash_centers() tracked_hash_marks = hash_tracker.get_tracked_hash_marks() return upper_hash_params, lower_hash_params, upper_centers, lower_centers, tracked_hash_marks # ============================================================================= # NEW: NO-GO ZONE CHECK FOR HASH MARKS # ============================================================================= def is_point_in_no_go_zone(point, no_go_zone, debug=False): if not no_go_zone: return False, "none" x, y = point # Get buffer line equations m_upper, b_upper = no_go_zone['upper_buffer_line'] m_lower, b_lower = no_go_zone['lower_buffer_line'] # Calculate y values of buffer lines at this x coordinate y_upper_buffer = m_upper * x + b_upper y_lower_buffer = m_lower * x + b_lower # Check if point is in upper no-go zone (above upper buffer line) # In image coordinates, smaller y = higher in image if y < y_upper_buffer: # Point is ABOVE upper buffer line = in upper no-go zone if debug: print(f" Point ({x},{y}) in UPPER no-go zone: y={y:.1f} < buffer_y={y_upper_buffer:.1f}") return True, "upper" # Check if point is in lower no-go zone (below lower buffer line) if y > y_lower_buffer: # Point is BELOW lower buffer line = in lower no-go zone if debug: print(f" Point ({x},{y}) in LOWER no-go zone: y={y:.1f} > buffer_y={y_lower_buffer:.1f}") return True, "lower" # Point is between buffer lines = safe zone return False, "none" def draw_hash_points_with_tracking_info(frame, upper_centers, lower_centers, tracked_hash_marks, overlay_image=None): target = overlay_image if overlay_image is not None else frame # Draw upper hash points in CYAN with track IDs for center in upper_centers: if len(center) >= 2: x, y = int(center[0]), int(center[1]) # Find track info if available track_id = None for mark in tracked_hash_marks: mark_center = mark['center'] if int(mark_center[0]) == x and int(mark_center[1]) == y: track_id = mark['track_id'] break # Draw point if overlay_image is not None: cv2.circle(target, (x, y), 6, (255, 255, 0, 200), -1) # CYAN with alpha cv2.circle(target, (x, y), 8, (255, 255, 0, 200), 2) # CYAN outline with alpha else: cv2.circle(target, (x, y), 6, (255, 255, 0), -1) # CYAN cv2.circle(target, (x, y), 8, (255, 255, 0), 2) # CYAN outline # Draw track ID if available if track_id is not None: cv2.putText(target, f"T{track_id}", (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0, 255) if overlay_image is not None else (255, 255, 0), 1) # Draw lower hash points in MAGENTA with track IDs for center in lower_centers: if len(center) >= 2: x, y = int(center[0]), int(center[1]) # Find track info if available track_id = None for mark in tracked_hash_marks: mark_center = mark['center'] if int(mark_center[0]) == x and int(mark_center[1]) == y: track_id = mark['track_id'] break # Draw point if overlay_image is not None: cv2.circle(target, (x, y), 6, (255, 0, 255, 200), -1) # MAGENTA with alpha cv2.circle(target, (x, y), 8, (255, 0, 255, 200), 2) # MAGENTA outline with alpha else: cv2.circle(target, (x, y), 6, (255, 0, 255), -1) # MAGENTA cv2.circle(target, (x, y), 8, (255, 0, 255), 2) # MAGENTA outline # Draw track ID if available if track_id is not None: cv2.putText(target, f"T{track_id}", (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255, 255) if overlay_image is not None else (255, 0, 255), 1) # ============================================================================= # MAIN FUNCTION WITH MODIFIED SIDELINE ESTIMATION # ============================================================================= # ============================================================================= # ASYNC MODEL INFERENCE MANAGER # ============================================================================= class AsyncModelInference: def __init__(self, device): self.device = device self.streams = {} self.executor = None # Setup based on device if self.device == 'cuda' and torch.cuda.is_available(): # Create separate CUDA streams for different model types self.streams['player'] = torch.cuda.Stream() self.streams['number'] = torch.cuda.Stream() self.streams['hash'] = torch.cuda.Stream() self.streams['yard_line'] = torch.cuda.Stream(priority=-1) # Lower priority for yard lines if needed self.streams['classifier'] = torch.cuda.Stream() print("✅ Concurrent inference: Using CUDA streams") elif self.device == 'mps' or self.device == 'cpu': # Use ThreadPoolExecutor for CPU/MPS concurrency import concurrent.futures self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) print(f"✅ Concurrent inference: Using thread pool on {device}") else: print("⚠️ Concurrent inference: Sequential execution (device not supported)") def prepare_input(self, model, input_data): if hasattr(input_data, 'to'): # It's a tensor # Check if model and input are on same device try: model_device = next(model.parameters()).device if input_data.device != model_device: input_data = input_data.to(model_device) # Match dtype for CUDA half precision if self.device == 'cuda' and next(model.parameters()).dtype == torch.float16: input_data = input_data.half() except StopIteration: # Model has no parameters (unlikely) pass return input_data def run_concurrently(self, tasks): if self.device == 'cuda' and torch.cuda.is_available(): return self._run_with_cuda_streams(tasks) elif self.executor: return self._run_with_thread_pool(tasks) else: # Sequential fallback return self._run_sequentially(tasks) def _run_with_cuda_streams(self, tasks): """Execute using CUDA streams for true GPU concurrency.""" results = [None] * len(tasks) # Launch all tasks on their streams for i, task in enumerate(tasks): stream = self.streams.get(task['name'], torch.cuda.default_stream()) with torch.cuda.stream(stream): # Prepare input for device consistency frame = self.prepare_input(task['model'], task['frame']) # Run inference if task['name'] == 'classifier': with torch.no_grad(): results[i] = task['model'](frame) else: # YOLO inference results[i] = task['model'](frame, **task.get('kwargs', {})) # Synchronize all streams torch.cuda.synchronize() return results def _run_with_thread_pool(self, tasks): """Execute using thread pool for CPU/MPS concurrency.""" futures = [] for task in tasks: # Submit each task to thread pool future = self.executor.submit(self._run_single_task, task) futures.append((task, future)) # Collect results results = [] for task, future in futures: try: results.append(future.result(timeout=5.0)) # 5-second timeout except Exception as e: print(f"Warning: Async task {task['name']} failed: {e}") results.append(None) return results def _run_single_task(self, task): """Run a single inference task (for thread pool).""" # Prepare input for device consistency frame = self.prepare_input(task['model'], task['frame']) if task['name'] == 'classifier': with torch.no_grad(): return task['model'](frame) else: return task['model'](frame, **task.get('kwargs', {})) def _run_sequentially(self, tasks): """Sequential fallback with device preparation.""" results = [] for task in tasks: # Prepare input for device consistency frame = self.prepare_input(task['model'], task['frame']) if task['name'] == 'classifier': with torch.no_grad(): results.append(task['model'](frame)) else: results.append(task['model'](frame, **task.get('kwargs', {}))) return results def cleanup(self): """Clean up resources.""" if self.executor: self.executor.shutdown(wait=True) self.streams.clear() # ============================================================================= # MODIFIED: FIELD TEMPLATEMANAGER CLASS - ADD NEW METHOD # ============================================================================= # Replace the entire FieldTemplateManager class with this updated version # (Only the new method is added; rest remains identical) # ============================================================================= # CORRECTED: FIELD TEMPLATEMANAGER CLASS - FIXED DRAW METHOD # ============================================================================= # Replace the entire FieldTemplateManager class with this corrected version # (Only the draw_speed_acceleration_overlay method is fixed; rest remains identical) class FieldTemplateManager: def __init__(self, frame_width=640, frame_height=480): """ Initialize field template manager with pre-rendered base template. """ self.frame_width = frame_width self.frame_height = frame_height # Pre-render the base field template (without any highlights) self.base_template_cpu = self._create_base_template() # Store the highlighted field template self.highlighted_field_template = None # GPU blender for template self.gpu_blender = GPUBlender() # Statistics self.stats = { 'frames_processed': 0, 'yard_lines_highlighted': 0, 'hash_lines_detected': 0, 'virtual_lines_detected': 0 } # Field dimensions for coordinate mapping self.field_length = 120.0 # yards self.field_width = 53.3 # yards # Scale factors for converting yard coordinates to pixels self._calculate_scaling_factors() # NEW: Cache for last displayed speed/accel stats (to prevent blinking) self.last_speed_stats = None self.last_accel_stats = None def _create_base_template(self): """Create the base field template without any highlights using existing function.""" # Create empty field template (no yard line assignments) base_template = create_football_field_image( [], # Empty assignments for base template self.frame_width, self.frame_height ) return base_template def _calculate_scaling_factors(self): """Calculate accurate scaling factors to convert yard coordinates to pixel coordinates.""" # The template image created by create_football_field_image has: # - Total width: 130 yards (-5 to 125) # - Total height: 63.3 yards (-5 to 58.3) # - Playing field: 0-120 yards in x, 0-53.3 yards in y # Total template dimensions in yards (including padding) total_width_yards = 130.0 # -5 to 125 = 130 yards total_height_yards = 63.3 # -5 to 58.3 = 63.3 yards # Calculate scaling factors (pixels per yard) self.scale_x = self.frame_width / total_width_yards self.scale_y = self.frame_height / total_height_yards # Store the origin point in template coordinates # The point (-5, -5) in yard coordinates maps to pixel (0, 0) self.origin_x_yards = -5.0 self.origin_y_yards = -5.0 def _yard_to_pixel(self, yard_x, yard_y): # Convert yards to pixels relative to origin pixel_x = int((yard_x - self.origin_x_yards) * self.scale_x) pixel_y = int((yard_y - self.origin_y_yards) * self.scale_y) # Ensure within bounds pixel_x = max(0, min(self.frame_width - 1, pixel_x)) pixel_y = max(0, min(self.frame_height - 1, pixel_y)) return pixel_x, pixel_y def _get_yard_line_x_position(self, yard_num, side, is_intermediate=False): try: yard_num_int = int(yard_num) except (ValueError, TypeError): return None # Center line (50 yard line) if side == 'C' or yard_num_int == 50: return 60.0 # Center of the field (10 + 50) # Left side yard lines (offense perspective) if side == 'L': # Left side yard lines increase from left to right # Playing field starts at 10 yards, so add 10 to yard number return 10.0 + float(yard_num_int) # Right side yard lines (offense perspective) elif side == 'R': # Right side yard lines decrease from left to right # Playing field ends at 110 yards, so subtract from 110 return 110.0 - float(yard_num_int) # Unknown side - assume left side else: return 10.0 + float(yard_num_int) def _draw_yard_line_highlight(self, template, assignment, overlay_image=None): yard_num = assignment.get('yard_line_num') side = assignment.get('side', 'unknown') is_intermediate = assignment.get('is_intermediate', False) if yard_num is None: return # Get the X position in yard coordinates x_yard = self._get_yard_line_x_position(yard_num, side, is_intermediate) if x_yard is None: return # Ensure x_yard is within field bounds (0-120 yards) x_yard = max(0.0, min(120.0, x_yard)) # Get start and end points for the yard line start_x, start_y = self._yard_to_pixel(x_yard, 0) end_x, end_y = self._yard_to_pixel(x_yard, self.field_width) # Determine line properties based on assignment target = overlay_image if overlay_image is not None else template if is_intermediate: # Intermediate yard line (blue) - dashed if overlay_image is not None: color = (255, 0, 0, 200) # BLUE in BGRA with alpha else: color = (255, 0, 0) # BLUE in BGR thickness = 2 # Draw dashed line dash_length = 8 gap_length = 4 current_y = start_y while current_y < end_y: segment_end = min(current_y + dash_length, end_y) if segment_end > current_y: # Make sure we have something to draw cv2.line(target, (start_x, current_y), (start_x, segment_end), color, thickness, cv2.LINE_AA) current_y = segment_end + gap_length else: # Main yard line (red) - solid if overlay_image is not None: color = (0, 0, 255, 200) # RED in BGRA with alpha else: color = (0, 0, 255) # RED in BGR thickness = 3 # Draw solid line cv2.line(target, (start_x, start_y), (start_x, end_y), color, thickness, cv2.LINE_AA) def draw_speed_acceleration_overlay(self, frame, speed_stats, accel_stats, player_speed_tracker=None, position_mode='template', frame_index=0, debug=False): # Get update interval from tracker if available, else default to 6 update_interval = player_speed_tracker.stats_interval_frames if player_speed_tracker else 6 # Update cached stats only every update_interval frames if frame_index % update_interval == 0: self.last_speed_stats = speed_stats self.last_accel_stats = accel_stats # If no cached stats yet (e.g., first frames), skip drawing if self.last_speed_stats is None or self.last_accel_stats is None: if debug: print(f"Skipping stats draw - no cached stats yet (frame {frame_index})") return frame # Use cached values for drawing (always draw to prevent blinking) display_speed_stats = self.last_speed_stats display_accel_stats = self.last_accel_stats if debug: print(f"Drawing stats at frame {frame_index} - Speed: {display_speed_stats['yards_per_sec']} yd/s, " f"Accel: {display_accel_stats['yards_per_sec_sq']} yd/s² (mode: {position_mode})") # Get frame dimensions h, w = frame.shape[:2] # Create small overlay for stats (BGRA) stats_overlay = np.zeros((h, w, 4), dtype=np.uint8) # Background color (semi-transparent white) bg_color = (255, 255, 255, 180) # White with 70% opacity text_color = (0, 0, 0, 255) # Black with full opacity font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.6 thickness = 2 # SPEED DISPLAY (1 decimal place) speed_text = [ f"Speed: {display_speed_stats['mph']} mph", f" {display_speed_stats['yards_per_sec']} yd/s", f" {display_speed_stats['meters_per_sec']} m/s", f" {display_speed_stats['km_per_hour']} km/h" ] # ACCELERATION DISPLAY (1 decimal place) accel_text = [ f"Accel: {display_accel_stats['meters_per_sec_sq']} m/s2", f" {display_accel_stats['yards_per_sec_sq']} yd/s2", f" {display_accel_stats['feet_per_sec_sq']} ft/s2" ] # Calculate text dimensions properly text_height = cv2.getTextSize("Speed: 12.3 yd/s", font, font_scale, thickness)[0][1] line_spacing = 5 # Calculate total height needed (with proper padding) total_speed_height = len(speed_text) * (text_height + line_spacing) total_accel_height = len(accel_text) * (text_height + line_spacing) # Padding for the box padding_top = 10 # Extra padding at top to prevent cutoff padding_bottom = 5 padding_left = 10 padding_right = 10 # Position based on mode if position_mode == 'template': # Original: Top of template (above field) speed_y_start = 20 + padding_top # Start lower to accommodate padding speed_x_start = w // 4 # Centered-ish # Acceleration positioned to the right of speed box accel_y_start = speed_y_start # Same vertical position accel_x_start = speed_x_start + 220 + 20 # Right of speed box with 20px spacing # For template mode bottom, keep original positioning as fallback # but maintain the adjacent relationship if speed_y_start + total_speed_height > h // 2: # If would overlap field # Use bottom positioning like original accel_y_end = h - 20 - padding_bottom accel_y_start = accel_y_end - total_accel_height - padding_top accel_x_start = w // 4 # Same x as speed for alignment else: # 'video' # Speed box: Top-right of video frame speed_y_start = 20 + padding_top # Start lower to accommodate padding speed_x_start = w - 220 # Right-aligned-ish # Acceleration box: Adjacent to speed (to the left) accel_y_start = speed_y_start # Same vertical position accel_x_start = speed_x_start - 220 - 20 # Left of speed box with 20px spacing # Draw speed background rect (with proper padding on all sides) cv2.rectangle(stats_overlay, (speed_x_start - padding_left, speed_y_start - padding_top), (speed_x_start + 200 + padding_right, speed_y_start + total_speed_height + padding_bottom), bg_color, -1) # Draw speed text lines (with proper vertical spacing) for i, line in enumerate(speed_text): y_pos = speed_y_start + (i * (text_height + line_spacing)) + text_height # Add text_height to start below top cv2.putText(stats_overlay, line, (speed_x_start, y_pos), font, font_scale, text_color, thickness) # Draw accel background rect (with proper padding on all sides) cv2.rectangle(stats_overlay, (accel_x_start - padding_left, accel_y_start - padding_top), (accel_x_start + 200 + padding_right, accel_y_start + total_accel_height + padding_bottom), bg_color, -1) # Draw accel text lines (with proper vertical spacing) for i, line in enumerate(accel_text): y_pos = accel_y_start + (i * (text_height + line_spacing)) + text_height # Add text_height to start below top cv2.putText(stats_overlay, line, (accel_x_start, y_pos), font, font_scale, text_color, thickness) # Blend stats overlay onto frame if self.gpu_blender.gpu_available: self.gpu_blender.upload_frame_to_gpu(frame) self.gpu_blender.upload_overlay_to_gpu(stats_overlay) gpu_result = self.gpu_blender.blend_gpu() if gpu_result is not None: frame = self.gpu_blender.download_from_gpu(gpu_result) else: frame = self.gpu_blender.blend_cpu(frame, stats_overlay) else: frame = self.gpu_blender.blend_cpu(frame, stats_overlay) return frame def update_field_template(self, yard_line_assignments, virtual_hash_data=None, debug=False): self.stats['frames_processed'] += 1 # Start with a copy of the pre-rendered base template field_template = self.base_template_cpu.copy() # Count detected lines if virtual_hash_data: if virtual_hash_data.get('upper_line_params'): self.stats['virtual_lines_detected'] += 1 if virtual_hash_data.get('lower_line_params'): self.stats['virtual_lines_detected'] += 1 if virtual_hash_data.get('upper_hash_line_params'): self.stats['hash_lines_detected'] += 1 if virtual_hash_data.get('lower_hash_line_params'): self.stats['hash_lines_detected'] += 1 # Highlight yard lines if any are detected if yard_line_assignments: self.stats['yard_lines_highlighted'] += len(yard_line_assignments) # Create overlay for template highlights template_overlay = np.zeros((self.frame_height, self.frame_width, 4), dtype=np.uint8) # Draw each yard line highlight on overlay for assignment in yard_line_assignments: self._draw_yard_line_highlight(field_template, assignment, overlay_image=template_overlay) # Blend overlay with template if self.gpu_blender.gpu_available: self.gpu_blender.upload_frame_to_gpu(field_template) self.gpu_blender.upload_overlay_to_gpu(template_overlay) gpu_result = self.gpu_blender.blend_gpu() if gpu_result is not None: field_template = self.gpu_blender.download_from_gpu(gpu_result) else: field_template = self.gpu_blender.blend_cpu(field_template, template_overlay) else: field_template = self.gpu_blender.blend_cpu(field_template, template_overlay) if debug and len(yard_line_assignments) > 0: print(f"Highlighted {len(yard_line_assignments)} yard lines on field template") self.highlighted_field_template = field_template return field_template def draw_players_on_template(self, template, player_positions, top_player_id=None, debug=False): if not player_positions or len(player_positions) == 0: return template if debug and len(player_positions) > 0: print(f"\nDEBUG: Drawing {len(player_positions)} players on template") players_drawn = 0 # Create overlay for player markers (BGRA) player_overlay = np.zeros((self.frame_height, self.frame_width, 4), dtype=np.uint8) # SINGLE COLOR for all players on template template_player_color = (0, 255, 0) # Green in BGR for all players (default) template_referee_color = (0, 0, 255) # Red in BGR for referees template_top_player_color = (0, 255, 255) # YELLOW in BGR for top player ring_color = (255, 0, 255) # MAGENTA ring for top player for player in player_positions: field_coords = player.get('field_coords') if field_coords is None: continue x_field, y_field = field_coords if debug: print(f" Player field coords: ({x_field:.1f}, {y_field:.1f}) yards") # Check if player is in playing field (not in end zones) if x_field < 10.0 or x_field > 110.0: if debug: print(f" Skipped - in end zone (x={x_field:.1f})") continue # Ensure within field bounds if x_field < 0.0 or x_field > 120.0 or y_field < 0.0 or y_field > 53.3: if debug: print(f" Skipped - outside field bounds") continue # Convert to pixel coordinates using the template's scaling x_pixel, y_pixel = self._yard_to_pixel(x_field, y_field) if debug: print(f" Pixel coords: ({x_pixel}, {y_pixel}) pixels") player_type = player.get('player_type', 'player') track_id = player.get('track_id', -1) # Determine color: YELLOW if this is top player, else default if player_type == 'refree': player_color_bgr = template_referee_color # Red for referees else: player_color_bgr = template_top_player_color if track_id == top_player_id else template_player_color # Alpha versions for overlay inner_color = (*player_color_bgr, 200) outer_color = (255, 255, 255, 200) ring_color_alpha = (*ring_color, 200) # MAGENTA ring with alpha for top player # ENHANCED: Increased circle sizes for top player by 30-35% if track_id == top_player_id: # Top player: increased size by ~33% (4->6, 6->8) + RING marker_size = 8 # Increased from 4 (50% increase) outer_size = 12 # Increased from 6 (33% increase) ring_size = 13 # Ring radius (larger than outer circle) ring_thickness = 3 # Thickness of the ring else: # Regular players: original sizes marker_size = 4 outer_size = 6 # Check bounds to avoid drawing outside image max_size = max(outer_size, ring_size if track_id == top_player_id else outer_size) if (max_size < x_pixel < self.frame_width - max_size and max_size < y_pixel < self.frame_height - max_size): # DRAW RING AROUND TOP PLAYER (only for top player) if track_id == top_player_id: # Draw magenta ring around the player cv2.circle(player_overlay, (x_pixel, y_pixel), ring_size, ring_color_alpha, ring_thickness) # Draw outer white circle cv2.circle(player_overlay, (x_pixel, y_pixel), outer_size, outer_color, -1) # Draw inner colored circle (yellow if top) cv2.circle(player_overlay, (x_pixel, y_pixel), marker_size, inner_color, -1) # Draw ID label ONLY for top player with track_id != -1 if track_id != -1 and track_id == top_player_id: label_text = f"ID:{track_id}" is_top_player = True # Use normal size for top player (twice as large as non-top would be) font_scale = 0.8 thickness = 2 (text_width, text_height), baseline = cv2.getTextSize( label_text, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness ) label_x = x_pixel + 10 label_y = y_pixel - 10 # Background rect (small, use player color - yellow if top) padding = 4 cv2.rectangle(player_overlay, (label_x - padding, label_y - text_height - padding), (label_x + text_width + padding, label_y + padding), inner_color, -1) # Text cv2.putText(player_overlay, label_text, (label_x, label_y), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255, 255), thickness) players_drawn += 1 if debug: if track_id == top_player_id: print(f" Drawn TOP PLAYER at pixel ({x_pixel}, {y_pixel}) - Size: {marker_size}/{outer_size}, Ring: {ring_size}") else: print(f" Drawn at pixel ({x_pixel}, {y_pixel}) - Size: {marker_size}/{outer_size}") # Blend player overlay with template if players_drawn > 0: if self.gpu_blender.gpu_available: self.gpu_blender.upload_frame_to_gpu(template) self.gpu_blender.upload_overlay_to_gpu(player_overlay) gpu_result = self.gpu_blender.blend_gpu() if gpu_result is not None: template = self.gpu_blender.download_from_gpu(gpu_result) else: template = self.gpu_blender.blend_cpu(template, player_overlay) else: template = self.gpu_blender.blend_cpu(template, player_overlay) if debug: print(f" Total players drawn: {players_drawn}") return template def reset_stats(self): """Reset field template statistics.""" self.stats = { 'frames_processed': 0, 'yard_lines_highlighted': 0, 'hash_lines_detected': 0, 'virtual_lines_detected': 0 } def draw_sidelines_on_overlay(target, upper_line, lower_line, width, height, overlay_image=None): """Draw validated sidelines (yellow lines) on overlay.""" if upper_line: x_left = 0 x_right = width - 1 m = upper_line.get('slope') b = upper_line.get('intercept') if m is not None and b is not None: y_left = m * x_left + b y_right = m * x_right + b y_left = max(0, min(height - 1, y_left)) y_right = max(0, min(height - 1, y_right)) if overlay_image is not None: cv2.line(target, (int(x_left), int(y_left)), (int(x_right), int(y_right)), (0, 255, 255, 200), 3) else: cv2.line(target, (int(x_left), int(y_left)), (int(x_right), int(y_right)), (0, 255, 255), 3) if lower_line: x_left = 0 x_right = width - 1 m = lower_line.get('slope') b = lower_line.get('intercept') if m is not None and b is not None: y_left = m * x_left + b y_right = m * x_right + b y_left = max(0, min(height - 1, y_left)) y_right = max(0, min(height - 1, y_right)) if overlay_image is not None: cv2.line(target, (int(x_left), int(y_left)), (int(x_right), int(y_right)), (0, 255, 255, 200), 3) else: cv2.line(target, (int(x_left), int(y_left)), (int(x_right), int(y_right)), (0, 255, 255), 3) def draw_yard_lines_and_assignments(target, yard_line_tracks, yard_line_assignments, overlay_image=None): """Draw yard lines and assignments on overlay.""" for track in yard_line_tracks: if 'extended_line' in track: extended_line = track['extended_line'] confidence = track['confidence'] track_id = track['id'] if len(extended_line) == 4: # Determine if this track is assigned as red or blue is_assigned = False is_intermediate = False assigned_yard = '?' for assignment in yard_line_assignments: if 'line_data' in assignment: assn_extended_line = assignment['line_data'].get('extended_line', []) if len(assn_extended_line) == 4: assn_center_x = (assn_extended_line[0] + assn_extended_line[2]) // 2 assn_center_y = (assn_extended_line[1] + assn_extended_line[3]) // 2 center_x = (extended_line[0] + extended_line[2]) // 2 center_y = (extended_line[1] + extended_line[3]) // 2 if abs(center_x - assn_center_x) < 20 and abs(center_y - assn_center_y) < 20: is_assigned = True assigned_yard = assignment.get('yard_line_num', '?') is_intermediate = assignment.get('is_intermediate', False) break # Draw the line with appropriate color if is_assigned: if is_intermediate: if overlay_image is not None: line_color = (255, 0, 0, 200) # BLUE for intermediate lines with alpha else: line_color = (255, 0, 0) # BLUE for intermediate lines line_thickness = 2 else: if overlay_image is not None: line_color = (0, 0, 255, 200) # RED for main lines with alpha else: line_color = (0, 0, 255) # RED for main lines line_thickness = 3 else: if overlay_image is not None: line_color = (255, 255, 0, 200) # YELLOW for unassigned with alpha else: line_color = (255, 255, 0) # YELLOW for unassigned line_thickness = 1 cv2.line(target, (extended_line[0], extended_line[1]), (extended_line[2], extended_line[3]), line_color, line_thickness) center_x = (extended_line[0] + extended_line[2]) // 2 center_y = (extended_line[1] + extended_line[3]) // 2 # ============================================================================= # MODIFIED MAIN FUNCTION WITH RELIABLE NO-GO ZONE DISPLAY # ============================================================================= def adjust_coordinates_for_template(x_field, y_field): # The template shows only the playing field (10-110 yards in x direction) # Original x: 0-120 (0-10: left endzone, 10-110: playing field, 110-120: right endzone) # Template x: 10-110 (only the playing field) # For x coordinate: clip to playing field area x_adjusted = max(10.0, min(110.0, x_field)) # For y coordinate: no change needed (0-53.3) y_adjusted = y_field return x_adjusted, y_adjusted def draw_live_graph(csv_path, frame_width, frame_height): """Draw live graph with adjustable height for full right column.""" import pandas as pd import matplotlib matplotlib.use('Agg') # Use non-interactive backend import matplotlib.pyplot as plt from io import BytesIO import numpy as np # Create figure with proper sizing for the full right column # frame_height parameter now represents TOTAL height (2*video_height) fig_width = frame_width / 100 # Convert pixels to inches (approx) fig_height = frame_height / 100 fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=100) try: # Read CSV data df = pd.read_csv(csv_path) if len(df) > 0: times = df['time_seconds'].values # CONVERT SPEED from yards/sec to mph: mph = (yards/sec * 3600) / 1760 speeds_yds_sec = df['speed_yds_sec'].values speeds_mph = (speeds_yds_sec * 3600) / 1760 # Convert to mph # CONVERT ACCELERATION from yards/sec² to m/s²: m/s² = yards/sec² * 0.9144 accels_yds_sec2 = df['accel_yds_sec2'].values accels_m_s2 = accels_yds_sec2 * 0.9144 # Convert to m/s² # Plot speed (in mph) and acceleration (in m/s²) ax.plot(times, speeds_mph, label='Speed (mph)', color='blue', linewidth=2, marker='o', markersize=3) ax.plot(times, accels_m_s2, label='Acceleration (m/s²)', color='red', linewidth=2, marker='x', markersize=3) # Labels and formatting with updated units ax.set_xlabel('Time (seconds)', fontsize=10) ax.set_ylabel('Value', fontsize=10) ax.set_title('Player Speed & Acceleration', fontsize=12, fontweight='bold') ax.legend(fontsize=9) ax.grid(True, alpha=0.3) # Set reasonable limits if len(times) > 0: ax.set_xlim(0, max(times) * 1.1) # Set y-axis limits based on converted data if len(speeds_mph) > 0 or len(accels_m_s2) > 0: all_data = list(speeds_mph) + list(accels_m_s2) y_min = min(all_data) * 0.9 if min(all_data) < 0 else 0 y_max = max(all_data) * 1.1 ax.set_ylim(y_min, y_max) # Tight layout to use space efficiently plt.tight_layout() except Exception as e: # If no data or error, create empty plot ax.text(0.5, 0.5, 'Waiting for data...', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=14) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xticks([]) ax.set_yticks([]) ax.set_title('Player Speed & Acceleration', fontsize=12, fontweight='bold') # Convert plot to image buf = BytesIO() plt.savefig(buf, format='png', dpi=100, bbox_inches='tight') plt.close(fig) buf.seek(0) # Convert to OpenCV format from PIL import Image img = Image.open(buf) graph_img = np.array(img) graph_img = cv2.cvtColor(graph_img, cv2.COLOR_RGB2BGR) # Resize to match the requested dimensions (for full right column) graph_img = cv2.resize(graph_img, (frame_width, frame_height)) return graph_img def write_metadata_csv(csv_path, frame_index, time_seconds, smoothed_position, smoothed_speed, smoothed_accel, raw_position=None, kalman_velocity=None, debug=False): import csv # Calculate Kalman speed prediction (magnitude) from velocity kalman_speed_prediction = 0.0 if kalman_velocity is not None: vx, vy = kalman_velocity kalman_speed_prediction = np.sqrt(vx**2 + vy**2) # Extract position values x_smoothed, y_smoothed = smoothed_position x_raw, y_raw = raw_position if raw_position is not None else (x_smoothed, y_smoothed) with open(csv_path, mode='a', newline='') as file: writer = csv.writer(file) # NEW FORMAT: time_seconds, x_yards, y_yards, speed_yds_sec, accel_yds_sec2, # actual_x_before_ema, actual_y_before_ema, kalman_speed_prediction writer.writerow([ round(time_seconds, 2), # time_seconds round(x_smoothed, 2), # x_yards (smoothed) round(y_smoothed, 2), # y_yards (smoothed) round(smoothed_speed, 2), # speed_yds_sec (smoothed) round(smoothed_accel, 2), # accel_yds_sec2 (smoothed) round(x_raw, 2), # actual_x_before_ema round(y_raw, 2), # actual_y_before_ema round(kalman_speed_prediction, 2) # kalman_speed_prediction ]) if debug: print(f"CSV Row {frame_index}: Time={time_seconds:.2f}s, " f"Smoothed=({x_smoothed:.2f},{y_smoothed:.2f}), " f"Raw=({x_raw:.2f},{y_raw:.2f}), " f"Speed={smoothed_speed:.2f} yd/s, " f"Kalman Speed={kalman_speed_prediction:.2f} yd/s") def process_video(input_video_path, output_video_path, progress_callback=None, target_player_id=None): # Configuration DETECTION_CONFIDENCE_THRESHOLD = 0.5 CLASSIFICATION_CONFIDENCE_THRESHOLD = 0.95 YARD_LINE_CONFIDENCE_THRESHOLD = 0.5 # Tracking parameters TRACKING_MAX_DISTANCE = 50 TRACKING_MAX_AGE = 15 YARD_LINE_TRACKING_MAX_AGE = 15 YARD_LINE_TRACKING_MAX_DISTANCE = 100 # Hash tracking parameters HASH_TRACKING_MAX_DISTANCE = 50 HASH_TRACKING_MAX_AGE = 15 HASH_CONFIDENCE_THRESHOLD = 0.25 HASH_IOU_THRESHOLD = 0.45 # No-go zone parameters for 3D perspective UPPER_BUFFER_PERCENTAGE = 0.075 LOWER_BUFFER_PERCENTAGE = 0.15 # NEW: Flag to determine if we should process limited frames (no target player) process_limited_frames = (target_player_id is None) MAX_FRAMES_NO_TARGET = 10 # Process only 10 frames when no target player frame_snippet_path = None # Path to save the 10th frame snippet # Get current directory script_dir = os.path.dirname(os.path.abspath(__file__)) # File paths - using relative paths number_detector_weights = os.path.join(script_dir, 'models', 'weights_1.pt') hash_detector_weights = os.path.join(script_dir, 'models', 'YOLO.pt') classifier_weights = os.path.join(script_dir, 'models', 'best_model.pt') player_detector_weights = os.path.join(script_dir, 'models', 'player.pt') # Check if player detector file exists if not os.path.exists(player_detector_weights): print(f"Warning: Player detector weights not found: {player_detector_weights}") print("Player detection will be disabled.") player_detector = None else: player_detector = None # Will be loaded later with GPU support # Check if files exist if not os.path.exists(number_detector_weights): raise FileNotFoundError(f"Number detector weights not found: {number_detector_weights}") # Try to load hash detector if os.path.exists(hash_detector_weights): try: hash_detector = YOLO(hash_detector_weights) print(f"Hash detector loaded successfully") except Exception as e: print(f"Error loading hash detector: {e}") hash_detector = None else: print(f"Warning: Hash detector weights not found: {hash_detector_weights}") print("Hash line detection will be disabled.") hash_detector = None if not os.path.exists(classifier_weights): raise FileNotFoundError(f"Classifier weights not found: {classifier_weights}") # Load models print(f"Loading models on device: {DEVICE}") # Load YOLO models with explicit device number_detector = YOLO(number_detector_weights) # Move YOLO model to device number_detector.to(DEVICE) print(f"✅ Number detector loaded on {DEVICE}") # Load player detector if available if os.path.exists(player_detector_weights): try: player_detector = YOLO(player_detector_weights) player_detector.to(DEVICE) print(f"✅ Player detector loaded on {DEVICE}") except Exception as e: print(f"Error loading player detector: {e}") player_detector = None else: player_detector = None print(f"Warning: Player detector weights not found, player detection disabled") # Load hash detector if available if os.path.exists(hash_detector_weights): try: hash_detector = YOLO(hash_detector_weights) hash_detector.to(DEVICE) print(f"✅ Hash detector loaded on {DEVICE}") except Exception as e: print(f"Error loading hash detector: {e}") hash_detector = None else: print(f"Warning: Hash detector weights not found") hash_detector = None # Load classifier with GPU support classifier, num_classes, image_size = load_classifier(classifier_weights) print(f"✅ All models loaded successfully on {DEVICE}") # Initialize trackers number_tracker = EnhancedNumberTracker( max_age=TRACKING_MAX_AGE, max_distance=TRACKING_MAX_DISTANCE, min_confidence=DETECTION_CONFIDENCE_THRESHOLD ) yard_line_tracker = YardLineTracker( max_age=YARD_LINE_TRACKING_MAX_AGE, max_distance=YARD_LINE_TRACKING_MAX_DISTANCE, min_confidence=YARD_LINE_CONFIDENCE_THRESHOLD ) field_tracker = FieldTemplateTracker(max_history=10) # Player detection counters person_count = 0 refree_count = 0 total_on_field_players = 0 total_bench_players = 0 total_player_crops_saved = 0 total_referee_crops_saved = 0 # Create player folders players_dir, referees_dir = create_player_folders() # Initialize virtual line verifier line_verifier = SimpleVirtualLineVerifier(slope_change_threshold=0.15) # Initialize sideline verifier sideline_verifier = SidelineVerifier(slope_change_threshold=0.2) # Initialize sideline position validator with 3D perspective sideline_validator = SidelinePositionValidator3D( upper_buffer_percentage=UPPER_BUFFER_PERCENTAGE, lower_buffer_percentage=LOWER_BUFFER_PERCENTAGE ) # Initialize hash line tracker hash_tracker = HashLineTracker( max_age=HASH_TRACKING_MAX_AGE, max_distance=HASH_TRACKING_MAX_DISTANCE, min_confidence=HASH_CONFIDENCE_THRESHOLD ) # Initialize Async Inference Manager async_inferencer = AsyncModelInference(DEVICE) # Initialize GPU Blender for main frame gpu_blender = GPUBlender() # Class names for digits class_names = {0: "10", 1: "20", 2: "30", 3: "40", 4: "50"} # Transform for classifier transform = transforms.Compose([ transforms.Resize((image_size, image_size)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Get YOLO class indices names = number_detector.names if hasattr(number_detector, 'names') else {} number_class_index = None yard_line_class_index = None if isinstance(names, dict): for k, v in names.items(): if v == 'number': number_class_index = k elif v == 'yard_line': yard_line_class_index = k elif isinstance(names, (list, tuple)): try: number_class_index = names.index('number') except ValueError: number_class_index = None try: yard_line_class_index = names.index('yard_line') except ValueError: yard_line_class_index = None if number_class_index is None: number_class_index = 0 if yard_line_class_index is None: yard_line_class_index = 1 # Open input video cap = cv2.VideoCapture(input_video_path) if not cap.isOpened(): raise RuntimeError(f"Cannot open video: {input_video_path}") fps = int(cap.get(cv2.CAP_PROP_FPS)) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Setup video writer for combined output (only if we're processing full video) if not process_limited_frames: # Setup video writer for DOUBLE HEIGHT layout combined_width = width * 2 combined_height = height * 2 # Double the height fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_video_path, fourcc, fps, (combined_width, combined_height)) if not out.isOpened(): print("Warning: Could not create output video writer") out = None else: out = None # Initialize Field Template Manager WITH VIDEO DIMENSIONS field_template_manager = FieldTemplateManager( frame_width=width, frame_height=height ) # Initialize counters processed_frames = 0 total_number_detections = 0 total_yard_line_detections = 0 total_assignments = 0 total_hash_detections = 0 total_virtual_line_detections = 0 # Line recovery counters line_recovery_stats = { 'upper_line_recovered': 0, 'lower_line_recovered': 0, 'upper_hash_recovered': 0, 'lower_hash_recovered': 0 } # Previous frame virtual line parameters for fallback prev_upper_line_params = None prev_lower_line_params = None # Sideline adjustment counters sideline_adjustments = [] # No-go zone display tracking no_go_zone_display_frames = 0 no_go_zone_missing_frames = 0 virtual_line_frames = 0 # Player speed tracker - UPDATED to use 10fps interval (6 frames at 60fps) player_speed_tracker = PlayerStatsTracker( history_window=5, fps=fps, stats_interval_frames=6 # 10fps at 60fps = every 6 frames ) # ===================================================== # UPDATED: USE PROVIDED TARGET PLAYER ID OR NONE # ===================================================== user_selected_id = target_player_id # Use provided ID or None frame_index = 0 # Frame counter (0-indexed) # NEW: Metadata CSV setup (only if target player is provided) outputs_dir = os.path.join(script_dir, "outputs") os.makedirs(outputs_dir, exist_ok=True) if user_selected_id is not None: metadata_csv_path = os.path.join(outputs_dir, "metadata.csv") # Overwrite/create CSV with headers import csv with open(metadata_csv_path, mode='w', newline='') as file: writer = csv.writer(file) # NEW HEADER FORMAT writer.writerow([ "time_seconds", "x_yards", # Smoothed x position "y_yards", # Smoothed y position "speed_yds_sec", # Smoothed speed "accel_yds_sec2", # Smoothed acceleration "actual_x_before_ema", # Raw x before EMA "actual_y_before_ema", # Raw y before EMA "kalman_speed_prediction" # Kalman speed prediction (magnitude) ]) else: metadata_csv_path = None # Counter for seconds (starts from 1) metadata_second_counter = 1 # Store the 10th frame for snippet tenth_frame_snippet = None try: # ===================================================== # SINGLE PASS PROCESSING WITH TARGET PLAYER ID OR LIMITED FRAMES # ===================================================== print("\n" + "="*80) if user_selected_id is not None: print(f"SINGLE PASS: Processing video with target player ID: {user_selected_id}") else: print(f"LIMITED PASS: Processing only {MAX_FRAMES_NO_TARGET} frames (no target player)") print("="*80) while True: # NEW: Check if we should stop processing (limited frames mode) if process_limited_frames and processed_frames >= MAX_FRAMES_NO_TARGET: print(f"Reached {MAX_FRAMES_NO_TARGET} frames in limited mode. Stopping.") break ret, frame = cap.read() if not ret: break processed_frames += 1 frame_index += 1 # Increment frame counter (starts at 1) # Update progress if callback provided if progress_callback and total_frames > 0: if process_limited_frames: # Adjust progress calculation for limited frames progress_percent = min(100, (processed_frames / MAX_FRAMES_NO_TARGET) * 100) else: progress_percent = (processed_frames / total_frames) * 100 progress_callback(progress_percent, f"Processing frame {processed_frames}/{MAX_FRAMES_NO_TARGET if process_limited_frames else total_frames}") # Periodic GPU memory cleanup (every 50 frames) if DEVICE == 'cuda' and frame_index % 50 == 0: torch.cuda.empty_cache() # Create output frame for left side (original detection) left_frame = frame.copy() gpu_frame_available = gpu_blender.upload_frame_to_gpu(left_frame) # Initialize no_go_zone_data to None for this frame no_go_zone_data = None # =========================================================== # STEP 1-4: CONCURRENT MODEL INFERENCES # =========================================================== yard_line_detections_raw = [] number_detections = [] virtual_hash_data = None player_results = None hash_results = None # Group independent model inferences that don't depend on each other's results inference_tasks = [] inference_tasks.append({ 'name': 'number', 'model': number_detector, 'frame': frame, 'kwargs': {'verbose': False} }) # Task 3: Player detection (if available) if player_detector is not None: inference_tasks.append({ 'name': 'player', 'model': player_detector, 'frame': frame, 'kwargs': {'verbose': False} }) # Task 4: Hash detection (if available) if hash_detector is not None: inference_tasks.append({ 'name': 'hash', 'model': hash_detector, 'frame': frame, 'kwargs': {'verbose': False, 'conf': HASH_CONFIDENCE_THRESHOLD, 'iou': HASH_IOU_THRESHOLD} }) # Run all inferences concurrently try: all_results = async_inferencer.run_concurrently(inference_tasks) # Parse results in order result_index = 0 # 1. Number detection results (for virtual lines and yard line numbers) if result_index < len(all_results) and all_results[result_index] is not None: number_results = all_results[result_index] result_index += 1 # 2. Player detection results if player_detector is not None and result_index < len(all_results): player_results = all_results[result_index] result_index += 1 # 3. Hash detection results if hash_detector is not None and result_index < len(all_results): hash_results = all_results[result_index] result_index += 1 except Exception as e: print(f"Error in concurrent inference: {e}") # Fallback to sequential processing yard_line_detections_raw = [] number_detections = [] # =========================================================== # STEP 1B: YARD LINE DETECTION (Sequential - custom processing) # =========================================================== try: yard_line_detections_raw = yolo_detect_yard_lines_frame( frame, number_detector, YARD_LINE_CONFIDENCE_THRESHOLD ) total_yard_line_detections += len(yard_line_detections_raw) except Exception as e: print(f"Error in yard line detection: {e}") yard_line_detections_raw = [] # Update yard line tracker yard_line_tracker.update(yard_line_detections_raw) yard_line_tracks = yard_line_tracker.get_tracks() # =========================================================== # STEP 2: NUMBER DETECTION WITH SIMPLIFIED CLASSIFICATION (Using async results) # =========================================================== detections_for_tracking = [] number_detections = [] upper_numbers = [] lower_numbers = [] # Classifier tasks for batch processing classifier_tasks = [] classifier_crops = [] # Store corresponding crop data try: # Use the number results from concurrent inference if 'number_results' in locals() and number_results is not None: boxes = number_results[0].boxes if hasattr(number_results, '__len__') and len(number_results) > 0 else None if boxes is not None: for box in boxes: cls = int(box.cls[0]) if cls == number_class_index: confidence = float(box.conf[0]) if confidence < DETECTION_CONFIDENCE_THRESHOLD: continue x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) x1, y1 = max(0, x1), max(0, y1) x2 = min(frame.shape[1], x2) y2 = min(frame.shape[0], y2) crop = frame[y1:y2, x1:x2] if crop.size == 0: continue try: pil_img = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)) input_tensor = transform(pil_img).unsqueeze(0) # Instead of immediate classification, prepare task for batch processing classifier_tasks.append({ 'name': 'classifier', 'model': classifier, 'frame': input_tensor }) classifier_crops.append({ 'crop': crop, 'det_conf': confidence, 'coords': (x1, y1, x2, y2), 'is_upper': (y1 + y2) / 2 < height // 2, 'index': len(classifier_tasks) - 1 }) except Exception as e: continue except Exception as e: print(f"Error in number detection: {e}") number_detections = [] # =========================================================== # STEP 2B: BATCH CLASSIFIER INFERENCE # =========================================================== if classifier_tasks: try: classifier_results = async_inferencer.run_concurrently(classifier_tasks) # Process all classifier results for crop_data in classifier_crops: idx = crop_data['index'] if idx < len(classifier_results) and classifier_results[idx] is not None: outputs = classifier_results[idx] probabilities = torch.softmax(outputs, dim=1) # Convert back to CPU for processing if needed if DEVICE != 'cpu': probabilities = probabilities.cpu() topk_probs, topk_indices = torch.topk(probabilities, 3, dim=1) top_probs = topk_probs[0].tolist() top_indices = topk_indices[0].tolist() selected_digit = top_indices[0] selected_class_name = class_names.get(selected_digit, f"Class_{selected_digit}") selected_cls_conf = top_probs[0] other_predictions = [] for i in range(1, 3): pred_idx = top_indices[i] pred_class = class_names.get(pred_idx, f"Class_{pred_idx}") pred_conf = top_probs[i] other_predictions.append((pred_class, pred_conf)) total_number_detections += 1 passes_filter = selected_cls_conf >= CLASSIFICATION_CONFIDENCE_THRESHOLD # Classify as upper or lower based on position x1, y1, x2, y2 = crop_data['coords'] center_y = (y1 + y2) / 2 is_upper = crop_data['is_upper'] if is_upper: upper_numbers.append(selected_class_name) else: lower_numbers.append(selected_class_name) detection = { 'bbox': (x1, y1, x2, y2), 'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2, 'class_name': selected_class_name, 'det_conf': crop_data['det_conf'], 'cls_conf': selected_cls_conf, 'passes_filter': passes_filter, 'other_predictions': other_predictions, 'top3_predictions': [(selected_class_name, selected_cls_conf)] + other_predictions, 'is_upper': is_upper, 'center_y': center_y } detections_for_tracking.append(detection) number_detections.append(detection) except Exception as e: print(f"Error in batch classifier inference: {e}") # Update number tracker try: number_tracker.update(detections_for_tracking) except Exception as e: print(f"Error in number tracking: {e}") # =========================================================== # STEP 3: COMPLETE VIRTUAL LINE DETECTION WITH ASYNC RESULTS # =========================================================== virtual_hash_data = None try: # CRITICAL: Use the existing function with proper parameters # We'll pass the number_results we got from async inference if 'number_results' in locals() and number_results is not None: img_height, img_width = frame.shape[:2] mid_y = img_height // 2 upper_centers = [] lower_centers = [] boxes = number_results[0].boxes if hasattr(number_results, '__len__') and len(number_results) > 0 else None if boxes is not None: for box in boxes: cls = int(box.cls[0]) if cls != number_class_index: continue x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 if center_y < mid_y: upper_centers.append((center_x, center_y)) else: lower_centers.append((center_x, center_y)) # Fit lines to the centers upper_line_params = None lower_line_params = None if len(upper_centers) >= 2: try: upper_line_params = fit_line_with_robust_method( upper_centers, width, height, line_type='virtual_top', debug=False ) except Exception as e: print(f"Upper line fitting error: {e}") if len(lower_centers) >= 2: try: lower_line_params = fit_line_with_robust_method( lower_centers, width, height, line_type='virtual_bottom', debug=False ) except Exception as e: print(f"Lower line fitting error: {e}") # Create initial virtual_hash_data virtual_hash_data = { 'upper_centers': upper_centers, 'lower_centers': lower_centers, 'upper_hash_centers': [], 'lower_hash_centers': [], 'upper_hash_centers_with_conf': [], 'lower_hash_centers_with_conf': [], 'upper_hash_bboxes': [], 'lower_hash_bboxes': [], 'upper_line_params': upper_line_params, 'lower_line_params': lower_line_params, 'upper_hash_line_params': None, 'lower_hash_line_params': None, 'hash_detections_count': 0, # Legacy keys 'upper_line': upper_line_params, 'lower_line': lower_line_params, 'upper_hash_line': None, 'lower_hash_line': None, } # Apply enhanced validation try: validated_virtual_hash_data, validation_summary = enhanced_line_fitting_with_validation( virtual_hash_data, width, height, debug=False ) # Merge validated data virtual_hash_data.update(validated_virtual_hash_data) except Exception as e: print(f"Enhanced validation failed: {e}") # Apply line verifier if line_verifier is not None: virtual_hash_data = line_verifier.verify_and_fill_missing_lines( virtual_hash_data, width, height, debug=False ) else: # If no number results, create empty virtual_hash_data virtual_hash_data = { 'upper_line_params': prev_upper_line_params, 'lower_line_params': prev_lower_line_params, 'upper_hash_line_params': None, 'lower_hash_line_params': None, 'hash_detections_count': 0, 'tracked_hash_marks': [], 'verification_info': {'recovery_used': True}, 'upper_line': prev_upper_line_params, 'lower_line': prev_lower_line_params, 'upper_hash_line': None, 'lower_hash_line': None, 'upper_centers': [], 'lower_centers': [], 'upper_hash_centers': [], 'lower_hash_centers': [], 'upper_hash_centers_with_conf': [], 'lower_hash_centers_with_conf': [], 'upper_hash_bboxes': [], 'lower_hash_bboxes': [] } # Store current frame's parameters for next frame if virtual_hash_data and virtual_hash_data.get('upper_line_params'): prev_upper_line_params = virtual_hash_data['upper_line_params'] if virtual_hash_data and virtual_hash_data.get('lower_line_params'): prev_lower_line_params = virtual_hash_data['lower_line_params'] # Track virtual line availability has_upper_virtual = virtual_hash_data.get('upper_line_params') is not None has_lower_virtual = virtual_hash_data.get('lower_line_params') is not None if has_upper_virtual or has_lower_virtual: virtual_line_frames += 1 # Update counters if virtual_hash_data: if virtual_hash_data.get('upper_line_params') is not None: total_virtual_line_detections += 1 if virtual_hash_data.get('lower_line_params') is not None: total_virtual_line_detections += 1 verification_info = virtual_hash_data.get('verification_info', {}) if verification_info.get('recovery_used', False): for line_type in ['upper_line', 'lower_line']: if verification_info['missing_counts'].get(line_type, 0) > 0: line_recovery_stats[line_type] += 1 # Ensure hash line fields exist even if empty if 'upper_hash_line_params' not in virtual_hash_data: virtual_hash_data['upper_hash_line_params'] = None if 'lower_hash_line_params' not in virtual_hash_data: virtual_hash_data['lower_hash_line_params'] = None if 'hash_detections_count' not in virtual_hash_data: virtual_hash_data['hash_detections_count'] = 0 if 'tracked_hash_marks' not in virtual_hash_data: virtual_hash_data['tracked_hash_marks'] = [] except Exception as e: print(f"Error in virtual line detection: {e}") virtual_hash_data = { 'upper_line_params': prev_upper_line_params, 'lower_line_params': prev_lower_line_params, 'upper_hash_line_params': None, 'lower_hash_line_params': None, 'hash_detections_count': 0, 'tracked_hash_marks': [], 'verification_info': {'recovery_used': True}, 'upper_line': prev_upper_line_params, 'lower_line': prev_lower_line_params, 'upper_hash_line': None, 'lower_hash_line': None, 'upper_centers': [], 'lower_centers': [], 'upper_hash_centers': [], 'lower_hash_centers': [], 'upper_hash_centers_with_conf': [], 'lower_hash_centers_with_conf': [], 'upper_hash_bboxes': [], 'lower_hash_bboxes': [] } # =========================================================== # STEP 4: NO-GO ZONE CALCULATION # =========================================================== if virtual_hash_data is not None: try: no_go_zone_data = sideline_validator.calculate_no_go_zone( virtual_hash_data, width, debug=False ) if no_go_zone_data: no_go_zone_display_frames += 1 else: no_go_zone_missing_frames += 1 except Exception as e: no_go_zone_missing_frames += 1 else: no_go_zone_missing_frames += 1 if sideline_validator.prev_upper_buffer_line and sideline_validator.prev_lower_buffer_line: no_go_zone_data = { 'upper_buffer_line': sideline_validator.prev_upper_buffer_line, 'lower_buffer_line': sideline_validator.prev_lower_buffer_line, 'virtual_distance': 0, 'upper_buffer_distance': 0, 'lower_buffer_distance': 0, 'upper_buffer_percentage': UPPER_BUFFER_PERCENTAGE, 'lower_buffer_percentage': LOWER_BUFFER_PERCENTAGE, 'center_x': width / 2, 'y_upper_buffer': 0, 'y_lower_buffer': 0, 'upper_virtual_line': None, 'lower_virtual_line': None, 'from_previous_frame': True } # =========================================================== # STEP 5: HASH MARK DETECTION WITH TRACKING (Using async results) # =========================================================== upper_hash_params = None lower_hash_params = None tracked_hash_marks = [] upper_centers = [] lower_centers = [] # Initialize current_hash_detections to avoid NameError current_hash_detections = [] if hash_detector is not None and 'hash_results' in locals() and hash_results is not None: try: # Convert hash_results to current_hash_detections format boxes = hash_results[0].boxes if hasattr(hash_results, '__len__') and len(hash_results) > 0 else None if boxes is not None: for box in boxes: x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) conf = float(box.conf[0]) if box.conf is not None else 0.0 current_hash_detections.append({ 'bbox': (x1, y1, x2, y2), 'confidence': conf }) # Get virtual slope directly from virtual_hash_data virtual_slope = None if virtual_hash_data: if virtual_hash_data.get('upper_line_params'): m, _ = virtual_hash_data['upper_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): virtual_slope = m if virtual_slope is None and virtual_hash_data.get('lower_line_params'): m, _ = virtual_hash_data['lower_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): virtual_slope = m upper_virtual_params = virtual_hash_data.get('upper_line_params') if virtual_hash_data else None lower_virtual_params = virtual_hash_data.get('lower_line_params') if virtual_hash_data else None # Update hash tracker with new detections if we have detections if current_hash_detections: # Only update if we have detections updated_upper_hash_params, updated_lower_hash_params = hash_tracker.update( current_hash_detections, height, virtual_slope, no_go_zone_data, upper_virtual_params, lower_virtual_params ) tracked_hash_marks = hash_tracker.get_tracked_hash_marks() upper_centers, lower_centers = hash_tracker.get_hash_centers() total_hash_detections += len(tracked_hash_marks) else: # No hash detections, use previous frame's parameters upper_hash_params, lower_hash_params = hash_tracker.get_hash_line_params() tracked_hash_marks = hash_tracker.get_tracked_hash_marks() upper_centers, lower_centers = hash_tracker.get_hash_centers() if virtual_hash_data is not None: virtual_hash_data['upper_hash_line_params'] = upper_hash_params virtual_hash_data['lower_hash_line_params'] = lower_hash_params virtual_hash_data['upper_hash_centers'] = [(int(c[0]), int(c[1])) for c in upper_centers] virtual_hash_data['lower_hash_centers'] = [(int(c[0]), int(c[1])) for c in lower_centers] virtual_hash_data['hash_detections_count'] = len(tracked_hash_marks) except Exception as e: print(f"Error in hash mark processing in frame {frame_index}: {e}") # Use previous frame's parameters as fallback if hash_tracker: upper_hash_params, lower_hash_params = hash_tracker.get_hash_line_params() tracked_hash_marks = hash_tracker.get_tracked_hash_marks() upper_centers, lower_centers = hash_tracker.get_hash_centers() if virtual_hash_data is not None: virtual_hash_data['upper_hash_line_params'] = upper_hash_params virtual_hash_data['lower_hash_line_params'] = lower_hash_params virtual_hash_data['upper_hash_centers'] = [(int(c[0]), int(c[1])) for c in upper_centers] virtual_hash_data['lower_hash_centers'] = [(int(c[0]), int(c[1])) for c in lower_centers] virtual_hash_data['tracked_hash_marks'] = tracked_hash_marks virtual_hash_data['hash_detections_count'] = len(tracked_hash_marks) # =========================================================== # STEP 6: SIDELINE ESTIMATION WITH VALIDATION (FIXED) # =========================================================== upper_line = None lower_line = None if yard_line_detections_raw and virtual_hash_data: try: top_points, bottom_points = extract_yard_line_endpoints(yard_line_detections_raw, debug=False) if len(top_points) >= 2 and len(bottom_points) >= 2: common_slope = get_virtual_line_slope(virtual_hash_data) if common_slope is not None: top_points_filtered, _ = remove_outliers_from_points(top_points, common_slope, max_distance=30) bottom_points_filtered, _ = remove_outliers_from_points(bottom_points, common_slope, max_distance=30) if len(top_points_filtered) >= 1 and len(bottom_points_filtered) >= 1: top_x = np.array([p[0] for p in top_points_filtered]) top_y = np.array([p[1] for p in top_points_filtered]) b_top_initial = np.mean(top_y - common_slope * top_x) bottom_x = np.array([p[0] for p in bottom_points_filtered]) bottom_y = np.array([p[1] for p in bottom_points_filtered]) b_bottom_initial = np.mean(bottom_y - common_slope * bottom_x) upper_line = { 'slope': common_slope, 'intercept': b_top_initial, 'method': 'virtual_slope_yard_fit', 'num_top_points': len(top_points_filtered), 'num_bottom_points': len(bottom_points_filtered), } lower_line = { 'slope': common_slope, 'intercept': b_bottom_initial, 'method': 'virtual_slope_yard_fit', 'num_top_points': len(top_points_filtered), 'num_bottom_points': len(bottom_points_filtered), } # CRITICAL: Apply sideline validator validated_upper, validated_lower, adjustments = sideline_validator.validate_and_adjust_sidelines( upper_line, lower_line, virtual_hash_data, width, height, debug=False ) upper_line = validated_upper lower_line = validated_lower sideline_adjustments.extend(adjustments) except Exception as e: print(f"Error in sideline estimation in frame {frame_index}: {e}") # Apply sideline verifier (this is correct in your code) upper_line, lower_line = sideline_verifier.verify_and_fill_missing_sidelines( upper_line, lower_line, width, height, debug=False ) # =========================================================== # STEP 7: UPDATE HASH TRACKER WITH SIDELINE INFORMATION # Only update if we have hash detections if hash_tracker and hash_detector and current_hash_detections: try: # Get virtual slope directly from virtual_hash_data virtual_slope = None if virtual_hash_data: if virtual_hash_data.get('upper_line_params'): m, _ = virtual_hash_data['upper_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): virtual_slope = m if virtual_slope is None and virtual_hash_data.get('lower_line_params'): m, _ = virtual_hash_data['lower_line_params'] if m is not None and not np.isnan(m) and not np.isinf(m): virtual_slope = m upper_virtual_params = virtual_hash_data.get('upper_line_params') if virtual_hash_data else None lower_virtual_params = virtual_hash_data.get('lower_line_params') if virtual_hash_data else None # Update hash tracker with sideline information updated_upper_hash_params, updated_lower_hash_params = hash_tracker.update( current_hash_detections, height, virtual_slope, no_go_zone_data, upper_virtual_params, lower_virtual_params ) tracked_hash_marks = hash_tracker.get_tracked_hash_marks() upper_centers, lower_centers = hash_tracker.get_hash_centers() if virtual_hash_data is not None: virtual_hash_data['upper_hash_line_params'] = updated_upper_hash_params virtual_hash_data['lower_hash_line_params'] = updated_lower_hash_params virtual_hash_data['upper_hash_centers'] = [(int(c[0]), int(c[1])) for c in upper_centers] virtual_hash_data['lower_hash_centers'] = [(int(c[0]), int(c[1])) for c in lower_centers] virtual_hash_data['tracked_hash_marks'] = tracked_hash_marks virtual_hash_data['hash_detections_count'] = len(tracked_hash_marks) except Exception as e: print(f"Error updating hash tracker with sidelines in frame {frame_index}: {e}") # =========================================================== # STEP 8: YARD LINE ASSIGNMENT # =========================================================== yard_line_data_for_assignment = [] for track in yard_line_tracks: if 'extended_line' in track: yard_line_data_for_assignment.append({ 'extended_line': track['extended_line'], 'confidence': track['confidence'], 'best_line': track.get('best_line', track['extended_line']), 'slope': track['line_props']['slope'], 'length': track['line_props']['length'] }) number_detections_for_assignment = [] for det in number_detections: final_class = det.get('final_class', det.get('stabilized_class', det.get('class_name', '0'))) final_conf = det.get('final_conf', det.get('stabilized_conf', det.get('cls_conf', 0.5))) number_detections_for_assignment.append({ 'x1': det['x1'], 'y1': det['y1'], 'x2': det['x2'], 'y2': det['y2'], 'class_name': final_class, 'cls_conf': final_conf, 'final_class': final_class, 'final_conf': final_conf, 'track_id': det.get('track_id', None), 'track_age': det.get('track_age', 0) }) # Assign yard lines yard_line_assignments = [] if yard_line_data_for_assignment and number_detections_for_assignment: yard_line_assignments = assign_yard_lines_dual_direction( yard_line_data_for_assignment, number_detections_for_assignment, width, field_tracker ) # Apply label stabilization using track history yard_line_assignments = yard_line_tracker.stabilize_labels_in_assignments(yard_line_assignments) # STEP 8B: ASSIGN INTERMEDIATE (BLUE) YARD LINES intermediate_assignments = [] if yard_line_assignments and len(yard_line_assignments) >= 1: try: intermediate_assignments = assign_intermediate_yard_lines( yard_line_tracks, yard_line_assignments, None, # transform_matrix not available yet None, # field_params not available yet width, height, debug=False ) # Add intermediate assignments to the main list yard_line_assignments.extend(intermediate_assignments) except Exception as e: print(f"Error assigning intermediate yard lines in frame {frame_index}: {e}") total_assignments += len(yard_line_assignments) # =========================================================== # STEP 9: CALCULATE PERSPECTIVE TRANSFORM FOR PLAYER MAPPING # =========================================================== transform_matrix = None field_params = None if yard_line_assignments and upper_line and lower_line: try: # Use the WITH_INTERMEDIATE version that you defined transform_matrix, field_params = calculate_perspective_transform_with_intermediate( yard_line_assignments, upper_line, lower_line, width, height ) except Exception as e: print(f"Error calculating perspective transform in frame {frame_index}: {e}") transform_matrix = None field_params = None # =========================================================== # STEP 10: PLAYER DETECTION WITH TARGET PLAYER ID # =========================================================== player_positions = [] # NEW: Determine drawing mode - if target player ID is provided, only tag that player # If no target player ID, show all players as before draw_all_labels = (user_selected_id is None) # Show all labels if no target player # Process player detection with overlay WITH TRACKING + OVERLAY if player_detector is not None and 'player_results' in locals() and player_results is not None: try: left_frame, person_count, refree_count, on_field_players, bench_players, \ player_positions, player_crops_saved, referee_crops_saved = detect_players_and_referees( left_frame, player_detector, person_count, refree_count, upper_line, lower_line, transform_matrix, field_params, virtual_hash_data, yard_line_assignments, draw_boxes=True, frame_index=frame_index, save_crops=False, players_dir=players_dir, referees_dir=referees_dir, overlay_image=None, # We'll create overlay separately id_frame_count=id_frame_count, # Update frame counts for consistency top_player_id=user_selected_id, # Pass for yellow highlighting (or None) draw_all_labels=draw_all_labels # Show all labels if no target player ) # ========== UPDATE SPEED TRACKING AND CSV ========== if user_selected_id is not None and player_positions: # Find the target player for player in player_positions: if player.get('track_id') == user_selected_id: # Get field coordinates (in YARDS) field_coords = player.get('field_coords') # In the main loop where you update the tracker: if field_coords is not None: player_speed_tracker.update_position(frame_index, field_coords) # Export at 10fps intervals (every 6 frames at 60fps) if frame_index % player_speed_tracker.stats_interval_frames == 0: # Get smoothed values from tracker speed_stats = player_speed_tracker.get_speed_all_units() accel_stats = player_speed_tracker.get_acceleration_all_units() player_state = player_speed_tracker.get_current_state() # Get raw position before EMA raw_position = player_speed_tracker.get_raw_position() # Get Kalman velocity for speed prediction kalman_velocity = player_speed_tracker.get_kalman_velocity() # Calculate time with 2 decimal places time_seconds = round(frame_index / fps, 2) # Extract smoothed position smoothed_position = (player_state['position']['x_yards'], player_state['position']['y_yards']) # Extract smoothed speed and acceleration smoothed_speed = speed_stats['yards_per_sec'] smoothed_accel = accel_stats['yards_per_sec_sq'] # Write to CSV with NEW format write_metadata_csv( metadata_csv_path, frame_index, time_seconds, smoothed_position, smoothed_speed, smoothed_accel, raw_position, kalman_velocity, debug=(frame_index % 180 == 0) # Debug every 3 seconds ) # Print debug info (optional) if frame_index % 180 == 0: kalman_speed = np.sqrt(kalman_velocity[0]**2 + kalman_velocity[1]**2) print(f"Frame {frame_index}: Time: {time_seconds}s, " f"Smoothed Pos: ({smoothed_position[0]:.1f}, {smoothed_position[1]:.1f}), " f"Raw Pos: ({raw_position[0]:.1f}, {raw_position[1]:.1f}), " f"Speed: {smoothed_speed:.1f} yd/s, " f"Kalman Speed: {kalman_speed:.1f} yd/s") break # Found the player, exit loop # NEW: Periodic top-ID print (every 30 frames, like Code 1 consistency check) if frame_index % 30 == 0 and id_frame_count: top_id = max(id_frame_count, key=id_frame_count.get) top_frames = id_frame_count[top_id] print(f"Frame {frame_index}: Current top ID: {top_id} ({top_frames} frames)") # DEBUG: Print player coordinates (existing) if frame_index % 30 == 0 and player_positions: # Every 30 frames debug_player_coordinates(frame_index, player_positions) # Update global counters (existing) total_on_field_players += on_field_players total_bench_players += bench_players total_player_crops_saved += player_crops_saved total_referee_crops_saved += referee_crops_saved except Exception as e: print(f"Error in player detection in frame {frame_index}: {e}") player_positions = [] # =========================================================== # STEP 11: CREATE OVERLAY AND BATCH ALL DRAWINGS # =========================================================== # Create overlay for batched drawing overlay = np.zeros((height, width, 4), dtype=np.uint8) # BGRA overlay # 1. Draw colored hash points with track IDs if upper_centers or lower_centers: draw_hash_points_with_tracking_info(left_frame, upper_centers, lower_centers, tracked_hash_marks, overlay_image=overlay) # 2. Draw virtual and hash lines if virtual_hash_data is not None: try: drawn_line_params = draw_enhanced_lines(left_frame, virtual_hash_data, overlay_image=overlay) except Exception as e: print(f"Error drawing enhanced lines in frame {frame_index}: {e}") # 3. Draw validated sidelines (yellow lines) on overlay draw_sidelines_on_overlay(overlay, upper_line, lower_line, width, height, overlay_image=overlay) # 4. Draw yard lines and assignments on overlay draw_yard_lines_and_assignments(overlay, yard_line_tracks, yard_line_assignments, overlay_image=overlay) # 6. Draw player markers on overlay # Note: Player markers are already drawn on left_frame with correct highlighting # We just need to blend the overlay # 7. Draw yard line assignments with labels on overlay for assignment in yard_line_assignments: line_data = assignment['line_data'] extended_line = line_data['extended_line'] yard_line_num = assignment['yard_line_num'] confidence = assignment.get('confidence', 0.5) side = assignment.get('side', '?') is_intermediate = assignment.get('is_intermediate', False) # Use different colors for intermediate vs main lines if is_intermediate: if overlay is not None: line_color = (255, 0, 0, 200) # BLUE for intermediate with alpha else: line_color = (255, 0, 0) # BLUE for intermediate line_thickness = 2 else: if confidence > 0.7: if overlay is not None: line_color = (0, 0, 255, 200) # RED for high confidence main lines with alpha else: line_color = (0, 0, 255) # RED for high confidence main lines else: if overlay is not None: line_color = (0, 165, 255, 200) # ORANGE for lower confidence main lines with alpha else: line_color = (0, 165, 255) # ORANGE for lower confidence main lines line_thickness = 3 if len(extended_line) >= 4: center_x = (extended_line[0] + extended_line[2]) // 2 center_y = (extended_line[1] + extended_line[3]) // 2 # Add label with appropriate color if is_intermediate: yard_label = f"{yard_line_num}{side} (INT)" if overlay is not None: label_color = (255, 0, 0, 255) # BLUE with full alpha else: label_color = line_color else: yard_label = f"{yard_line_num}{side} ({int(confidence*100)}%)" label_color = line_color # Draw text on overlay target = overlay if overlay is not None else left_frame cv2.putText(target, yard_label, (center_x + 15, center_y - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, label_color, 2) # 8. Draw numbers with final prediction display on overlay for detection in number_detections: if 'track_id' in detection: x1, y1, x2, y2 = detection['bbox'] stabilized_class = detection.get('stabilized_class', detection.get('class_name', 'Unknown')) center_y = (y1 + y2) // 2 if center_y < height * 0.7: # Upper numbers - BLUE if overlay is not None: box_color = (255, 0, 0, 200) # Blue with alpha text_color = (255, 0, 0, 255) # Blue with full alpha else: box_color = (255, 0, 0) # Blue in BGR text_color = (255, 0, 0) # Blue in BGR else: # Lower numbers - DARK RED/MAGENTA if overlay is not None: box_color = (128, 0, 128, 200) # Dark magenta with alpha text_color = (128, 0, 128, 255) # Dark magenta with full alpha else: box_color = (128, 0, 128) # Dark magenta in BGR text_color = (128, 0, 128) # Dark magenta in BGR # Draw bounding box target = overlay if overlay is not None else left_frame cv2.rectangle(target, (x1, y1), (x2, y2), box_color, 2) # Calculate center of bounding box center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 # Display only the final number in center with bold text (increased thickness) cv2.putText(target, stabilized_class, (center_x - 20, center_y + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, text_color, 4) # Changed thickness from 2 to 4 # =========================================================== # STEP 12: GPU BLENDING OF OVERLAY WITH FRAME # =========================================================== if np.any(overlay): # Only blend if overlay has content # Upload overlay to GPU gpu_blender.upload_overlay_to_gpu(overlay) # Blend using GPU or CPU fallback if gpu_blender.gpu_available and gpu_frame_available: gpu_result = gpu_blender.blend_gpu() if gpu_result is not None: left_frame = gpu_blender.download_from_gpu(gpu_result) else: left_frame = gpu_blender.blend_cpu(left_frame, overlay) else: left_frame = gpu_blender.blend_cpu(left_frame, overlay) # ========== DRAW SPEED/ACCEL OVERLAY AT 10FPS ========== # Get current stats (they're calculated every frame) speed_stats = player_speed_tracker.get_speed_all_units() accel_stats = player_speed_tracker.get_acceleration_all_units() # Draw on left frame at 10fps (pass frame_index for conditional drawing) left_frame = field_template_manager.draw_speed_acceleration_overlay( left_frame, speed_stats, accel_stats, player_speed_tracker=player_speed_tracker, position_mode='video', frame_index=frame_index, # Pass for 10fps logic debug=(frame_index % 60 == 0) # Debug every second ) # =========================================================== # STEP 13: CREATE FIELD TEMPLATE AND DRAW PLAYERS # =========================================================== # 13a: Create/update the base field template with yard lines right_frame = field_template_manager.update_field_template( yard_line_assignments, virtual_hash_data, debug=(frame_index % 30 == 0) ) # 13b: Draw players on the template (if positions available) if player_positions: right_frame = field_template_manager.draw_players_on_template( right_frame, player_positions, top_player_id=user_selected_id, # For yellow highlighting debug=(frame_index % 30 == 0) ) # =========================================================== # STEP 14: NEW LAYOUT - DOUBLE HEIGHT WITH GRAPH (Only if processing full video) # =========================================================== if not process_limited_frames: # Only create full layout if we're processing the whole video if user_selected_id is not None: # Generate live graph from CSV data graph_img = draw_live_graph(metadata_csv_path, width, combined_height) else: # No target player, no graph needed graph_img = np.zeros((combined_height, width, 3), dtype=np.uint8) # Ensure all frames have the same dimensions if left_frame.shape[0] != height or left_frame.shape[1] != width: left_frame = cv2.resize(left_frame, (width, height)) if right_frame.shape[0] != height or right_frame.shape[1] != width: right_frame = cv2.resize(right_frame, (width, height)) # Create LEFT COLUMN - Stack video on top, 2D map on bottom left_column = np.zeros((combined_height, width, 3), dtype=np.uint8) # Top-left: Video frame (no padding, left-aligned) left_column[0:height, 0:width] = left_frame # Bottom-left: 2D field template left_column[height:combined_height, 0:width] = right_frame # RIGHT COLUMN - Full graph (width x 2*height) if graph_img.shape[0] != combined_height or graph_img.shape[1] != width: graph_img = cv2.resize(graph_img, (width, combined_height)) # Combine left column and right graph horizontally combined_frame = np.hstack([left_column, graph_img]) # Final check: combined_frame should be (2*height, 2*width) if combined_frame.shape != (combined_height, combined_width, 3): combined_frame = cv2.resize(combined_frame, (combined_width, combined_height)) # =========================================================== # STEP 15: WRITE OUTPUT (Only for full video processing) # =========================================================== if out is not None: out.write(combined_frame) else: # For limited frame processing, store the 10th frame if processed_frames == 10: # Save the 10th frame as a snippet tenth_frame_snippet = left_frame.copy() print(f"Captured 10th frame snippet (size: {tenth_frame_snippet.shape})") except Exception as e: print(f"\nError during processing: {e}") import traceback traceback.print_exc() finally: cap.release() if out is not None: out.release() # Cleanup async inferencer if 'async_inferencer' in locals(): async_inferencer.cleanup() # Cleanup GPU blender gpu_blender.cleanup() # NEW: Save the 10th frame snippet if we were in limited mode if process_limited_frames and tenth_frame_snippet is not None: # Create a path for the snippet snippet_filename = f"snippet_{os.path.basename(output_video_path).replace('.mp4', '')}.jpg" snippet_path = os.path.join(outputs_dir, snippet_filename) cv2.imwrite(snippet_path, tenth_frame_snippet) print(f"Saved 10th frame snippet to: {snippet_path}") frame_snippet_path = snippet_path # Print final statistics print("\n" + "="*80) print("PROCESSING COMPLETE - FINAL STATISTICS") print("="*80) print(f"Total frames processed: {processed_frames}") if user_selected_id is not None: print(f"CSV entries written: {processed_frames // 6} (10fps)") print(f"Final CSV file: {metadata_csv_path}") print("="*80) print("\n" + "="*80) print("FIELD TEMPLATE VISUALIZATION - FINAL STATISTICS") print("="*80) print(f"Total number detections: {total_number_detections}") print(f"Total yard line detections: {total_yard_line_detections}") print(f"Total yard line assignments: {total_assignments}") print(f"Total hash mark detections: {total_hash_detections}") print(f"Total virtual line detections: {total_virtual_line_detections}") print(f"Total on-field players detected: {total_on_field_players}") print(f"Total bench players detected: {total_bench_players}") print(f"Total player crops saved: {total_player_crops_saved}") print(f"Total referee crops saved: {total_referee_crops_saved}") print(f"Field template frames: {field_template_manager.stats['frames_processed']}") print(f"Yard lines highlighted on template: {field_template_manager.stats['yard_lines_highlighted']}") print(f"Virtual lines detected: {field_template_manager.stats['virtual_lines_detected']}") print(f"Hash lines detected: {field_template_manager.stats['hash_lines_detected']}") print(f"No-go zone displayed frames: {no_go_zone_display_frames}/{processed_frames}") # Print target player tracking results print("\n" + "="*80) print("TARGET PLAYER TRACKING RESULTS") print("="*80) if user_selected_id is not None: print(f"✅ Target Player ID: {user_selected_id}") print(f" Speed tracking: {'ENABLED' if player_speed_tracker.stats['frames_tracked'] > 0 else 'NOT DETECTED'}") else: print(f"No target player specified - processed {processed_frames} frames for player ID discovery.") print(f"10th frame snippet saved for player ID selection.") # Print top 5 tracked player IDs based on frame presence print("\n" + "="*80) print("TOP 5 TRACKED PLAYER IDs BY FRAME PRESENCE") print("="*80) if id_frame_count: # Sort by value (frame count) in descending order sorted_ids = sorted(id_frame_count.items(), key=lambda x: x[1], reverse=True) top_5 = sorted_ids[:5] print(f"Total unique player IDs tracked: {len(id_frame_count)}") print("\nRank | Player ID | Frames Present | Percentage") print("-" * 50) for rank, (player_id, frame_count) in enumerate(top_5, 1): percentage = (frame_count / processed_frames) * 100 print(f"{rank:4d} | {player_id:9d} | {frame_count:13d} | {percentage:8.1f}%") if len(sorted_ids) > 5: print(f"\nNote: Showing top 5 out of {len(sorted_ids)} tracked player IDs") else: print("No player tracking data available (player detection may be disabled)") # Return results with different format based on processing mode if process_limited_frames: # Return the 10th frame snippet path and player IDs return { "processed_frames": processed_frames, "number_detections": total_number_detections, "yard_line_detections": total_yard_line_detections, "assignments": total_assignments, "hash_detections": total_hash_detections, "on_field_players": total_on_field_players, "bench_players": total_bench_players, "player_crops_saved": total_player_crops_saved, "referee_crops_saved": total_referee_crops_saved, "frame_snippet_path": frame_snippet_path, "player_ids": list(id_frame_count.keys()) if id_frame_count else [], "top_player_ids": dict(sorted(id_frame_count.items(), key=lambda x: x[1], reverse=True)[:5]) if id_frame_count else {}, "processing_mode": "limited_frames_no_target" } else: # Return the video path for full processing return { "processed_frames": processed_frames, "number_detections": total_number_detections, "yard_line_detections": total_yard_line_detections, "assignments": total_assignments, "hash_detections": total_hash_detections, "on_field_players": total_on_field_players, "bench_players": total_bench_players, "player_crops_saved": total_player_crops_saved, "referee_crops_saved": total_referee_crops_saved, "output_path": output_video_path, "target_player_id": user_selected_id, "player_speed_stats": player_speed_tracker.get_stats(), "final_speed": player_speed_tracker.get_speed_all_units(), "final_accel": player_speed_tracker.get_acceleration_all_units(), "processing_mode": "full_video_with_target" }