Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -73,7 +73,7 @@ st.markdown("---")
|
|
73 |
# Tabs
|
74 |
tabs = st.tabs(["Upload", "Webcam"])
|
75 |
|
76 |
-
# Tab 1: Upload (
|
77 |
with tabs[0]:
|
78 |
col1, col2 = st.columns(2)
|
79 |
with col1:
|
@@ -81,7 +81,6 @@ with tabs[0]:
|
|
81 |
st.write("Upload an image or video to scan for fire or smoke.")
|
82 |
source_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
83 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
|
84 |
-
# Expanded sampling options
|
85 |
sampling_options = {
|
86 |
"Every Frame": 0,
|
87 |
"1 FPS": 1,
|
@@ -128,27 +127,28 @@ with tabs[0]:
|
|
128 |
|
129 |
# Calculate frame skip
|
130 |
target_rate = sampling_options[sampling_rate]
|
131 |
-
if target_rate == 0:
|
132 |
frame_skip = 1
|
133 |
-
elif target_rate <= 5:
|
134 |
frame_skip = max(1, int(fps / target_rate))
|
135 |
-
else:
|
136 |
frame_skip = max(1, int(fps * target_rate))
|
137 |
|
138 |
# Batch processing setup
|
139 |
-
batch_size = 10
|
140 |
frames_to_process = []
|
141 |
frame_indices = []
|
142 |
|
143 |
-
# Output video
|
144 |
output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
|
145 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
146 |
-
|
|
|
|
|
147 |
|
148 |
success, frame = vidcap.read()
|
149 |
frame_count = 0
|
150 |
processed_count = 0
|
151 |
-
last_detected_frame = None
|
152 |
last_ui_update = time.time()
|
153 |
|
154 |
while success:
|
@@ -162,21 +162,18 @@ with tabs[0]:
|
|
162 |
res = model.predict(frames_to_process, conf=confidence)
|
163 |
for i, (result, idx) in enumerate(zip(res, frame_indices)):
|
164 |
detected_frame = result.plot()[:, :, ::-1]
|
165 |
-
|
166 |
-
# Update UI sparingly (every 1s)
|
167 |
if time.time() - last_ui_update >= 1.0:
|
168 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
169 |
status_placeholder.write(f"Frame {idx}: Objects detected: {len(result.boxes)}")
|
170 |
last_ui_update = time.time()
|
171 |
-
|
172 |
-
|
173 |
-
if last_detected_frame is not None:
|
174 |
-
out.write(last_detected_frame[:, :, ::-1])
|
175 |
|
176 |
# Progress
|
177 |
if total_frames > 0:
|
178 |
progress_percent = (frame_count + 1) / total_frames * 100
|
179 |
-
progress_placeholder.write(f"Progress: {progress_percent:.1f}% (
|
180 |
else:
|
181 |
progress_placeholder.write(f"Progress: {frame_count} frames processed")
|
182 |
|
@@ -197,7 +194,7 @@ with tabs[0]:
|
|
197 |
file_name="analyzed_video.mp4",
|
198 |
mime="video/mp4"
|
199 |
)
|
200 |
-
status_placeholder.write(f"Video processing complete.
|
201 |
except Exception as e:
|
202 |
status_placeholder.error(f"Error processing video: {str(e)}")
|
203 |
|
|
|
73 |
# Tabs
|
74 |
tabs = st.tabs(["Upload", "Webcam"])
|
75 |
|
76 |
+
# Tab 1: Upload (Output only analyzed frames)
|
77 |
with tabs[0]:
|
78 |
col1, col2 = st.columns(2)
|
79 |
with col1:
|
|
|
81 |
st.write("Upload an image or video to scan for fire or smoke.")
|
82 |
source_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
83 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
|
|
|
84 |
sampling_options = {
|
85 |
"Every Frame": 0,
|
86 |
"1 FPS": 1,
|
|
|
127 |
|
128 |
# Calculate frame skip
|
129 |
target_rate = sampling_options[sampling_rate]
|
130 |
+
if target_rate == 0:
|
131 |
frame_skip = 1
|
132 |
+
elif target_rate <= 5:
|
133 |
frame_skip = max(1, int(fps / target_rate))
|
134 |
+
else:
|
135 |
frame_skip = max(1, int(fps * target_rate))
|
136 |
|
137 |
# Batch processing setup
|
138 |
+
batch_size = 10
|
139 |
frames_to_process = []
|
140 |
frame_indices = []
|
141 |
|
142 |
+
# Output video (only analyzed frames)
|
143 |
output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
|
144 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
145 |
+
# Use 1 FPS for output to make it viewable, adjust as needed
|
146 |
+
output_fps = 1
|
147 |
+
out = cv2.VideoWriter(output_tfile.name, fourcc, output_fps, (frame_width, frame_height))
|
148 |
|
149 |
success, frame = vidcap.read()
|
150 |
frame_count = 0
|
151 |
processed_count = 0
|
|
|
152 |
last_ui_update = time.time()
|
153 |
|
154 |
while success:
|
|
|
162 |
res = model.predict(frames_to_process, conf=confidence)
|
163 |
for i, (result, idx) in enumerate(zip(res, frame_indices)):
|
164 |
detected_frame = result.plot()[:, :, ::-1]
|
165 |
+
# Update UI sparingly
|
|
|
166 |
if time.time() - last_ui_update >= 1.0:
|
167 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
168 |
status_placeholder.write(f"Frame {idx}: Objects detected: {len(result.boxes)}")
|
169 |
last_ui_update = time.time()
|
170 |
+
# Write only analyzed frames
|
171 |
+
out.write(detected_frame[:, :, ::-1])
|
|
|
|
|
172 |
|
173 |
# Progress
|
174 |
if total_frames > 0:
|
175 |
progress_percent = (frame_count + 1) / total_frames * 100
|
176 |
+
progress_placeholder.write(f"Progress: {progress_percent:.1f}% (Analyzed {processed_count} frames)")
|
177 |
else:
|
178 |
progress_placeholder.write(f"Progress: {frame_count} frames processed")
|
179 |
|
|
|
194 |
file_name="analyzed_video.mp4",
|
195 |
mime="video/mp4"
|
196 |
)
|
197 |
+
status_placeholder.write(f"Video processing complete. Analyzed {processed_count} frames.")
|
198 |
except Exception as e:
|
199 |
status_placeholder.error(f"Error processing video: {str(e)}")
|
200 |
|