Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
+
import numpy as np
|
| 5 |
+
from collections import defaultdict
|
| 6 |
+
|
| 7 |
+
# Import your existing functions here
|
| 8 |
+
# from quadball_tracker import detect_balls, update_trackers, check_events, get_quadrant, COLOR_RANGES
|
| 9 |
+
|
| 10 |
+
def process_video(video_path):
|
| 11 |
+
cap = cv2.VideoCapture(video_path)
|
| 12 |
+
|
| 13 |
+
frame_count = 0
|
| 14 |
+
ball_trackers = defaultdict(lambda: defaultdict(dict))
|
| 15 |
+
events = []
|
| 16 |
+
|
| 17 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 18 |
+
|
| 19 |
+
progress_bar = st.progress(0)
|
| 20 |
+
status_text = st.empty()
|
| 21 |
+
|
| 22 |
+
while cap.isOpened():
|
| 23 |
+
ret, frame = cap.read()
|
| 24 |
+
if not ret:
|
| 25 |
+
break
|
| 26 |
+
|
| 27 |
+
frame_count += 1
|
| 28 |
+
|
| 29 |
+
progress_bar.progress(frame_count / total_frames)
|
| 30 |
+
status_text.text(f"Processing frame {frame_count}/{total_frames}")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
balls = detect_balls(frame)
|
| 34 |
+
update_trackers(ball_trackers, balls, frame_count)
|
| 35 |
+
new_events = check_events(ball_trackers, frame_count)
|
| 36 |
+
events.extend(new_events)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Error processing frame {frame_count}: {str(e)}")
|
| 39 |
+
|
| 40 |
+
cap.release()
|
| 41 |
+
return events
|
| 42 |
+
|
| 43 |
+
st.title('QuadBall Tracker 🔴🟢🔵🟡')
|
| 44 |
+
|
| 45 |
+
st.write("""
|
| 46 |
+
Welcome to QuadBall Tracker! This application uses computer vision to track colored balls
|
| 47 |
+
across different quadrants in a video. Upload your video to get started!
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
|
| 51 |
+
|
| 52 |
+
if uploaded_file is not None:
|
| 53 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 54 |
+
tfile.write(uploaded_file.read())
|
| 55 |
+
|
| 56 |
+
st.video(tfile.name)
|
| 57 |
+
|
| 58 |
+
if st.button('Process Video'):
|
| 59 |
+
events = process_video(tfile.name)
|
| 60 |
+
|
| 61 |
+
st.success('Video processing complete!')
|
| 62 |
+
|
| 63 |
+
st.subheader('Event Log')
|
| 64 |
+
for event in events:
|
| 65 |
+
st.write(f"Time: {event['time']}, Quadrant: {event['quadrant']}, "
|
| 66 |
+
f"Color: {event['color']}, Type: {event['type']}")
|
| 67 |
+
|
| 68 |
+
# Create a DataFrame for easy filtering
|
| 69 |
+
import pandas as pd
|
| 70 |
+
df = pd.DataFrame(events)
|
| 71 |
+
|
| 72 |
+
st.subheader('Filter Events')
|
| 73 |
+
color_filter = st.multiselect('Select colors', df['color'].unique())
|
| 74 |
+
quadrant_filter = st.multiselect('Select quadrants', df['quadrant'].unique())
|
| 75 |
+
|
| 76 |
+
filtered_df = df
|
| 77 |
+
if color_filter:
|
| 78 |
+
filtered_df = filtered_df[filtered_df['color'].isin(color_filter)]
|
| 79 |
+
if quadrant_filter:
|
| 80 |
+
filtered_df = filtered_df[filtered_df['quadrant'].isin(quadrant_filter)]
|
| 81 |
+
|
| 82 |
+
st.dataframe(filtered_df)
|
| 83 |
+
|
| 84 |
+
st.sidebar.title('About')
|
| 85 |
+
st.sidebar.info('This application uses computer vision to track colored balls across video quadrants. '
|
| 86 |
+
'Upload a video to see it in action!')
|