File size: 7,625 Bytes
8d89bb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Streamlit Review Dashboard
import streamlit as st
import pandas as pd
import json
import os
from pathlib import Path
from typing import List, Dict

try:
    import plotly.express as px
    import plotly.graph_objects as go
    PLOTLY_AVAILABLE = True
except ImportError:
    PLOTLY_AVAILABLE = False
    st.warning("Plotly not available. Some visualizations will be disabled.")


def load_events(json_path: str) -> List[Dict]:
    """Load events from JSON file"""
    if not os.path.exists(json_path):
        return []
    
    with open(json_path, 'r') as f:
        data = json.load(f)
        return data.get('events', [])


def load_checkpoints(checkpoint_dir: str) -> List[str]:
    """List available checkpoint files"""
    if not os.path.exists(checkpoint_dir):
        return []
    
    checkpoints = sorted([
        f for f in os.listdir(checkpoint_dir)
        if f.startswith('checkpoint_') and f.endswith('.json')
    ])
    return checkpoints


def render_event_summary(events: List[Dict]):
    """Render event summary statistics"""
    if not events:
        st.info("No events loaded")
        return
    
    # Count events by type
    event_counts = {}
    for event in events:
        event_type = event.get('type', 'unknown')
        event_counts[event_type] = event_counts.get(event_type, 0) + 1
    
    # Display metrics
    col1, col2, col3, col4, col5 = st.columns(5)
    
    with col1:
        st.metric("Total Events", len(events))
    with col2:
        st.metric("Passes", event_counts.get('pass', 0))
    with col3:
        st.metric("Dribbles", event_counts.get('dribble', 0))
    with col4:
        st.metric("Shots", event_counts.get('shot', 0))
    with col5:
        st.metric("Recoveries", event_counts.get('recovery', 0))


def render_event_timeline(events: List[Dict]):
    """Render event timeline visualization"""
    if not events:
        return
    
    if not PLOTLY_AVAILABLE:
        st.info("Plotly not available for timeline visualization")
        return
    
    # Prepare data
    timeline_data = []
    for event in events:
        timeline_data.append({
            'frame': event.get('start_frame', 0),
            'type': event.get('type', 'unknown'),
            'confidence': event.get('confidence', 0.0)
        })
    
    df = pd.DataFrame(timeline_data)
    
    # Create timeline plot
    fig = px.scatter(
        df,
        x='frame',
        y='type',
        color='confidence',
        color_continuous_scale='Viridis',
        title='Event Timeline',
        labels={'frame': 'Frame Number', 'type': 'Event Type'}
    )
    
    st.plotly_chart(fig, use_container_width=True)


def render_event_table(events: List[Dict]):
    """Render events in a table"""
    if not events:
        return
    
    # Convert to DataFrame
    table_data = []
    for event in events:
        table_data.append({
            'ID': event.get('id', ''),
            'Type': event.get('type', ''),
            'Start Frame': event.get('start_frame', 0),
            'End Frame': event.get('end_frame', 0),
            'Confidence': f"{event.get('confidence', 0.0):.2f}",
            'Players': len(event.get('involved_players', []))
        })
    
    df = pd.DataFrame(table_data)
    st.dataframe(df, use_container_width=True)


def render_pitch_map(events: List[Dict]):
    """Render events on pitch map"""
    if not events:
        return
    
    if not PLOTLY_AVAILABLE:
        st.info("Plotly not available for pitch map visualization")
        return
    
    # Extract locations
    pass_locs = []
    shot_locs = []
    dribble_locs = []
    
    for event in events:
        event_type = event.get('type', '')
        start_loc = event.get('start_location', {})
        end_loc = event.get('end_location', {})
        
        if event_type == 'pass':
            pass_locs.append({
                'x': [start_loc.get('x', 0), end_loc.get('x', 0)],
                'y': [start_loc.get('y', 0), end_loc.get('y', 0)]
            })
        elif event_type == 'shot':
            shot_locs.append({
                'x': end_loc.get('x', 0),
                'y': end_loc.get('y', 0)
            })
        elif event_type == 'dribble':
            dribble_locs.append({
                'x': end_loc.get('x', 0),
                'y': end_loc.get('y', 0)
            })
    
    # Create pitch visualization
    fig = go.Figure()
    
    # Draw pitch outline (105m x 68m)
    pitch_x = [-52.5, 52.5, 52.5, -52.5, -52.5]
    pitch_y = [-34, -34, 34, 34, -34]
    fig.add_trace(go.Scatter(
        x=pitch_x,
        y=pitch_y,
        mode='lines',
        name='Pitch',
        line=dict(color='green', width=2)
    ))
    
    # Draw passes
    for pass_loc in pass_locs:
        fig.add_trace(go.Scatter(
            x=pass_loc['x'],
            y=pass_loc['y'],
            mode='lines+markers',
            name='Pass',
            line=dict(color='blue', width=1),
            marker=dict(size=5)
        ))
    
    # Draw shots
    if shot_locs:
        shot_x = [loc['x'] for loc in shot_locs]
        shot_y = [loc['y'] for loc in shot_locs]
        fig.add_trace(go.Scatter(
            x=shot_x,
            y=shot_y,
            mode='markers',
            name='Shot',
            marker=dict(size=10, color='red', symbol='star')
        ))
    
    # Draw dribbles
    if dribble_locs:
        dribble_x = [loc['x'] for loc in dribble_locs]
        dribble_y = [loc['y'] for loc in dribble_locs]
        fig.add_trace(go.Scatter(
            x=dribble_x,
            y=dribble_y,
            mode='markers',
            name='Dribble',
            marker=dict(size=8, color='orange', symbol='circle')
        ))
    
    fig.update_layout(
        title='Event Map on Pitch',
        xaxis_title='X (meters)',
        yaxis_title='Y (meters)',
        showlegend=True,
        width=800,
        height=600
    )
    
    st.plotly_chart(fig, use_container_width=True)


def main():
    """Main Streamlit app"""
    st.set_page_config(page_title="Soccer Analysis Dashboard", layout="wide")
    st.title("Soccer Analysis Pipeline - Review Dashboard")
    
    # Sidebar for file selection
    st.sidebar.header("Data Selection")
    
    output_dir = st.sidebar.text_input("Output Directory", value="data/output")
    
    # Load events
    json_path = os.path.join(output_dir, "events.json")
    events = load_events(json_path)
    
    # Checkpoint selection
    checkpoint_dir = os.path.join(output_dir, "checkpoints")
    checkpoints = load_checkpoints(checkpoint_dir)
    
    if checkpoints:
        st.sidebar.subheader("Checkpoints")
        selected_checkpoint = st.sidebar.selectbox(
            "Select Checkpoint",
            options=checkpoints,
            index=len(checkpoints) - 1 if checkpoints else 0
        )
        
        if selected_checkpoint:
            checkpoint_path = os.path.join(checkpoint_dir, selected_checkpoint)
            checkpoint_events = load_events(checkpoint_path)
            if checkpoint_events:
                st.sidebar.info(f"Loaded {len(checkpoint_events)} events from checkpoint")
    
    # Main content
    if events:
        st.header("Event Summary")
        render_event_summary(events)
        
        st.header("Event Timeline")
        render_event_timeline(events)
        
        st.header("Pitch Map")
        render_pitch_map(events)
        
        st.header("Event Details")
        render_event_table(events)
    else:
        st.warning(f"No events found at {json_path}")
        st.info("Run the main pipeline to generate events")


if __name__ == "__main__":
    main()