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,79 +81,95 @@ 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 |
|
85 |
with col2:
|
86 |
frame_placeholder = st.empty()
|
87 |
status_placeholder = st.empty()
|
88 |
-
progress_placeholder = st.empty()
|
89 |
download_placeholder = st.empty()
|
90 |
|
91 |
-
if source_file
|
92 |
-
|
93 |
-
if
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
else:
|
110 |
-
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
111 |
-
if total_frames <= 0:
|
112 |
-
total_frames = None # Fallback if frame count unavailable
|
113 |
-
|
114 |
-
# Prepare output video
|
115 |
-
output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
|
116 |
-
frame_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
117 |
-
frame_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
118 |
-
fps = int(vidcap.get(cv2.CAP_PROP_FPS)) or 30
|
119 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
120 |
-
out = cv2.VideoWriter(output_tfile.name, fourcc, fps, (frame_width, frame_height))
|
121 |
-
|
122 |
-
success, frame = vidcap.read()
|
123 |
-
frame_count = 0
|
124 |
-
while success:
|
125 |
-
res = model.predict(frame, conf=confidence)
|
126 |
-
detected_frame = res[0].plot()[:, :, ::-1]
|
127 |
-
frame_placeholder.image(detected_frame, use_column_width=True) # Preview
|
128 |
-
status_placeholder.write(f"Frame {frame_count}: Objects detected: {len(res[0].boxes)}")
|
129 |
-
|
130 |
-
# Write to output video
|
131 |
-
detected_frame_bgr = detected_frame[:, :, ::-1]
|
132 |
-
out.write(detected_frame_bgr)
|
133 |
-
|
134 |
-
# Update progress
|
135 |
-
if total_frames:
|
136 |
-
progress_percent = (frame_count + 1) / total_frames * 100
|
137 |
-
progress_placeholder.write(f"Progress: {progress_percent:.1f}%")
|
138 |
else:
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
# Tab 2: Webcam (Unchanged)
|
159 |
with tabs[1]:
|
|
|
73 |
# Tabs
|
74 |
tabs = st.tabs(["Upload", "Webcam"])
|
75 |
|
76 |
+
# Tab 1: Upload (Simplified with diagnostics)
|
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 = {"Every Frame": 0, "1 FPS": 1, "2 FPS": 2, "5 FPS": 5}
|
85 |
+
sampling_rate = st.selectbox("Analysis Rate", list(sampling_options.keys()), index=1, key="sampling_rate")
|
86 |
|
87 |
with col2:
|
88 |
frame_placeholder = st.empty()
|
89 |
status_placeholder = st.empty()
|
90 |
+
progress_placeholder = st.empty()
|
91 |
download_placeholder = st.empty()
|
92 |
|
93 |
+
if source_file:
|
94 |
+
st.write(f"File size: {source_file.size / 1024 / 1024:.2f} MB") # Diagnostic
|
95 |
+
if st.button("Detect Wildfire", key="upload_detect"):
|
96 |
+
file_type = source_file.type.split('/')[0]
|
97 |
+
if file_type == 'image':
|
98 |
+
uploaded_image = PIL.Image.open(source_file)
|
99 |
+
res = model.predict(uploaded_image, conf=confidence)
|
100 |
+
detected_image = res[0].plot()[:, :, ::-1]
|
101 |
+
frame_placeholder.image(detected_image, use_column_width=True)
|
102 |
+
status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
|
103 |
+
elif file_type == 'video':
|
104 |
+
try:
|
105 |
+
# Save input video
|
106 |
+
input_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
107 |
+
input_tfile.write(source_file.read())
|
108 |
+
input_tfile.close()
|
109 |
|
110 |
+
# Open video
|
111 |
+
vidcap = cv2.VideoCapture(input_tfile.name)
|
112 |
+
if not vidcap.isOpened():
|
113 |
+
status_placeholder.error("Failed to open video file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
else:
|
115 |
+
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
116 |
+
fps = int(vidcap.get(cv2.CAP_PROP_FPS)) or 30
|
117 |
+
frame_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
118 |
+
frame_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
119 |
+
|
120 |
+
# Frame sampling
|
121 |
+
target_fps = sampling_options[sampling_rate]
|
122 |
+
frame_skip = 1 if target_fps == 0 else max(1, int(fps / target_fps))
|
123 |
+
|
124 |
+
# Output video
|
125 |
+
output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
|
126 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
127 |
+
out = cv2.VideoWriter(output_tfile.name, fourcc, fps, (frame_width, frame_height))
|
128 |
+
|
129 |
+
success, frame = vidcap.read()
|
130 |
+
frame_count = 0
|
131 |
+
processed_count = 0
|
132 |
+
last_detected_frame = None
|
133 |
+
|
134 |
+
while success:
|
135 |
+
if frame_count % frame_skip == 0:
|
136 |
+
res = model.predict(frame, conf=confidence)
|
137 |
+
detected_frame = res[0].plot()[:, :, ::-1]
|
138 |
+
last_detected_frame = detected_frame
|
139 |
+
frame_placeholder.image(detected_frame, use_column_width=True)
|
140 |
+
status_placeholder.write(f"Frame {frame_count}: Objects detected: {len(res[0].boxes)}")
|
141 |
+
processed_count += 1
|
142 |
+
elif last_detected_frame is not None:
|
143 |
+
frame_placeholder.image(last_detected_frame, use_column_width=True)
|
144 |
+
|
145 |
+
if last_detected_frame is not None:
|
146 |
+
out.write(last_detected_frame[:, :, ::-1])
|
147 |
+
|
148 |
+
# Progress
|
149 |
+
if total_frames > 0:
|
150 |
+
progress_percent = (frame_count + 1) / total_frames * 100
|
151 |
+
progress_placeholder.write(f"Progress: {progress_percent:.1f}% (Processed {processed_count} frames)")
|
152 |
+
else:
|
153 |
+
progress_placeholder.write(f"Progress: {frame_count} frames processed")
|
154 |
+
|
155 |
+
success, frame = vidcap.read()
|
156 |
+
frame_count += 1
|
157 |
+
time.sleep(0.05)
|
158 |
+
|
159 |
+
vidcap.release()
|
160 |
+
out.release()
|
161 |
+
|
162 |
+
os.unlink(input_tfile.name)
|
163 |
+
with open(output_tfile.name, 'rb') as f:
|
164 |
+
download_placeholder.download_button(
|
165 |
+
label="Download Analyzed Video",
|
166 |
+
data=f,
|
167 |
+
file_name="analyzed_video.mp4",
|
168 |
+
mime="video/mp4"
|
169 |
+
)
|
170 |
+
status_placeholder.write(f"Video processing complete. Processed {processed_count} of {frame_count} frames.")
|
171 |
+
except Exception as e:
|
172 |
+
status_placeholder.error(f"Error processing video: {str(e)}")
|
173 |
|
174 |
# Tab 2: Webcam (Unchanged)
|
175 |
with tabs[1]:
|