File size: 2,836 Bytes
26784f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import tempfile
import numpy as np
from collections import defaultdict

# Import your existing functions here
# from quadball_tracker import detect_balls, update_trackers, check_events, get_quadrant, COLOR_RANGES

def process_video(video_path):
    cap = cv2.VideoCapture(video_path)
    
    frame_count = 0
    ball_trackers = defaultdict(lambda: defaultdict(dict))
    events = []

    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
    progress_bar = st.progress(0)
    status_text = st.empty()

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        frame_count += 1
        
        progress_bar.progress(frame_count / total_frames)
        status_text.text(f"Processing frame {frame_count}/{total_frames}")
        
        try:
            balls = detect_balls(frame)
            update_trackers(ball_trackers, balls, frame_count)
            new_events = check_events(ball_trackers, frame_count)
            events.extend(new_events)
        except Exception as e:
            st.error(f"Error processing frame {frame_count}: {str(e)}")
        
    cap.release()
    return events

st.title('QuadBall Tracker 🔴🟢🔵🟡')

st.write("""
Welcome to QuadBall Tracker! This application uses computer vision to track colored balls 
across different quadrants in a video. Upload your video to get started!
""")

uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])

if uploaded_file is not None:
    tfile = tempfile.NamedTemporaryFile(delete=False) 
    tfile.write(uploaded_file.read())
    
    st.video(tfile.name)
    
    if st.button('Process Video'):
        events = process_video(tfile.name)
        
        st.success('Video processing complete!')
        
        st.subheader('Event Log')
        for event in events:
            st.write(f"Time: {event['time']}, Quadrant: {event['quadrant']}, "
                     f"Color: {event['color']}, Type: {event['type']}")
        
        # Create a DataFrame for easy filtering
        import pandas as pd
        df = pd.DataFrame(events)
        
        st.subheader('Filter Events')
        color_filter = st.multiselect('Select colors', df['color'].unique())
        quadrant_filter = st.multiselect('Select quadrants', df['quadrant'].unique())
        
        filtered_df = df
        if color_filter:
            filtered_df = filtered_df[filtered_df['color'].isin(color_filter)]
        if quadrant_filter:
            filtered_df = filtered_df[filtered_df['quadrant'].isin(quadrant_filter)]
        
        st.dataframe(filtered_df)

st.sidebar.title('About')
st.sidebar.info('This application uses computer vision to track colored balls across video quadrants. '
                'Upload a video to see it in action!')