Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -121,7 +121,7 @@ with tabs[1]:
|
|
| 121 |
with col1:
|
| 122 |
st.markdown("**Webcam Feed**")
|
| 123 |
st.write("Provide a webcam URL (image or video stream) to monitor for hazards.")
|
| 124 |
-
webcam_url = st.text_input("Webcam URL", "
|
| 125 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="webcam_conf")
|
| 126 |
refresh_rate = st.slider("Refresh Rate (seconds)", 1, 60, 30, key="webcam_rate")
|
| 127 |
start = st.button("Begin Monitoring", key="webcam_start")
|
|
@@ -140,21 +140,44 @@ with tabs[1]:
|
|
| 140 |
timer_placeholder = st.empty()
|
| 141 |
|
| 142 |
if st.session_state.monitoring and st.session_state.current_webcam_url:
|
| 143 |
-
# Try video stream first
|
| 144 |
cap = cv2.VideoCapture(webcam_url)
|
| 145 |
is_video_stream = cap.isOpened()
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
ret, frame = cap.read()
|
| 152 |
if not ret:
|
| 153 |
status_placeholder.error("Video stream interrupted.")
|
| 154 |
break
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
response = requests.get(webcam_url, timeout=5)
|
| 157 |
-
if response.status_code
|
|
|
|
|
|
|
|
|
|
| 158 |
status_placeholder.error(f"Fetch failed: HTTP {response.status_code}")
|
| 159 |
break
|
| 160 |
image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
|
|
@@ -163,27 +186,19 @@ with tabs[1]:
|
|
| 163 |
status_placeholder.error("Image decoding failed.")
|
| 164 |
break
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
if not is_video_stream:
|
| 176 |
time.sleep(remaining)
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
status_placeholder.error(f"Error: {e}")
|
| 182 |
-
st.session_state.monitoring = False
|
| 183 |
-
break
|
| 184 |
-
|
| 185 |
-
if is_video_stream:
|
| 186 |
-
cap.release()
|
| 187 |
|
| 188 |
# Tab 3: YouTube
|
| 189 |
with tabs[2]:
|
|
|
|
| 121 |
with col1:
|
| 122 |
st.markdown("**Webcam Feed**")
|
| 123 |
st.write("Provide a webcam URL (image or video stream) to monitor for hazards.")
|
| 124 |
+
webcam_url = st.text_input("Webcam URL", "https://wms-prod-3.wetmet.net/live/281-05-01/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9Mi8yOC8yMDI1IDk6MDE6MTcgUE0maGFzaF92YWx1ZT1jR0JLZnJYbmFVOEdYbDh1M0wyc1FnPT0mdmFsaWRtaW51dGVzPTMw", label_visibility="collapsed")
|
| 125 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="webcam_conf")
|
| 126 |
refresh_rate = st.slider("Refresh Rate (seconds)", 1, 60, 30, key="webcam_rate")
|
| 127 |
start = st.button("Begin Monitoring", key="webcam_start")
|
|
|
|
| 140 |
timer_placeholder = st.empty()
|
| 141 |
|
| 142 |
if st.session_state.monitoring and st.session_state.current_webcam_url:
|
| 143 |
+
# Try as video stream first
|
| 144 |
cap = cv2.VideoCapture(webcam_url)
|
| 145 |
is_video_stream = cap.isOpened()
|
| 146 |
|
| 147 |
+
if is_video_stream:
|
| 148 |
+
status_placeholder.write("Connected to video stream...")
|
| 149 |
+
while st.session_state.monitoring:
|
| 150 |
+
try:
|
| 151 |
+
start_time = time.time()
|
| 152 |
ret, frame = cap.read()
|
| 153 |
if not ret:
|
| 154 |
status_placeholder.error("Video stream interrupted.")
|
| 155 |
break
|
| 156 |
+
results = model.predict(frame, conf=confidence)
|
| 157 |
+
detected_frame = results[0].plot()[:, :, ::-1]
|
| 158 |
+
frame_placeholder.image(detected_frame, use_column_width=True)
|
| 159 |
+
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
| 160 |
+
|
| 161 |
+
elapsed = time.time() - start_time
|
| 162 |
+
remaining = max(0, refresh_rate - elapsed)
|
| 163 |
+
timer_placeholder.write(f"Next scan: {int(remaining)}s")
|
| 164 |
+
time.sleep(0.1)
|
| 165 |
+
except Exception as e:
|
| 166 |
+
status_placeholder.error(f"Video error: {e}")
|
| 167 |
+
st.session_state.monitoring = False
|
| 168 |
+
break
|
| 169 |
+
cap.release()
|
| 170 |
+
else:
|
| 171 |
+
# Fallback to image-based fetch
|
| 172 |
+
status_placeholder.write("Trying as image-based webcam...")
|
| 173 |
+
while st.session_state.monitoring:
|
| 174 |
+
try:
|
| 175 |
+
start_time = time.time()
|
| 176 |
response = requests.get(webcam_url, timeout=5)
|
| 177 |
+
if response.status_code == 403:
|
| 178 |
+
status_placeholder.error("HTTP 403: Access denied. Token may have expired.")
|
| 179 |
+
break
|
| 180 |
+
elif response.status_code != 200:
|
| 181 |
status_placeholder.error(f"Fetch failed: HTTP {response.status_code}")
|
| 182 |
break
|
| 183 |
image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
|
|
|
|
| 186 |
status_placeholder.error("Image decoding failed.")
|
| 187 |
break
|
| 188 |
|
| 189 |
+
results = model.predict(frame, conf=confidence)
|
| 190 |
+
detected_frame = results[0].plot()[:, :, ::-1]
|
| 191 |
+
frame_placeholder.image(detected_frame, use_column_width=True)
|
| 192 |
+
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
| 193 |
+
|
| 194 |
+
elapsed = time.time() - start_time
|
| 195 |
+
remaining = max(0, refresh_rate - elapsed)
|
| 196 |
+
timer_placeholder.write(f"Next scan: {int(remaining)}s")
|
|
|
|
|
|
|
| 197 |
time.sleep(remaining)
|
| 198 |
+
except Exception as e:
|
| 199 |
+
status_placeholder.error(f"Image fetch error: {e}")
|
| 200 |
+
st.session_state.monitoring = False
|
| 201 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
# Tab 3: YouTube
|
| 204 |
with tabs[2]:
|