Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,7 @@ for key in ["processed_frames", "slider_value", "processed_video", "start_time"]
|
|
| 30 |
with st.sidebar:
|
| 31 |
st.header("Upload & Settings")
|
| 32 |
source_file = st.file_uploader("Upload image/video", type=["jpg", "jpeg", "png", "bmp", "webp", "mp4"])
|
| 33 |
-
confidence = float(st.slider("Confidence Threshold", 10, 100,
|
| 34 |
fps_options = {
|
| 35 |
"Original FPS": None,
|
| 36 |
"3 FPS": 3,
|
|
@@ -64,8 +64,8 @@ Early wildfire detection using YOLOv8 AI vision model. See detected results abov
|
|
| 64 |
Click on video frames to load and play examples.
|
| 65 |
""")
|
| 66 |
|
| 67 |
-
# Function to create
|
| 68 |
-
def
|
| 69 |
try:
|
| 70 |
orig_bytes = requests.get(orig_url).content
|
| 71 |
proc_bytes = requests.get(proc_url).content
|
|
@@ -76,52 +76,19 @@ def create_synced_video_pair(orig_url, proc_url, pair_id):
|
|
| 76 |
<div style="display: flex; gap: 10px; margin-bottom: 20px;">
|
| 77 |
<div style="flex: 1;">
|
| 78 |
<h4>Original</h4>
|
| 79 |
-
<video
|
| 80 |
<source src="data:video/mp4;base64,{orig_b64}" type="video/mp4">
|
| 81 |
Your browser does not support the video tag.
|
| 82 |
</video>
|
| 83 |
</div>
|
| 84 |
<div style="flex: 1;">
|
| 85 |
<h4>Processed</h4>
|
| 86 |
-
<video
|
| 87 |
<source src="data:video/mp4;base64,{proc_b64}" type="video/mp4">
|
| 88 |
Your browser does not support the video tag.
|
| 89 |
</video>
|
| 90 |
</div>
|
| 91 |
</div>
|
| 92 |
-
<script>
|
| 93 |
-
document.addEventListener('DOMContentLoaded', function() {{
|
| 94 |
-
const origVideo = document.getElementById('orig_{pair_id}');
|
| 95 |
-
const procVideo = document.getElementById('proc_{pair_id}');
|
| 96 |
-
|
| 97 |
-
if (origVideo && procVideo) {{
|
| 98 |
-
origVideo.addEventListener('play', function() {{
|
| 99 |
-
procVideo.currentTime = origVideo.currentTime;
|
| 100 |
-
procVideo.play().catch(e => console.log('Play error:', e));
|
| 101 |
-
}});
|
| 102 |
-
procVideo.addEventListener('play', function() {{
|
| 103 |
-
origVideo.currentTime = procVideo.currentTime;
|
| 104 |
-
origVideo.play().catch(e => console.log('Play error:', e));
|
| 105 |
-
}});
|
| 106 |
-
|
| 107 |
-
origVideo.addEventListener('pause', function() {{
|
| 108 |
-
procVideo.pause();
|
| 109 |
-
}});
|
| 110 |
-
procVideo.addEventListener('pause', function() {{
|
| 111 |
-
origVideo.pause();
|
| 112 |
-
}});
|
| 113 |
-
|
| 114 |
-
origVideo.addEventListener('seeked', function() {{
|
| 115 |
-
procVideo.currentTime = origVideo.currentTime;
|
| 116 |
-
}});
|
| 117 |
-
procVideo.addEventListener('seeked', function() {{
|
| 118 |
-
origVideo.currentTime = procVideo.currentTime;
|
| 119 |
-
}});
|
| 120 |
-
}} else {{
|
| 121 |
-
console.log('Video elements not found for {pair_id}');
|
| 122 |
-
}}
|
| 123 |
-
}});
|
| 124 |
-
</script>
|
| 125 |
"""
|
| 126 |
return html
|
| 127 |
except Exception as e:
|
|
@@ -134,7 +101,7 @@ st.header("Your Results")
|
|
| 134 |
result_cols = st.columns(2)
|
| 135 |
viewer_slot = st.empty()
|
| 136 |
|
| 137 |
-
# Example videos
|
| 138 |
st.header("Example Results")
|
| 139 |
examples = [
|
| 140 |
("LA Example", "LA1.mp4", "LA2.mp4"),
|
|
@@ -144,8 +111,7 @@ for title, orig_file, proc_file in examples:
|
|
| 144 |
st.subheader(title)
|
| 145 |
orig_url = f"https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/{orig_file}"
|
| 146 |
proc_url = f"https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/{proc_file}"
|
| 147 |
-
|
| 148 |
-
video_html = create_synced_video_pair(orig_url, proc_url, pair_id)
|
| 149 |
st.markdown(video_html, unsafe_allow_html=True)
|
| 150 |
|
| 151 |
# Load model
|
|
@@ -180,6 +146,9 @@ if process_button and source_file and model:
|
|
| 180 |
output_fps = fps_options[video_option] if fps_options[video_option] else orig_fps
|
| 181 |
sample_interval = max(1, int(orig_fps / output_fps)) if output_fps else 1
|
| 182 |
|
|
|
|
|
|
|
|
|
|
| 183 |
st.session_state.start_time = time.time()
|
| 184 |
frame_count = 0
|
| 185 |
processed_count = 0
|
|
@@ -198,9 +167,7 @@ if process_button and source_file and model:
|
|
| 198 |
progress = frame_count / total_frames
|
| 199 |
|
| 200 |
if elapsed > 0 and progress > 0:
|
| 201 |
-
# Total estimated time = elapsed time / progress
|
| 202 |
total_estimated_time = elapsed / progress
|
| 203 |
-
# ETA = total estimated time - elapsed time
|
| 204 |
eta = total_estimated_time - elapsed
|
| 205 |
elapsed_str = f"{int(elapsed // 60)}m {int(elapsed % 60)}s"
|
| 206 |
eta_str = f"{int(eta // 60)}m {int(eta % 60)}s" if eta > 0 else "Almost done"
|
|
@@ -209,7 +176,7 @@ if process_button and source_file and model:
|
|
| 209 |
eta_str = "Calculating..."
|
| 210 |
|
| 211 |
progress_bar.progress(min(progress, 1.0))
|
| 212 |
-
progress_text.text(f"Progress: {progress:.1%}
|
| 213 |
|
| 214 |
frame_count += 1
|
| 215 |
success, frame = vidcap.read()
|
|
@@ -222,7 +189,7 @@ if process_button and source_file and model:
|
|
| 222 |
writer = ffmpeg.write_frames(
|
| 223 |
out_path,
|
| 224 |
(width, height),
|
| 225 |
-
fps=
|
| 226 |
codec='libx264',
|
| 227 |
pix_fmt_in='bgr24',
|
| 228 |
pix_fmt_out='yuv420p'
|
|
@@ -240,7 +207,7 @@ if process_button and source_file and model:
|
|
| 240 |
elapsed_final = time.time() - st.session_state.start_time
|
| 241 |
elapsed_final_str = f"{int(elapsed_final // 60)}m {int(elapsed_final % 60)}s"
|
| 242 |
progress_bar.progress(1.0)
|
| 243 |
-
progress_text.text(f"
|
| 244 |
with result_cols[0]:
|
| 245 |
st.video(source_file)
|
| 246 |
with result_cols[1]:
|
|
|
|
| 30 |
with st.sidebar:
|
| 31 |
st.header("Upload & Settings")
|
| 32 |
source_file = st.file_uploader("Upload image/video", type=["jpg", "jpeg", "png", "bmp", "webp", "mp4"])
|
| 33 |
+
confidence = float(st.slider("Confidence Threshold", 10, 100, 20)) / 100
|
| 34 |
fps_options = {
|
| 35 |
"Original FPS": None,
|
| 36 |
"3 FPS": 3,
|
|
|
|
| 64 |
Click on video frames to load and play examples.
|
| 65 |
""")
|
| 66 |
|
| 67 |
+
# Function to create simple video pair HTML
|
| 68 |
+
def create_video_pair(orig_url, proc_url):
|
| 69 |
try:
|
| 70 |
orig_bytes = requests.get(orig_url).content
|
| 71 |
proc_bytes = requests.get(proc_url).content
|
|
|
|
| 76 |
<div style="display: flex; gap: 10px; margin-bottom: 20px;">
|
| 77 |
<div style="flex: 1;">
|
| 78 |
<h4>Original</h4>
|
| 79 |
+
<video width="100%" controls>
|
| 80 |
<source src="data:video/mp4;base64,{orig_b64}" type="video/mp4">
|
| 81 |
Your browser does not support the video tag.
|
| 82 |
</video>
|
| 83 |
</div>
|
| 84 |
<div style="flex: 1;">
|
| 85 |
<h4>Processed</h4>
|
| 86 |
+
<video width="100%" controls>
|
| 87 |
<source src="data:video/mp4;base64,{proc_b64}" type="video/mp4">
|
| 88 |
Your browser does not support the video tag.
|
| 89 |
</video>
|
| 90 |
</div>
|
| 91 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
"""
|
| 93 |
return html
|
| 94 |
except Exception as e:
|
|
|
|
| 101 |
result_cols = st.columns(2)
|
| 102 |
viewer_slot = st.empty()
|
| 103 |
|
| 104 |
+
# Example videos (LA before T)
|
| 105 |
st.header("Example Results")
|
| 106 |
examples = [
|
| 107 |
("LA Example", "LA1.mp4", "LA2.mp4"),
|
|
|
|
| 111 |
st.subheader(title)
|
| 112 |
orig_url = f"https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/{orig_file}"
|
| 113 |
proc_url = f"https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/{proc_file}"
|
| 114 |
+
video_html = create_video_pair(orig_url, proc_url)
|
|
|
|
| 115 |
st.markdown(video_html, unsafe_allow_html=True)
|
| 116 |
|
| 117 |
# Load model
|
|
|
|
| 146 |
output_fps = fps_options[video_option] if fps_options[video_option] else orig_fps
|
| 147 |
sample_interval = max(1, int(orig_fps / output_fps)) if output_fps else 1
|
| 148 |
|
| 149 |
+
# Set fixed output FPS to 2 (500ms per frame = 2 FPS)
|
| 150 |
+
fixed_output_fps = 2
|
| 151 |
+
|
| 152 |
st.session_state.start_time = time.time()
|
| 153 |
frame_count = 0
|
| 154 |
processed_count = 0
|
|
|
|
| 167 |
progress = frame_count / total_frames
|
| 168 |
|
| 169 |
if elapsed > 0 and progress > 0:
|
|
|
|
| 170 |
total_estimated_time = elapsed / progress
|
|
|
|
| 171 |
eta = total_estimated_time - elapsed
|
| 172 |
elapsed_str = f"{int(elapsed // 60)}m {int(elapsed % 60)}s"
|
| 173 |
eta_str = f"{int(eta // 60)}m {int(eta % 60)}s" if eta > 0 else "Almost done"
|
|
|
|
| 176 |
eta_str = "Calculating..."
|
| 177 |
|
| 178 |
progress_bar.progress(min(progress, 1.0))
|
| 179 |
+
progress_text.text(f"Progress: {progress:.1%}\nElapsed: {elapsed_str}\nETA: {eta_str}")
|
| 180 |
|
| 181 |
frame_count += 1
|
| 182 |
success, frame = vidcap.read()
|
|
|
|
| 189 |
writer = ffmpeg.write_frames(
|
| 190 |
out_path,
|
| 191 |
(width, height),
|
| 192 |
+
fps=fixed_output_fps, # Fixed at 2 FPS (500ms per frame)
|
| 193 |
codec='libx264',
|
| 194 |
pix_fmt_in='bgr24',
|
| 195 |
pix_fmt_out='yuv420p'
|
|
|
|
| 207 |
elapsed_final = time.time() - st.session_state.start_time
|
| 208 |
elapsed_final_str = f"{int(elapsed_final // 60)}m {int(elapsed_final % 60)}s"
|
| 209 |
progress_bar.progress(1.0)
|
| 210 |
+
progress_text.text(f"Progress: 100%\nElapsed: {elapsed_final_str}\nETA: 0m 0s")
|
| 211 |
with result_cols[0]:
|
| 212 |
st.video(source_file)
|
| 213 |
with result_cols[1]:
|