-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
173 lines (136 loc) · 6.22 KB
/
app.py
File metadata and controls
173 lines (136 loc) · 6.22 KB
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
# import streamlit as st
# from main import run_analysis
# import os
# import time # for unique timestamp
# st.title("🎾 Tennis Video Analysis")
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# temp_dir = os.path.join(BASE_DIR, "temp")
# output_dir = os.path.join(BASE_DIR, "output_videos")
# os.makedirs(temp_dir, exist_ok=True)
# os.makedirs(output_dir, exist_ok=True)
# uploaded_file = st.file_uploader("Upload your tennis video (mp4)", type=["mp4"])
# if uploaded_file:
# input_path = os.path.join(temp_dir, uploaded_file.name)
# # Use timestamp to make output file unique
# output_path = os.path.join(output_dir, f"streamlit_output_{int(time.time())}.mp4")
# # Save uploaded file
# with open(input_path, "wb") as f:
# f.write(uploaded_file.getbuffer())
# st.info("Running analysis... This might take a while depending on your video length.")
# try:
# with st.spinner("Processing video..."):
# output_video_path = run_analysis(input_path, output_path)
# if os.path.exists(output_video_path):
# size_mb = os.path.getsize(output_video_path) / (1024 * 1024)
# st.success(f"Analysis complete! Output video size: {size_mb:.2f} MB")
# st.video(output_video_path) # Streamlit will serve fresh video
# else:
# st.error("Output video file not found after analysis.")
# except Exception as e:
# st.error(f"Error during analysis: {e}")
# else:
# st.info("Please upload a tennis video file to get started.")
import streamlit as st
from main import run_analysis
import os
import time
import cv2
st.title("🎾 Tennis Video Analysis")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
temp_dir = os.path.join(BASE_DIR, "temp")
output_dir = os.path.join(BASE_DIR, "output_videos")
os.makedirs(temp_dir, exist_ok=True)
os.makedirs(output_dir, exist_ok=True)
def verify_video_file(video_path):
"""Verify that the video file is valid and playable"""
try:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return False, "Cannot open video file"
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
duration = frame_count / fps if fps > 0 else 0
cap.release()
if frame_count == 0:
return False, "Video has no frames"
if duration == 0:
return False, "Video has zero duration"
return True, f"Video OK: {frame_count} frames, {fps:.2f} fps, {duration:.2f}s"
except Exception as e:
return False, f"Error reading video: {str(e)}"
uploaded_file = st.file_uploader("Upload your tennis video (mp4)", type=["mp4"])
if uploaded_file:
input_path = os.path.join(temp_dir, uploaded_file.name)
# Use timestamp to make output file unique
timestamp = int(time.time())
output_path = os.path.join(output_dir, f"streamlit_output_{timestamp}.mp4")
# Save uploaded file
with open(input_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Verify input video
st.info("🔍 Verifying uploaded video...")
is_valid, message = verify_video_file(input_path)
if not is_valid:
st.error(f"❌ Video validation failed: {message}")
st.stop()
else:
st.success(f"✅ {message}")
st.info("🚀 Running analysis... This might take a while depending on your video length.")
try:
with st.spinner("Processing video..."):
output_video_path = run_analysis(input_path, output_path)
if output_video_path and os.path.exists(output_video_path):
# Verify output video
is_valid_output, output_message = verify_video_file(output_video_path)
if is_valid_output:
size_mb = os.path.getsize(output_video_path) / (1024 * 1024)
st.success(f"🎉 Analysis complete! Output video size: {size_mb:.2f} MB")
st.info(f"📊 {output_message}")
# Display video
st.video(output_video_path)
# Provide download option
with open(output_video_path, "rb") as file:
st.download_button(
label="📥 Download Processed Video",
data=file.read(),
file_name=f"tennis_analysis_{timestamp}.mp4",
mime="video/mp4"
)
else:
st.error(f"❌ Output video validation failed: {output_message}")
# Show file info for debugging
if os.path.exists(output_video_path):
size_mb = os.path.getsize(output_video_path) / (1024 * 1024)
st.error(f"Output file exists but is invalid. Size: {size_mb:.2f} MB")
else:
st.error("Output video file was not created.")
else:
st.error("❌ Analysis failed - no output video was generated.")
except Exception as e:
st.error(f"❌ Error during analysis: {e}")
# Show debugging info
st.error("🔧 Debug Information:")
st.code(str(e))
# Check if partial output exists
if os.path.exists(output_path):
size_mb = os.path.getsize(output_path) / (1024 * 1024)
st.info(f"Partial output file exists: {size_mb:.2f} MB")
else:
st.info("📤 Please upload a tennis video file to get started.")
# Add some usage instructions
with st.expander("ℹ️ How to use this app"):
st.markdown("""
1. **Upload Video**: Choose an MP4 tennis video file
2. **Wait for Processing**: The analysis can take several minutes
3. **View Results**: Watch the processed video with:
- Player tracking boxes
- Ball tracking
- Court line detection
- Mini court visualization
- Player statistics
4. **Download**: Save the processed video to your device
**Tips for best results:**
- Use clear, well-lit tennis videos
- Ensure players and ball are visible
- MP4 format works best
""")