Spaces:
Runtime error
Runtime error
Download app.py from SoccerQ/video-analysis: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/spaces/SoccerQ/video-analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/SoccerQ/video-analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SoccerQ/video-analysis/resolve/main/app.py
10.4 kB
| """ | |
| SoccerQ Vision - Video Analysis | |
| ================================ | |
| Analisi video calcio con YOLO + DeepSORT | |
| Supporta video da URL o upload file | |
| """ | |
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| import tempfile | |
| import os | |
| import requests | |
| from pathlib import Path | |
| # Import pipeline components | |
| from trackers.tracker import Tracker | |
| from player_ball_assigner import PlayerBallAssigner | |
| from camera_movement_estimator import CameraMovementEstimator | |
| from view_transformer import ViewTransformer | |
| from speed_and_distance_estimator import SpeedAndDistance_Estimator | |
| from team_assigner.team_assigner import DigitAssigner | |
| # Global tracker instance | |
| tracker = None | |
| digit_assigner = None | |
| def init_tracker(): | |
| """Initialize tracker (lazy loading)""" | |
| global tracker, digit_assigner | |
| if tracker is None: | |
| print("Initializing YOLO tracker...") | |
| tracker = Tracker('models/bbox_180camerar_best', use_gpu=True) | |
| digit_assigner = DigitAssigner() | |
| return tracker, digit_assigner | |
| # Sample video URLs (publicly accessible) | |
| SAMPLE_VIDEO_URL = "https://github.com/roboflow/supervision/raw/develop/demo_files/soccer-players.mp4" | |
| def download_video(url: str, max_size_mb: int = 100) -> str: | |
| """Download video from URL to temp file""" | |
| print(f"Downloading video from: {url}") | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' | |
| } | |
| try: | |
| # Stream download to check size | |
| response = requests.get(url, stream=True, timeout=30, headers=headers) | |
| response.raise_for_status() | |
| # Check content length | |
| content_length = response.headers.get('content-length') | |
| if content_length and int(content_length) > max_size_mb * 1024 * 1024: | |
| raise ValueError(f"Video too large (max {max_size_mb}MB)") | |
| # Save to temp file | |
| suffix = Path(url).suffix or '.mp4' | |
| if '?' in suffix: | |
| suffix = suffix.split('?')[0] | |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix) | |
| for chunk in response.iter_content(chunk_size=8192): | |
| temp_file.write(chunk) | |
| temp_file.close() | |
| print(f"Downloaded to: {temp_file.name}") | |
| return temp_file.name | |
| except Exception as e: | |
| raise ValueError(f"Failed to download video: {str(e)}") | |
| def process_video_frames(frames: list, progress_callback=None) -> list: | |
| """Process frames through the analysis pipeline""" | |
| global tracker, digit_assigner | |
| print(f"Starting process_video_frames with {len(frames)} frames") | |
| tracker, digit_assigner = init_tracker() | |
| if not frames: | |
| print("No frames to process!") | |
| return [] | |
| # Initialize estimators | |
| camera_movement_estimator = CameraMovementEstimator(frames[0]) | |
| view_transformer = ViewTransformer() | |
| speed_and_distance_estimator = SpeedAndDistance_Estimator() | |
| player_assigner = PlayerBallAssigner() | |
| try: | |
| # Get tracks from YOLO + DeepSORT | |
| if progress_callback: | |
| progress_callback(0.2, "Running YOLO detection...") | |
| print("Starting YOLO detection...") | |
| tracks = tracker.get_object_tracks(frames) | |
| print(f"YOLO detection complete. Players detected in frames: {len(tracks.get('players', []))}") | |
| # Add positions | |
| if progress_callback: | |
| progress_callback(0.4, "Computing positions...") | |
| tracker.add_position_to_tracks(tracks) | |
| # Camera movement compensation | |
| if progress_callback: | |
| progress_callback(0.5, "Estimating camera movement...") | |
| camera_movement_per_frame = camera_movement_estimator.get_camera_movement(frames) | |
| camera_movement_estimator.add_adjust_positions_to_tracks(tracks, camera_movement_per_frame) | |
| # Transform to field coordinates | |
| view_transformer.add_transformed_position_to_tracks(tracks) | |
| # Speed and distance | |
| if progress_callback: | |
| progress_callback(0.6, "Computing speed and distance...") | |
| speed_and_distance_estimator.add_speed_and_distance_to_tracks(tracks) | |
| # Process digits (jersey numbers) | |
| if progress_callback: | |
| progress_callback(0.7, "Detecting jersey numbers...") | |
| process_digits(tracks, frames) | |
| # Draw annotations | |
| if progress_callback: | |
| progress_callback(0.8, "Drawing annotations...") | |
| output_frames = tracker.draw_annotations(frames, tracks, [], digit_assigner) | |
| speed_and_distance_estimator.draw_speed_and_distance(output_frames, tracks) | |
| return output_frames | |
| except Exception as e: | |
| print(f"Error processing frames: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return frames # Return original frames on error | |
| def process_digits(player_track, video_frames): | |
| """Process jersey number detection""" | |
| global digit_assigner | |
| all_players_all_frames = player_track.get('players', []) | |
| for frame_num, all_players in enumerate(all_players_all_frames): | |
| if frame_num % 30 != 0: | |
| continue | |
| frame = video_frames[frame_num] | |
| frame_height = frame.shape[0] | |
| for playerID in sorted(all_players.keys()): | |
| playerData = all_players[playerID] | |
| digit_assigner.get_player_digit( | |
| frame_height, | |
| frame, | |
| playerData['bbox'], | |
| playerID, | |
| draw_bbox=True, | |
| only_draw_if_exists=False | |
| ) | |
| def analyze_video(video_input, video_url, max_frames, progress=gr.Progress()): | |
| """Main analysis function for Gradio""" | |
| video_path = None | |
| temp_downloaded = False | |
| try: | |
| # Determine video source | |
| if video_url and video_url.strip(): | |
| progress(0.05, "Downloading video...") | |
| video_path = download_video(video_url.strip()) | |
| temp_downloaded = True | |
| elif video_input: | |
| video_path = video_input | |
| else: | |
| return None, "Please provide a video URL or upload a file" | |
| # Open video | |
| progress(0.1, "Opening video...") | |
| cap = cv2.VideoCapture(video_path) | |
| if not cap.isOpened(): | |
| return None, f"Failed to open video: {video_path}" | |
| # Get video properties | |
| fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25 | |
| 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)) | |
| # Limit frames | |
| frames_to_process = min(max_frames, total_frames) | |
| # Read frames | |
| progress(0.15, f"Reading {frames_to_process} frames...") | |
| frames = [] | |
| for i in range(frames_to_process): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| frames.append(frame) | |
| cap.release() | |
| if not frames: | |
| return None, "No frames could be read from video" | |
| # Process frames | |
| def update_progress(pct, msg): | |
| progress(pct, msg) | |
| output_frames = process_video_frames(frames, update_progress) | |
| # Write output video | |
| progress(0.9, "Encoding output video...") | |
| output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) | |
| for frame in output_frames: | |
| out.write(frame) | |
| out.release() | |
| # Cleanup temp download | |
| if temp_downloaded and video_path: | |
| try: | |
| os.unlink(video_path) | |
| except: | |
| pass | |
| progress(1.0, "Done!") | |
| stats = f""" | |
| ### Analysis Complete | |
| - **Frames processed**: {len(output_frames)} | |
| - **Resolution**: {width}x{height} | |
| - **FPS**: {fps} | |
| - **Duration**: {len(output_frames)/fps:.1f}s | |
| """ | |
| return output_path, stats | |
| except Exception as e: | |
| import traceback | |
| error_msg = f"Error: {str(e)}\n{traceback.format_exc()}" | |
| print(error_msg) | |
| return None, error_msg | |
| # Gradio Interface | |
| with gr.Blocks(title="SoccerQ Vision - Video Analysis") as demo: | |
| gr.Markdown(""" | |
| # SoccerQ Vision - Video Analysis | |
| Analisi automatica di video calcio con: | |
| - **YOLO** - Detection giocatori e palla | |
| - **DeepSORT** - Tracking persistente | |
| - **Jersey Recognition** - Riconoscimento numeri maglia | |
| - **Speed/Distance** - Calcolo velocita e distanza | |
| --- | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### Input Video") | |
| video_url = gr.Textbox( | |
| label="Video URL", | |
| placeholder="https://example.com/video.mp4", | |
| info="Inserisci URL di un video (Bundesliga, YouTube, etc.)" | |
| ) | |
| gr.Markdown("**Oppure**") | |
| video_file = gr.Video( | |
| label="Upload Video", | |
| sources=["upload"] | |
| ) | |
| max_frames = gr.Slider( | |
| minimum=30, | |
| maximum=900, | |
| value=150, | |
| step=30, | |
| label="Max Frames", | |
| info="Numero massimo di frame da processare (30 frame = 1 sec @ 30fps)" | |
| ) | |
| gr.Markdown("### Sample Videos") | |
| sample_btn = gr.Button("Load Soccer Sample", size="sm") | |
| sample_btn.click( | |
| fn=lambda: SAMPLE_VIDEO_URL, | |
| outputs=video_url | |
| ) | |
| analyze_btn = gr.Button("Analyze Video", variant="primary", size="lg") | |
| with gr.Column(scale=2): | |
| gr.Markdown("### Output") | |
| output_video = gr.Video(label="Analyzed Video") | |
| output_stats = gr.Markdown(label="Statistics") | |
| # Connect analyze button | |
| analyze_btn.click( | |
| fn=analyze_video, | |
| inputs=[video_file, video_url, max_frames], | |
| outputs=[output_video, output_stats] | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| ### Note | |
| - I video vengono processati localmente | |
| - Per video lunghi, aumenta il limite frame | |
| - Supporta: MP4, AVI, MOV, MKV | |
| **SoccerQ Vision** - [GitHub](https://github.com/SoccerQ) | [HuggingFace](https://huggingface.co/SoccerQ) | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |