Spaces:
Sleeping
Sleeping
Merge branch 'main' of github.com:Break-Into-Data/workshop_week1_private
Browse files- .gitignore +1 -0
- ball_app.py +110 -0
- requirements.txt +4 -0
.gitignore
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
.DS_Store
|
|
|
|
| 2 |
__pycache__/
|
| 3 |
datasets/
|
| 4 |
runs/
|
|
|
|
| 1 |
.DS_Store
|
| 2 |
+
venv/
|
| 3 |
__pycache__/
|
| 4 |
datasets/
|
| 5 |
runs/
|
ball_app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import tempfile
|
| 4 |
+
import cv2
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
return YOLO("models/soccer_ball.pt")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def process_video(video_path):
|
| 16 |
+
cap = cv2.VideoCapture(video_path)
|
| 17 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 18 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 19 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 20 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 21 |
+
|
| 22 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 23 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 24 |
+
out = cv2.VideoWriter(temp_file.name, fourcc, fps, (width, height))
|
| 25 |
+
|
| 26 |
+
progress_text = "Processing video... Please wait."
|
| 27 |
+
progress_bar = st.progress(0)
|
| 28 |
+
status_text = st.empty()
|
| 29 |
+
time_text = st.empty()
|
| 30 |
+
|
| 31 |
+
start_time = time.time()
|
| 32 |
+
|
| 33 |
+
for frame_num in range(total_frames):
|
| 34 |
+
ret, frame = cap.read()
|
| 35 |
+
if not ret:
|
| 36 |
+
break
|
| 37 |
+
|
| 38 |
+
results = model(frame)
|
| 39 |
+
annotated_frame = results[0].plot()
|
| 40 |
+
out.write(annotated_frame)
|
| 41 |
+
|
| 42 |
+
# Update progress
|
| 43 |
+
progress = (frame_num + 1) / total_frames
|
| 44 |
+
elapsed_time = time.time() - start_time
|
| 45 |
+
estimated_total_time = elapsed_time / progress
|
| 46 |
+
remaining_time = estimated_total_time - elapsed_time
|
| 47 |
+
|
| 48 |
+
progress_bar.progress(progress)
|
| 49 |
+
status_text.text(f"Processing frame {frame_num+1}/{total_frames}")
|
| 50 |
+
time_text.text(f"Elapsed time: {elapsed_time:.2f}s | Estimated time remaining: {remaining_time:.2f}s")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
cap.release()
|
| 54 |
+
out.release()
|
| 55 |
+
progress_bar.empty()
|
| 56 |
+
status_text.text(f"Processed {total_frames} frames")
|
| 57 |
+
time_text.text(f"Total time: {time.time() - start_time:.2f}s")
|
| 58 |
+
return temp_file.name
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
model = load_model()
|
| 62 |
+
st.title("Soccer Ball Detection App")
|
| 63 |
+
|
| 64 |
+
# Sidebar for options
|
| 65 |
+
st.sidebar.header("Options")
|
| 66 |
+
video_option = st.sidebar.radio("Choose video source:", ("Use preset video", "Upload video"))
|
| 67 |
+
|
| 68 |
+
if video_option == "Upload video":
|
| 69 |
+
uploaded_file = st.sidebar.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
|
| 70 |
+
if uploaded_file is not None:
|
| 71 |
+
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 72 |
+
tfile.write(uploaded_file.read())
|
| 73 |
+
video_path = tfile.name
|
| 74 |
+
else:
|
| 75 |
+
preset_videos = {
|
| 76 |
+
"Ronaldo": "preset_videos/Ronaldo.mp4",
|
| 77 |
+
"Sancho": "preset_videos/CityUtdR video.mp4",
|
| 78 |
+
"Messi": "preset_videos/Messi.mp4",
|
| 79 |
+
}
|
| 80 |
+
selected_video = st.sidebar.selectbox("Select a preset video", list(preset_videos.keys()))
|
| 81 |
+
video_path = preset_videos[selected_video]
|
| 82 |
+
|
| 83 |
+
if 'video_path' in locals():
|
| 84 |
+
st.header("Original Video")
|
| 85 |
+
st.video(video_path)
|
| 86 |
+
|
| 87 |
+
if st.button("Detect"):
|
| 88 |
+
with st.spinner("Processing video..."):
|
| 89 |
+
processed_video_path = process_video(video_path)
|
| 90 |
+
|
| 91 |
+
st.success("Video processing complete!")
|
| 92 |
+
st.header("Processed Video")
|
| 93 |
+
st.video(processed_video_path)
|
| 94 |
+
|
| 95 |
+
# Add download link
|
| 96 |
+
with open(processed_video_path, "rb") as file:
|
| 97 |
+
btn = st.download_button(
|
| 98 |
+
label="Download Video",
|
| 99 |
+
data=file,
|
| 100 |
+
file_name="processed_video.mp4",
|
| 101 |
+
mime="video/mp4"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Clean up temporary files
|
| 105 |
+
os.unlink(processed_video_path)
|
| 106 |
+
if video_option == "Upload video":
|
| 107 |
+
os.unlink(video_path)
|
| 108 |
+
|
| 109 |
+
st.sidebar.markdown("---")
|
| 110 |
+
st.sidebar.write("Developed with ❤️ by Break Into Data")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
streamlit
|