NFL_CV_DEMO / app.py
Prathmesh Gawali
manual selection and updated graph logic
f3fb497
Raw History Blame Contribute Delete
27.3 kB
import gradio as gr
import time
import uuid
import shutil
import base64
import queue
import threading
import os
from pathlib import Path
import subprocess
try:
from main import process_video
MAIN_AVAILABLE = True
except ImportError:
print("Warning: Main module not available. Running in simulation mode.")
MAIN_AVAILABLE = False
try:
from graph import generate_player_graph
GRAPH_AVAILABLE = True
except ImportError:
print("Warning: Graph module not available. Visualization will be disabled.")
GRAPH_AVAILABLE = False
UPLOAD_DIR = Path("uploads")
OUTPUT_DIR = Path("outputs")
UPLOAD_DIR.mkdir(exist_ok=True)
OUTPUT_DIR.mkdir(exist_ok=True)
is_processing = False
current_progress = {}
processing_thread = None
first_frame_path = None
def cleanup_old_files(hours_old=24):
import time as ttime
current_time = ttime.time()
for directory in [UPLOAD_DIR, OUTPUT_DIR]:
if not directory.exists():
continue
for file in directory.iterdir():
if file.is_file():
file_age = current_time - file.stat().st_mtime
if file_age > hours_old * 3600:
try:
file.unlink()
except:
pass
def get_gpu_info():
try:
import torch
if torch.cuda.is_available():
gpu_name = torch.cuda.get_device_name(0)
return f"GPU: {gpu_name}"
except:
pass
return "Running on CPU"
def extract_first_frame(video_path, output_path):
try:
import cv2
cap = cv2.VideoCapture(str(video_path))
if cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imwrite(str(output_path), frame)
cap.release()
except Exception as e:
print(f"Error extracting first frame: {e}")
def format_time(seconds):
if seconds < 0:
return "--:--"
if seconds < 3600:
minutes = int(seconds // 60)
secs = int(seconds % 60)
return f"{minutes:02d}:{secs:02d}"
else:
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def process_video_in_thread(input_path, output_path, progress_queue, player_id=None, thread_result=None):
start_time = time.time()
def progress_callback(percent, message):
elapsed_time = time.time() - start_time
if percent > 0:
estimated_total_time = (elapsed_time / percent) * 100
remaining_time = estimated_total_time - elapsed_time
else:
estimated_total_time = 0
remaining_time = 0
elapsed_str = format_time(elapsed_time)
remaining_str = format_time(remaining_time) if remaining_time > 0 else "--:--"
progress_queue.put({
"percent": percent,
"message": message,
"elapsed": elapsed_str,
"left": remaining_str
})
try:
if MAIN_AVAILABLE:
result = process_video(
input_video_path=str(input_path),
output_video_path=str(output_path),
progress_callback=progress_callback,
target_player_id=player_id # Pass the player ID parameter
)
total_time = time.time() - start_time
total_time_str = format_time(total_time)
progress_queue.put({
"percent": 99,
"message": "Finalizing...",
"elapsed": total_time_str,
"left": "00:01"
})
if thread_result is not None:
thread_result[0] = result
return result
else:
# Simulation mode
for i in range(0, 100, 5):
time.sleep(0.1)
elapsed = time.time() - start_time
if i > 0:
estimated_total = (elapsed / i) * 100
remaining = estimated_total - elapsed
else:
remaining = 0
elapsed_str = format_time(elapsed)
remaining_str = format_time(remaining) if remaining > 0 else "--:--"
progress_queue.put({
"percent": i,
"message": f"Simulating processing... {i}%",
"elapsed": elapsed_str,
"left": remaining_str
})
elapsed = time.time() - start_time
elapsed_str = format_time(elapsed)
progress_queue.put({
"percent": 99,
"message": "Finalizing...",
"elapsed": elapsed_str,
"left": "00:01"
})
# In simulation mode, create a dummy image for limited frame mode
if player_id is None:
# Create a dummy image for the snippet
import cv2
import numpy as np
dummy_image = np.ones((480, 640, 3), dtype=np.uint8) * 255
cv2.putText(dummy_image, "Simulated Player Discovery Image", (50, 240),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
cv2.putText(dummy_image, "Player IDs: 7, 12, 23, 42, 55", (50, 280),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
cv2.imwrite(str(output_path), dummy_image)
result = {"frame_snippet_path": str(output_path)}
else:
# For full video, we don't create a video in simulation
result = {"output_path": str(output_path)}
if thread_result is not None:
thread_result[0] = result
return result
except Exception as e:
elapsed_time = time.time() - start_time
elapsed_str = format_time(elapsed_time)
progress_queue.put({
"percent": 0,
"message": f"Error: {str(e)}",
"elapsed": elapsed_str,
"left": "--:--"
})
raise
def create_progress_overlay_html(progress_data, first_frame_image=None, show_overlay=True):
percent = progress_data.get("percent", 0)
message = progress_data.get("message", "Ready")
elapsed = progress_data.get("elapsed", "00:00")
remaining = progress_data.get("left", "--:--")
blur_amount = max(0, 20 - (percent * 0.2))
overlay_opacity = max(0, 0.95 - (percent * 0.0095))
if not show_overlay:
return f"""
<div style="
position: relative;
width: 100%;
height: 600px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 20px;
">
Upload a video to begin processing
</div>
"""
bg_style = ""
if first_frame_image:
bg_style = f"""
background-image: url('data:image/jpeg;base64,{first_frame_image}');
background-size: cover;
background-position: center;
"""
else:
bg_style = "background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);"
radius = 80
circumference = 2 * 3.14159 * radius
dash_offset = circumference - (percent / 100) * circumference
html = f"""
<div style="
position: relative;
width: 100%;
height: 600px;
{bg_style}
overflow: hidden;
border-radius: 12px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
">
<div style="
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
backdrop-filter: blur({blur_amount}px);
-webkit-backdrop-filter: blur({blur_amount}px);
background: rgba(0, 0, 0, {overlay_opacity});
transition: all 0.5s ease;
"></div>
<div style="
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 10;
">
<div style="position: relative; width: 200px; height: 200px;">
<svg style="transform: rotate(-90deg); width: 200px; height: 200px;">
<circle
cx="100"
cy="100"
r="{radius}"
stroke="rgba(255, 255, 255, 0.2)"
stroke-width="8"
fill="none"
/>
<circle
cx="100"
cy="100"
r="{radius}"
stroke="#10b981"
stroke-width="8"
fill="none"
stroke-dasharray="{circumference}"
stroke-dashoffset="{dash_offset}"
stroke-linecap="round"
style="transition: stroke-dashoffset 0.5s ease;"
/>
</svg>
<div style="
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: white;
">
<div style="
font-size: 42px;
font-weight: 700;
margin-bottom: 8px;
text-shadow: 0 2px 10px rgba(0,0,0,0.5);
">{percent:.0f}%</div>
<div style="
font-size: 14px;
opacity: 0.9;
margin-bottom: 3px;
">
<span style="opacity: 0.7;">Time:</span> {elapsed}
</div>
<div style="
font-size: 14px;
opacity: 0.9;
">
<span style="opacity: 0.7;">Left:</span> {remaining}
</div>
</div>
</div>
<div style="
margin-top: 30px;
color: white;
font-size: 16px;
font-weight: 500;
text-align: center;
max-width: 400px;
padding: 0 20px;
text-shadow: 0 2px 10px rgba(0,0,0,0.5);
">{message}</div>
</div>
</div>
"""
return html
def generate_stats_graph():
if not GRAPH_AVAILABLE:
return (
gr.update(visible=True, value=None),
gr.update(visible=True, value="Warning: Graph module not available. Please check graph.py is in the same directory.")
)
try:
graph_path = generate_player_graph()
if graph_path is None:
return (
gr.update(visible=True, value=None),
gr.update(visible=True, value="Error: Could not generate graph. Check if metadata.csv exists in outputs folder.")
)
return (
gr.update(visible=True, value=graph_path),
gr.update(visible=True, value="Graph generated successfully!")
)
except Exception as e:
error_msg = f"Error generating graph: {str(e)}"
print(error_msg)
return (
gr.update(visible=True, value=None),
gr.update(visible=True, value=error_msg)
)
def upload_and_process(video_file, player_id_str=None):
global is_processing, current_progress, processing_thread, first_frame_path
if not video_file:
return (
None,
None,
create_progress_overlay_html({"percent": 0, "message": "Please upload a video file", "elapsed": "00:00", "left": "--:--"}, show_overlay=False),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
if is_processing:
return (
None,
None,
create_progress_overlay_html({"percent": 0, "message": "Already processing another video", "elapsed": "00:00", "left": "--:--"}, show_overlay=True),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
try:
is_processing = True
current_progress = {"percent": 0, "message": "Finding the most optimal player for tracking...", "elapsed": "00:00", "left": "--:--"}
cleanup_old_files()
timestamp = int(time.time())
unique_id = str(uuid.uuid4())[:8]
input_filename = Path(video_file).stem
# Check if player_id_str is empty or "discover"
if player_id_str is None or str(player_id_str).strip() == "" or str(player_id_str).lower() == "discover":
player_id = None
output_path = OUTPUT_DIR / f"snippet_{timestamp}_{unique_id}_{input_filename}.jpg"
processing_mode = "limited_frames"
current_progress["message"] = "Discovering players in first 10 frames..."
else:
try:
player_id = int(player_id_str)
output_path = OUTPUT_DIR / f"processed_{timestamp}_{unique_id}_{input_filename}.mp4"
processing_mode = "full_video"
current_progress["message"] = f"Processing full video with tracking for Player ID: {player_id}..."
except ValueError:
return (
None,
None,
create_progress_overlay_html({"percent": 0, "message": f"Invalid Player ID: '{player_id_str}'. Please enter a number or leave empty for discovery mode.", "elapsed": "00:00", "left": "--:--"}, show_overlay=True),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
upload_path = UPLOAD_DIR / f"{timestamp}_{unique_id}_{input_filename}.mp4"
first_frame_path = OUTPUT_DIR / f"frame_{timestamp}_{unique_id}.jpg"
shutil.copy(video_file, upload_path)
extract_first_frame(upload_path, first_frame_path)
first_frame_base64 = None
if first_frame_path and first_frame_path.exists():
with open(first_frame_path, "rb") as f:
first_frame_base64 = base64.b64encode(f.read()).decode()
progress_queue = queue.Queue()
# Create a list to store thread result
thread_result = [None]
processing_thread = threading.Thread(
target=process_video_in_thread,
args=(upload_path, output_path, progress_queue, player_id, thread_result)
)
processing_thread.start()
yield (
None,
None,
create_progress_overlay_html(current_progress, first_frame_base64, show_overlay=True),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
reached_99_percent = False
while processing_thread.is_alive():
try:
while True:
try:
progress_update = progress_queue.get_nowait()
current_progress = progress_update
if progress_update.get("percent", 0) >= 99:
reached_99_percent = True
html_content = create_progress_overlay_html(progress_update, first_frame_base64, show_overlay=True)
yield (
None,
None,
html_content,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
except queue.Empty:
break
time.sleep(0.1)
except Exception as e:
print(f"Error monitoring progress: {e}")
processing_thread.join()
# Get the result from the thread
result = thread_result[0]
if reached_99_percent:
completion_progress = {
"percent": 100,
"message": "Finalizing...",
"elapsed": current_progress.get("elapsed", "00:00"),
"left": "00:00"
}
html_content = create_progress_overlay_html(completion_progress, first_frame_base64, show_overlay=True)
yield (
None,
None,
html_content,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
time.sleep(1)
stats_markdown = ""
image_output_path = None
video_output_path = None
# Check if we got a result
if result is not None:
if player_id is not None:
# Full video processing - show video
video_output_path = result.get('output_path')
image_output_path = None
stats_markdown = f"""
## Processing Complete!
- **Target Player ID (Highlighted in Yellow)**: **{player_id}**
- **Processing Mode**: Full video with player tracking
- **Output**: Complete processed video with player tracking
"""
else:
# Limited frame processing - show image
image_output_path = result.get('frame_snippet_path')
video_output_path = None
stats_markdown = f"""
## Player ID Discovery Complete!
- **Processing Mode**: Limited frames (10 frames)
- **Output**: 10th frame snippet with all player IDs visible
- **Instructions**:
1. Examine the image above to see all detected player IDs
2. Choose a player ID from the visible players
3. Enter that ID in the "Player ID" field
4. Click "Start Processing" again for full tracking
"""
else:
stats_markdown = "## Error: Processing failed - no result returned"
# Show image or video based on processing mode
if player_id is not None:
# Full video mode - show video, hide image
yield (
video_output_path,
image_output_path, # Still pass image_output_path but it will be None
create_progress_overlay_html(current_progress, first_frame_base64, show_overlay=False),
gr.update(visible=False),
gr.update(visible=True), # Show video output
gr.update(visible=False), # Hide image output
stats_markdown,
gr.update(visible=True), # Show visualize button for full processing
gr.update(visible=False),
""
)
else:
# Limited frame mode - show image, hide video
yield (
video_output_path, # Will be None
image_output_path,
create_progress_overlay_html(current_progress, first_frame_base64, show_overlay=False),
gr.update(visible=False),
gr.update(visible=False), # Hide video output
gr.update(visible=True), # Show image output
stats_markdown,
gr.update(visible=False), # Hide visualize button for limited frames
gr.update(visible=False),
""
)
except Exception as e:
error_message = f"Error: {str(e)}"
html_content = create_progress_overlay_html(
{"percent": 0, "message": error_message, "elapsed": "00:00", "left": "--:--"},
show_overlay=True
)
yield (
None,
None,
html_content,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False),
""
)
finally:
is_processing = False
with gr.Blocks(title="Football Field Detection", theme=gr.themes.Soft(), css="""
.video-container {
position: relative;
width: 100%;
height: 600px;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.progress-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
}
.video-player, .image-player {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
}
.graph-container {
margin-top: 20px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.hidden {
display: none !important;
}
.visible {
display: block !important;
}
""") as demo:
gr.Markdown(f"""
# Football Field Detection System
**{get_gpu_info()}**
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Upload Video")
video_input = gr.Video(
label="Select Football Video",
sources=["upload"]
)
# Player ID input - changed to Textbox for better flexibility
player_id_input = gr.Textbox(
label="Player ID",
info="Leave empty or type 'discover' for Player Discovery mode (processes 10 frames to show all player IDs).",
value="",
placeholder="discover players"
)
upload_button = gr.Button(
"Start Processing",
variant="primary",
size="lg"
)
visualize_button = gr.Button(
"Visualize Player Stats",
variant="secondary",
size="lg",
visible=False
)
gr.Markdown("### Instructions")
gr.Markdown("""
**Two-Stage Processing Workflow:**
**Stage 1 - Player Discovery (Default):**
1. Upload video
2. Leave Player ID empty or type "discover"
3. Click "Start Processing"
4. Get image with all detected player IDs
**Stage 2 - Full Player Tracking:**
1. Choose a player ID from the discovery image
2. Enter the Player ID number (e.g., 7, 23, etc.)
3. Click "Start Processing"
4. Get full processed video with player tracking
5. Click "Visualize Player Stats" for performance graphs
**Note:** Player Discovery mode only processes the first 10 frames to show available player IDs.
""")
with gr.Column(scale=2):
gr.Markdown("### Video Processing & Output")
with gr.Column(elem_classes="video-container"):
video_output = gr.Video(
label="Processed Video",
interactive=False,
autoplay=True,
elem_classes="video-player",
visible=False # Start hidden
)
image_output = gr.Image(
label="Player ID Discovery Image",
type="filepath",
elem_classes="image-player",
visible=False # Start hidden
)
progress_overlay = gr.HTML(
value=create_progress_overlay_html({"percent": 0, "message": "Ready to process", "elapsed": "00:00", "left": "--:--"}, show_overlay=False),
elem_classes="progress-container",
visible=False
)
stats_output = gr.Markdown("", visible=False)
with gr.Column(visible=False) as graph_section:
gr.Markdown("### Player Statistics Graph")
graph_image = gr.Image(
label="Player Speed & Acceleration Graph",
type="filepath",
visible=False
)
graph_message = gr.Markdown("", visible=False)
upload_button.click(
fn=upload_and_process,
inputs=[video_input, player_id_input],
outputs=[
video_output, # Video output
image_output, # Image output
progress_overlay,
progress_overlay,
video_output,
image_output,
stats_output,
visualize_button,
graph_image,
graph_message
]
)
visualize_button.click(
fn=generate_stats_graph,
inputs=[],
outputs=[
graph_image,
graph_message
]
).then(
fn=lambda: gr.update(visible=True),
inputs=[],
outputs=[graph_section]
)
if __name__ == "__main__":
cleanup_old_files()
print("=" * 60)
print("Football Field Detection System - Two-Stage Processing")
print("=" * 60)
print(f"Upload directory: {UPLOAD_DIR.absolute()}")
print(f"Output directory: {OUTPUT_DIR.absolute()}")
print(f"Main module available: {MAIN_AVAILABLE}")
print(f"Graph module available: {GRAPH_AVAILABLE}")
print("\nTWO-STAGE PROCESSING:")
print("1. Default (Empty Player ID): Process 10 frames → Show all player IDs")
print("2. With Player ID: Full video processing with tracking")
print("=" * 60)
print("Starting Gradio interface...")
print("=" * 60)
demo.launch(share=True, debug=True)