Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from ultralytics import YOLO
|
|
8 |
import requests
|
9 |
|
10 |
###############################################################################
|
11 |
-
# Helper
|
12 |
###############################################################################
|
13 |
def show_autoplay_video(video_bytes: bytes, title: str = "Video"):
|
14 |
if not video_bytes:
|
@@ -25,7 +25,7 @@ def show_autoplay_video(video_bytes: bytes, title: str = "Video"):
|
|
25 |
st.markdown(video_html, unsafe_allow_html=True)
|
26 |
|
27 |
###############################################################################
|
28 |
-
# Session state initialization
|
29 |
###############################################################################
|
30 |
if "processed_frames" not in st.session_state:
|
31 |
st.session_state["processed_frames"] = []
|
@@ -35,7 +35,7 @@ if "shortened_video_ready" not in st.session_state:
|
|
35 |
st.session_state["shortened_video_ready"] = False
|
36 |
|
37 |
###############################################################################
|
38 |
-
# Configure YOLO model path and page layout
|
39 |
###############################################################################
|
40 |
model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt'
|
41 |
st.set_page_config(
|
@@ -46,11 +46,10 @@ st.set_page_config(
|
|
46 |
)
|
47 |
|
48 |
###############################################################################
|
49 |
-
# SIDEBAR: Video input options, confidence, sampling options, and example selection
|
50 |
###############################################################################
|
51 |
with st.sidebar:
|
52 |
st.header("Video Input Options")
|
53 |
-
# Option to select an example pair; "None" means use an uploaded file.
|
54 |
example_option = st.selectbox(
|
55 |
"Select Example Pair (optional)",
|
56 |
["None", "T Example", "LA Example"]
|
@@ -66,8 +65,6 @@ with st.sidebar:
|
|
66 |
)
|
67 |
progress_text = st.empty()
|
68 |
progress_bar = st.progress(0)
|
69 |
-
# Placeholder for the download button so it persists.
|
70 |
-
download_placeholder = st.empty()
|
71 |
|
72 |
###############################################################################
|
73 |
# MAIN TITLE
|
@@ -75,7 +72,7 @@ with st.sidebar:
|
|
75 |
st.title("Fire Detection: Original vs. Processed Video")
|
76 |
|
77 |
###############################################################################
|
78 |
-
# Load YOLO model
|
79 |
###############################################################################
|
80 |
try:
|
81 |
model = YOLO(model_path)
|
@@ -90,7 +87,7 @@ original_video_data = None
|
|
90 |
processed_video_data = None # For example pairs
|
91 |
|
92 |
if example_option != "None":
|
93 |
-
#
|
94 |
if example_option == "T Example":
|
95 |
orig_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T1.mp4"
|
96 |
proc_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T2.mpg"
|
@@ -103,16 +100,17 @@ if example_option != "None":
|
|
103 |
except Exception as ex:
|
104 |
st.error("Error loading example videos. Check your URLs.")
|
105 |
else:
|
106 |
-
# No example selected. If a file is uploaded, use it.
|
107 |
if source_file:
|
108 |
file_type = source_file.type.split('/')[0]
|
109 |
if file_type == 'image':
|
|
|
110 |
original_image = PIL.Image.open(source_file)
|
111 |
buf = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
112 |
original_image.save(buf.name, format="PNG")
|
113 |
with open(buf.name, "rb") as f:
|
114 |
original_video_data = f.read()
|
115 |
else:
|
|
|
116 |
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
117 |
tfile.write(source_file.read())
|
118 |
tfile.flush()
|
@@ -124,7 +122,7 @@ else:
|
|
124 |
st.info("Please select an example pair or upload a video file.")
|
125 |
|
126 |
###############################################################################
|
127 |
-
# Layout: Two columns for Original and Processed videos
|
128 |
###############################################################################
|
129 |
col1, col2 = st.columns(2)
|
130 |
|
@@ -135,25 +133,27 @@ with col1:
|
|
135 |
else:
|
136 |
st.info("No original video available.")
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
else:
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
150 |
|
151 |
###############################################################################
|
152 |
# DETECTION: Process the uploaded video if no example is selected.
|
153 |
###############################################################################
|
154 |
if example_option == "None" and source_file and source_file.type.split('/')[0] != 'image':
|
155 |
if st.sidebar.button("Let's Detect Wildfire"):
|
156 |
-
# Reset previous
|
157 |
st.session_state["processed_frames"] = []
|
158 |
st.session_state["shortened_video_data"] = None
|
159 |
st.session_state["shortened_video_ready"] = False
|
@@ -186,18 +186,22 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
|
|
186 |
sample_interval = 1
|
187 |
output_fps = orig_fps
|
188 |
|
|
|
189 |
success, image = vidcap.read()
|
190 |
while success:
|
191 |
if frame_count % sample_interval == 0:
|
192 |
res = model.predict(image, conf=confidence)
|
193 |
res_plotted = res[0].plot()[:, :, ::-1]
|
194 |
processed_frames.append(res_plotted)
|
|
|
195 |
if total_frames > 0:
|
196 |
progress_pct = int((frame_count / total_frames) * 100)
|
197 |
progress_text.text(f"Processing frame {frame_count} / {total_frames} ({progress_pct}%)")
|
198 |
progress_bar.progress(min(100, progress_pct))
|
199 |
else:
|
200 |
progress_text.text(f"Processing frame {frame_count}")
|
|
|
|
|
201 |
frame_count += 1
|
202 |
success, image = vidcap.read()
|
203 |
|
@@ -207,7 +211,7 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
|
|
207 |
# Create shortened video from processed frames.
|
208 |
if processed_frames:
|
209 |
temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
210 |
-
# Use
|
211 |
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
212 |
out = cv2.VideoWriter(temp_video_file.name, fourcc, output_fps, (width, height))
|
213 |
for frame in processed_frames:
|
@@ -220,14 +224,17 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
|
|
220 |
st.session_state["shortened_video_ready"] = True
|
221 |
|
222 |
st.success("Processed video created successfully!")
|
|
|
|
|
|
|
223 |
else:
|
224 |
st.error("No frames were processed from the video.")
|
225 |
|
226 |
###############################################################################
|
227 |
-
#
|
228 |
###############################################################################
|
229 |
if st.session_state["shortened_video_ready"] and st.session_state["shortened_video_data"]:
|
230 |
-
|
231 |
label="Download Processed Video",
|
232 |
data=st.session_state["shortened_video_data"],
|
233 |
file_name="processed_video.mp4",
|
|
|
8 |
import requests
|
9 |
|
10 |
###############################################################################
|
11 |
+
# Helper: Embed an HTML5 video for autoplay (muted) display.
|
12 |
###############################################################################
|
13 |
def show_autoplay_video(video_bytes: bytes, title: str = "Video"):
|
14 |
if not video_bytes:
|
|
|
25 |
st.markdown(video_html, unsafe_allow_html=True)
|
26 |
|
27 |
###############################################################################
|
28 |
+
# Session state initialization for processed results.
|
29 |
###############################################################################
|
30 |
if "processed_frames" not in st.session_state:
|
31 |
st.session_state["processed_frames"] = []
|
|
|
35 |
st.session_state["shortened_video_ready"] = False
|
36 |
|
37 |
###############################################################################
|
38 |
+
# Configure YOLO model path and page layout.
|
39 |
###############################################################################
|
40 |
model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt'
|
41 |
st.set_page_config(
|
|
|
46 |
)
|
47 |
|
48 |
###############################################################################
|
49 |
+
# SIDEBAR: Video input options, confidence, sampling options, and example selection.
|
50 |
###############################################################################
|
51 |
with st.sidebar:
|
52 |
st.header("Video Input Options")
|
|
|
53 |
example_option = st.selectbox(
|
54 |
"Select Example Pair (optional)",
|
55 |
["None", "T Example", "LA Example"]
|
|
|
65 |
)
|
66 |
progress_text = st.empty()
|
67 |
progress_bar = st.progress(0)
|
|
|
|
|
68 |
|
69 |
###############################################################################
|
70 |
# MAIN TITLE
|
|
|
72 |
st.title("Fire Detection: Original vs. Processed Video")
|
73 |
|
74 |
###############################################################################
|
75 |
+
# Load YOLO model.
|
76 |
###############################################################################
|
77 |
try:
|
78 |
model = YOLO(model_path)
|
|
|
87 |
processed_video_data = None # For example pairs
|
88 |
|
89 |
if example_option != "None":
|
90 |
+
# Load example videos from URLs.
|
91 |
if example_option == "T Example":
|
92 |
orig_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T1.mp4"
|
93 |
proc_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T2.mpg"
|
|
|
100 |
except Exception as ex:
|
101 |
st.error("Error loading example videos. Check your URLs.")
|
102 |
else:
|
|
|
103 |
if source_file:
|
104 |
file_type = source_file.type.split('/')[0]
|
105 |
if file_type == 'image':
|
106 |
+
# For images, show the image preview.
|
107 |
original_image = PIL.Image.open(source_file)
|
108 |
buf = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
109 |
original_image.save(buf.name, format="PNG")
|
110 |
with open(buf.name, "rb") as f:
|
111 |
original_video_data = f.read()
|
112 |
else:
|
113 |
+
# For video uploads, save to a temporary file.
|
114 |
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
115 |
tfile.write(source_file.read())
|
116 |
tfile.flush()
|
|
|
122 |
st.info("Please select an example pair or upload a video file.")
|
123 |
|
124 |
###############################################################################
|
125 |
+
# Layout: Two columns for Original and Processed videos.
|
126 |
###############################################################################
|
127 |
col1, col2 = st.columns(2)
|
128 |
|
|
|
133 |
else:
|
134 |
st.info("No original video available.")
|
135 |
|
136 |
+
# Create a placeholder for the processed video display.
|
137 |
+
viewer_slot = col2.empty()
|
138 |
+
col2.subheader("Result File")
|
139 |
+
|
140 |
+
if example_option != "None":
|
141 |
+
if processed_video_data:
|
142 |
+
show_autoplay_video(processed_video_data, title="Processed Video")
|
143 |
else:
|
144 |
+
col2.info("No processed video available in example.")
|
145 |
+
else:
|
146 |
+
if st.session_state["shortened_video_ready"] and st.session_state["shortened_video_data"]:
|
147 |
+
show_autoplay_video(st.session_state["shortened_video_data"], title="Processed Video")
|
148 |
+
else:
|
149 |
+
col2.info("Processed video will appear here once detection is run.")
|
150 |
|
151 |
###############################################################################
|
152 |
# DETECTION: Process the uploaded video if no example is selected.
|
153 |
###############################################################################
|
154 |
if example_option == "None" and source_file and source_file.type.split('/')[0] != 'image':
|
155 |
if st.sidebar.button("Let's Detect Wildfire"):
|
156 |
+
# Reset previous results.
|
157 |
st.session_state["processed_frames"] = []
|
158 |
st.session_state["shortened_video_data"] = None
|
159 |
st.session_state["shortened_video_ready"] = False
|
|
|
186 |
sample_interval = 1
|
187 |
output_fps = orig_fps
|
188 |
|
189 |
+
# Process frames.
|
190 |
success, image = vidcap.read()
|
191 |
while success:
|
192 |
if frame_count % sample_interval == 0:
|
193 |
res = model.predict(image, conf=confidence)
|
194 |
res_plotted = res[0].plot()[:, :, ::-1]
|
195 |
processed_frames.append(res_plotted)
|
196 |
+
# Update progress.
|
197 |
if total_frames > 0:
|
198 |
progress_pct = int((frame_count / total_frames) * 100)
|
199 |
progress_text.text(f"Processing frame {frame_count} / {total_frames} ({progress_pct}%)")
|
200 |
progress_bar.progress(min(100, progress_pct))
|
201 |
else:
|
202 |
progress_text.text(f"Processing frame {frame_count}")
|
203 |
+
# Update the viewer with the most recent processed frame.
|
204 |
+
viewer_slot.image(res_plotted, caption=f"Frame {frame_count}", use_column_width=True)
|
205 |
frame_count += 1
|
206 |
success, image = vidcap.read()
|
207 |
|
|
|
211 |
# Create shortened video from processed frames.
|
212 |
if processed_frames:
|
213 |
temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
214 |
+
# Use 'avc1' codec (H.264) for better compatibility.
|
215 |
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
216 |
out = cv2.VideoWriter(temp_video_file.name, fourcc, output_fps, (width, height))
|
217 |
for frame in processed_frames:
|
|
|
224 |
st.session_state["shortened_video_ready"] = True
|
225 |
|
226 |
st.success("Processed video created successfully!")
|
227 |
+
# Update the viewer with the final processed video.
|
228 |
+
viewer_slot.empty()
|
229 |
+
show_autoplay_video(st.session_state["shortened_video_data"], title="Processed Video")
|
230 |
else:
|
231 |
st.error("No frames were processed from the video.")
|
232 |
|
233 |
###############################################################################
|
234 |
+
# ALWAYS display the download button if a processed video is ready.
|
235 |
###############################################################################
|
236 |
if st.session_state["shortened_video_ready"] and st.session_state["shortened_video_data"]:
|
237 |
+
st.download_button(
|
238 |
label="Download Processed Video",
|
239 |
data=st.session_state["shortened_video_data"],
|
240 |
file_name="processed_video.mp4",
|