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: Embed an HTML5 video
|
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 for processed results
|
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,7 +46,7 @@ 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")
|
@@ -65,6 +65,7 @@ with st.sidebar:
|
|
65 |
)
|
66 |
progress_text = st.empty()
|
67 |
progress_bar = st.progress(0)
|
|
|
68 |
|
69 |
###############################################################################
|
70 |
# MAIN TITLE
|
@@ -72,7 +73,7 @@ with st.sidebar:
|
|
72 |
st.title("Fire Detection: Original vs. Processed Video")
|
73 |
|
74 |
###############################################################################
|
75 |
-
# Load YOLO model
|
76 |
###############################################################################
|
77 |
try:
|
78 |
model = YOLO(model_path)
|
@@ -87,7 +88,7 @@ original_video_data = None
|
|
87 |
processed_video_data = None # For example pairs
|
88 |
|
89 |
if example_option != "None":
|
90 |
-
#
|
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,17 +101,16 @@ if example_option != "None":
|
|
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()
|
@@ -133,27 +133,24 @@ with col1:
|
|
133 |
else:
|
134 |
st.info("No original video available.")
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
if example_option != "None":
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
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 |
-
|
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,7 +183,6 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
|
|
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:
|
@@ -234,7 +230,7 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
|
|
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 |
-
|
238 |
label="Download Processed Video",
|
239 |
data=st.session_state["shortened_video_data"],
|
240 |
file_name="processed_video.mp4",
|
|
|
8 |
import requests
|
9 |
|
10 |
###############################################################################
|
11 |
+
# Helper: Embed an HTML5 video that autoplays (muted) with controls.
|
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")
|
|
|
65 |
)
|
66 |
progress_text = st.empty()
|
67 |
progress_bar = st.progress(0)
|
68 |
+
download_placeholder = st.empty() # This placeholder will hold the download button
|
69 |
|
70 |
###############################################################################
|
71 |
# MAIN TITLE
|
|
|
73 |
st.title("Fire Detection: Original vs. Processed Video")
|
74 |
|
75 |
###############################################################################
|
76 |
+
# Load YOLO model
|
77 |
###############################################################################
|
78 |
try:
|
79 |
model = YOLO(model_path)
|
|
|
88 |
processed_video_data = None # For example pairs
|
89 |
|
90 |
if example_option != "None":
|
91 |
+
# Use example videos from remote URLs.
|
92 |
if example_option == "T Example":
|
93 |
orig_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T1.mp4"
|
94 |
proc_url = "https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/T2.mpg"
|
|
|
101 |
except Exception as ex:
|
102 |
st.error("Error loading example videos. Check your URLs.")
|
103 |
else:
|
104 |
+
# No example selected. If a file is uploaded, use it.
|
105 |
if source_file:
|
106 |
file_type = source_file.type.split('/')[0]
|
107 |
if file_type == 'image':
|
|
|
108 |
original_image = PIL.Image.open(source_file)
|
109 |
buf = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
110 |
original_image.save(buf.name, format="PNG")
|
111 |
with open(buf.name, "rb") as f:
|
112 |
original_video_data = f.read()
|
113 |
else:
|
|
|
114 |
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
115 |
tfile.write(source_file.read())
|
116 |
tfile.flush()
|
|
|
133 |
else:
|
134 |
st.info("No original video available.")
|
135 |
|
136 |
+
with col2:
|
137 |
+
st.subheader("Result File")
|
138 |
+
# Create a dedicated placeholder for the processed video.
|
139 |
+
viewer_slot = st.empty()
|
140 |
+
if example_option != "None":
|
141 |
+
if processed_video_data:
|
142 |
+
show_autoplay_video(processed_video_data, title="Processed Video")
|
143 |
+
else:
|
144 |
+
st.info("No processed video available in example.")
|
|
|
|
|
|
|
145 |
else:
|
146 |
+
viewer_slot.info("Processed video will appear here once detection is run.")
|
147 |
|
148 |
###############################################################################
|
149 |
# DETECTION: Process the uploaded video if no example is selected.
|
150 |
###############################################################################
|
151 |
if example_option == "None" and source_file and source_file.type.split('/')[0] != 'image':
|
152 |
if st.sidebar.button("Let's Detect Wildfire"):
|
153 |
+
# Reset previous processed results.
|
154 |
st.session_state["processed_frames"] = []
|
155 |
st.session_state["shortened_video_data"] = None
|
156 |
st.session_state["shortened_video_ready"] = False
|
|
|
183 |
sample_interval = 1
|
184 |
output_fps = orig_fps
|
185 |
|
|
|
186 |
success, image = vidcap.read()
|
187 |
while success:
|
188 |
if frame_count % sample_interval == 0:
|
|
|
230 |
# ALWAYS display the download button if a processed video is ready.
|
231 |
###############################################################################
|
232 |
if st.session_state["shortened_video_ready"] and st.session_state["shortened_video_data"]:
|
233 |
+
download_placeholder.download_button(
|
234 |
label="Download Processed Video",
|
235 |
data=st.session_state["shortened_video_data"],
|
236 |
file_name="processed_video.mp4",
|