Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +118 -0
- preset_videos/soccer.mp4 +3 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
preset_videos/soccer.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
import time
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
from huggingface_hub import hf_hub_url, cached_download
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
|
| 13 |
+
repo_id = 'navoditamathur/Soccer_yolo'
|
| 14 |
+
model_filename = 'soccer_ball.pt'
|
| 15 |
+
# Create a URL for the model file on the Hugging Face Hub
|
| 16 |
+
model_url = hf_hub_url(repo_id, model_filename)
|
| 17 |
+
|
| 18 |
+
# Download the model file from the Hub and cache it locally
|
| 19 |
+
cached_model_path = cached_download(model_url)
|
| 20 |
+
|
| 21 |
+
# Rename the file to have a .pt extension
|
| 22 |
+
new_cached_model_path = f"{cached_model_path}.pt"
|
| 23 |
+
os.rename(cached_model_path, new_cached_model_path)
|
| 24 |
+
|
| 25 |
+
print(f"Downloaded model to {new_cached_model_path}")
|
| 26 |
+
|
| 27 |
+
# Load the model using YOLO from the cached model file
|
| 28 |
+
return YOLO(new_cached_model_path)
|
| 29 |
+
|
| 30 |
+
def process_video(video_path, output_path):
|
| 31 |
+
cap = cv2.VideoCapture(video_path)
|
| 32 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 33 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 34 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 35 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 36 |
+
|
| 37 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 38 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 39 |
+
|
| 40 |
+
progress_text = "Please wait..."
|
| 41 |
+
progress_bar = st.progress(0)
|
| 42 |
+
progress_bar.text(progress_text)
|
| 43 |
+
|
| 44 |
+
status_text = st.empty()
|
| 45 |
+
time_text = st.empty()
|
| 46 |
+
|
| 47 |
+
start_time = time.time()
|
| 48 |
+
|
| 49 |
+
for i in range(total_frames):
|
| 50 |
+
ret, frame = cap.read()
|
| 51 |
+
if not ret:
|
| 52 |
+
break
|
| 53 |
+
|
| 54 |
+
boxes = model(frame)
|
| 55 |
+
annotated_frame = boxes[0].plot()
|
| 56 |
+
out.write(annotated_frame)
|
| 57 |
+
|
| 58 |
+
progress = (i + 1) / total_frames
|
| 59 |
+
progress_bar.progress(progress)
|
| 60 |
+
|
| 61 |
+
elasped_time = time.time() - start_time
|
| 62 |
+
time_per_frame = elasped_time / (i + 1)
|
| 63 |
+
remaining_time = (total_frames - (i + 1)) * time_per_frame
|
| 64 |
+
|
| 65 |
+
status_text.text(f"Processing frame {i + 1} of {total_frames}")
|
| 66 |
+
time_text.text(f"Time remaining: {remaining_time:.2f} seconds")
|
| 67 |
+
cap.release()
|
| 68 |
+
out.release()
|
| 69 |
+
status_text.text("Video processing completed.")
|
| 70 |
+
progress_bar.empty()
|
| 71 |
+
time_text.empty()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
model = load_model()
|
| 75 |
+
st.title("Soccer Ball Detection App")
|
| 76 |
+
|
| 77 |
+
# Sidebar for options
|
| 78 |
+
st.sidebar.header("Options")
|
| 79 |
+
video_option = st.sidebar.radio("Choose video source:", ("Use preset video", "Upload video"))
|
| 80 |
+
|
| 81 |
+
if video_option == "Upload video":
|
| 82 |
+
uploaded_file = st.sidebar.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
|
| 83 |
+
if uploaded_file is not None:
|
| 84 |
+
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 85 |
+
tfile.write(uploaded_file.read())
|
| 86 |
+
video_path = tfile.name
|
| 87 |
+
else:
|
| 88 |
+
preset_videos = {
|
| 89 |
+
"Soccer Video": "preset_videos/soccer.mp4"
|
| 90 |
+
}
|
| 91 |
+
selected_video = st.sidebar.selectbox("Select a preset video", list(preset_videos.keys()))
|
| 92 |
+
video_path = preset_videos[selected_video]
|
| 93 |
+
|
| 94 |
+
if 'video_path' in locals():
|
| 95 |
+
st.header("Original Video")
|
| 96 |
+
st.video(video_path)
|
| 97 |
+
|
| 98 |
+
if st.button("Detect"):
|
| 99 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 100 |
+
output_path = temp_file.name
|
| 101 |
+
process_video(video_path, output_path)
|
| 102 |
+
with open(output_path, 'rb') as video_file:
|
| 103 |
+
video_bytes = video_file.read()
|
| 104 |
+
st.header("Detected Video")
|
| 105 |
+
|
| 106 |
+
# Debugging: Display video size
|
| 107 |
+
st.write(f"Processed video size: {len(video_bytes)} bytes")
|
| 108 |
+
|
| 109 |
+
if len(video_bytes) > 0:
|
| 110 |
+
st.video(video_bytes)
|
| 111 |
+
|
| 112 |
+
# Generate a download button
|
| 113 |
+
btn = st.download_button(
|
| 114 |
+
label="Download Processed Video",
|
| 115 |
+
data=video_bytes,
|
| 116 |
+
file_name="processed_video.mp4",
|
| 117 |
+
mime="video/mp4"
|
| 118 |
+
)
|
preset_videos/soccer.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e6a565de9d3440544fb3592e952ce13e4fa3eadf1cc3f3feb785a89f34b756d1
|
| 3 |
+
size 6412733
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.25.0
|
| 2 |
+
torch==2.0.1
|
| 3 |
+
ultralytics==8.0.109
|
| 4 |
+
opencv-python==4.8.0.76
|
| 5 |
+
Pillow==9.4.0
|