Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,27 @@ import numpy as np
|
|
9 |
import streamlink
|
10 |
|
11 |
# Page Config
|
12 |
-
st.set_page_config(page_title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Load Model
|
15 |
model_path = 'https://huggingface.co/spaces/ankitkupadhyay/fire_and_smoke/resolve/main/best.pt'
|
@@ -19,20 +39,21 @@ except Exception as ex:
|
|
19 |
st.error(f"Model loading failed: {ex}")
|
20 |
st.stop()
|
21 |
|
22 |
-
#
|
23 |
-
st.title("
|
24 |
-
st.markdown("
|
25 |
|
26 |
# Tabs
|
27 |
tabs = st.tabs(["Upload", "Webcam", "YouTube"])
|
28 |
|
29 |
-
# Tab 1:
|
30 |
with tabs[0]:
|
31 |
col1, col2 = st.columns([1, 1])
|
32 |
with col1:
|
33 |
-
st.markdown("**
|
|
|
34 |
uploaded_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
35 |
-
confidence = st.slider("
|
36 |
with col2:
|
37 |
if uploaded_file:
|
38 |
file_type = uploaded_file.type.split('/')[0]
|
@@ -41,7 +62,7 @@ with tabs[0]:
|
|
41 |
results = model.predict(image, conf=confidence)
|
42 |
detected_image = results[0].plot()[:, :, ::-1]
|
43 |
st.image(detected_image, use_column_width=True)
|
44 |
-
st.write(f"
|
45 |
elif file_type == 'video':
|
46 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
47 |
tfile.write(uploaded_file.read())
|
@@ -57,97 +78,80 @@ with tabs[0]:
|
|
57 |
time.sleep(0.05)
|
58 |
cap.release()
|
59 |
|
60 |
-
# Tab 2: Webcam
|
61 |
with tabs[1]:
|
62 |
col1, col2 = st.columns([1, 1])
|
63 |
with col1:
|
64 |
-
st.markdown("**Webcam
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
start = st.button("
|
69 |
with col2:
|
70 |
if start:
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
if response.status_code != 200:
|
81 |
-
st.error(f"Failed to fetch image: HTTP {response.status_code}")
|
82 |
-
break
|
83 |
-
image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
|
84 |
-
frame = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
|
85 |
-
if frame is None:
|
86 |
-
st.error("Failed to decode image from URL.")
|
87 |
-
break
|
88 |
-
|
89 |
-
# Check for fire/smoke using the YOLO model
|
90 |
-
results = model.predict(frame, conf=confidence)
|
91 |
-
detected_frame = results[0].plot()[:, :, ::-1] # Convert BGR to RGB for display
|
92 |
-
|
93 |
-
# Display the processed image
|
94 |
-
image_placeholder.image(detected_frame, use_column_width=True)
|
95 |
-
|
96 |
-
# Update timer
|
97 |
-
elapsed = time.time() - start_time
|
98 |
-
remaining = max(0, refresh_interval - elapsed)
|
99 |
-
timer_placeholder.write(f"Next refresh in: {int(remaining)}s")
|
100 |
-
|
101 |
-
# Wait until 30 seconds have passed, updating the timer
|
102 |
-
while remaining > 0:
|
103 |
-
time.sleep(1)
|
104 |
-
elapsed = time.time() - start_time
|
105 |
-
remaining = max(0, refresh_interval - elapsed)
|
106 |
-
timer_placeholder.write(f"Next refresh in: {int(remaining)}s")
|
107 |
-
|
108 |
-
# Refresh the page to fetch a new image
|
109 |
-
st.experimental_rerun()
|
110 |
-
|
111 |
-
except Exception as e:
|
112 |
-
st.error(f"Error: {e}")
|
113 |
break
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
while cap.isOpened():
|
119 |
-
ret, frame = cap.read()
|
120 |
-
if not ret:
|
121 |
-
st.error("Stream failed.")
|
122 |
break
|
123 |
results = model.predict(frame, conf=confidence)
|
124 |
detected_frame = results[0].plot()[:, :, ::-1]
|
125 |
-
|
126 |
-
time.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
-
# Tab 3: YouTube
|
129 |
with tabs[2]:
|
130 |
col1, col2 = st.columns([1, 1])
|
131 |
with col1:
|
132 |
-
st.markdown("**YouTube
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
with col2:
|
137 |
-
if
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import streamlink
|
10 |
|
11 |
# Page Config
|
12 |
+
st.set_page_config(page_title="AI Fire Watch", page_icon="🌍", layout="wide")
|
13 |
+
|
14 |
+
# Lighter Background CSS
|
15 |
+
st.markdown(
|
16 |
+
"""
|
17 |
+
<style>
|
18 |
+
.stApp {
|
19 |
+
background-color: #f5f5f5;
|
20 |
+
color: #333333;
|
21 |
+
}
|
22 |
+
.stTabs > div > button {
|
23 |
+
background-color: #e0e0e0;
|
24 |
+
color: #333333;
|
25 |
+
}
|
26 |
+
.stTabs > div > button:hover {
|
27 |
+
background-color: #d0d0d0;
|
28 |
+
}
|
29 |
+
</style>
|
30 |
+
""",
|
31 |
+
unsafe_allow_html=True
|
32 |
+
)
|
33 |
|
34 |
# Load Model
|
35 |
model_path = 'https://huggingface.co/spaces/ankitkupadhyay/fire_and_smoke/resolve/main/best.pt'
|
|
|
39 |
st.error(f"Model loading failed: {ex}")
|
40 |
st.stop()
|
41 |
|
42 |
+
# Header
|
43 |
+
st.title("AI Fire Watch")
|
44 |
+
st.markdown("Monitor fire and smoke in real-time with AI precision.")
|
45 |
|
46 |
# Tabs
|
47 |
tabs = st.tabs(["Upload", "Webcam", "YouTube"])
|
48 |
|
49 |
+
# Tab 1: Upload
|
50 |
with tabs[0]:
|
51 |
col1, col2 = st.columns([1, 1])
|
52 |
with col1:
|
53 |
+
st.markdown("**Add Your File**")
|
54 |
+
st.write("Upload an image or video to scan for fire or smoke.")
|
55 |
uploaded_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
56 |
+
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
|
57 |
with col2:
|
58 |
if uploaded_file:
|
59 |
file_type = uploaded_file.type.split('/')[0]
|
|
|
62 |
results = model.predict(image, conf=confidence)
|
63 |
detected_image = results[0].plot()[:, :, ::-1]
|
64 |
st.image(detected_image, use_column_width=True)
|
65 |
+
st.write(f"Objects detected: {len(results[0].boxes)}")
|
66 |
elif file_type == 'video':
|
67 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
68 |
tfile.write(uploaded_file.read())
|
|
|
78 |
time.sleep(0.05)
|
79 |
cap.release()
|
80 |
|
81 |
+
# Tab 2: Webcam
|
82 |
with tabs[1]:
|
83 |
col1, col2 = st.columns([1, 1])
|
84 |
with col1:
|
85 |
+
st.markdown("**Webcam Feed**")
|
86 |
+
st.write("Provide a webcam URL to check snapshots for hazards every 30 seconds.")
|
87 |
+
webcam_url = st.text_input("Webcam URL", "http://<your_webcam_ip>/current.jpg", label_visibility="collapsed")
|
88 |
+
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="webcam_conf")
|
89 |
+
start = st.button("Begin Monitoring", key="webcam_start")
|
90 |
with col2:
|
91 |
if start:
|
92 |
+
image_placeholder = st.empty()
|
93 |
+
timer_placeholder = st.empty()
|
94 |
+
refresh_interval = 30 # Refresh every 30 seconds
|
95 |
+
while True:
|
96 |
+
start_time = time.time()
|
97 |
+
try:
|
98 |
+
response = requests.get(webcam_url, timeout=5)
|
99 |
+
if response.status_code != 200:
|
100 |
+
st.error(f"Fetch failed: HTTP {response.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
break
|
102 |
+
image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
|
103 |
+
frame = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
|
104 |
+
if frame is None:
|
105 |
+
st.error("Image decoding failed.")
|
|
|
|
|
|
|
|
|
106 |
break
|
107 |
results = model.predict(frame, conf=confidence)
|
108 |
detected_frame = results[0].plot()[:, :, ::-1]
|
109 |
+
image_placeholder.image(detected_frame, use_column_width=True)
|
110 |
+
elapsed = time.time() - start_time
|
111 |
+
remaining = max(0, refresh_interval - elapsed)
|
112 |
+
timer_placeholder.write(f"Next scan: {int(remaining)}s")
|
113 |
+
while remaining > 0:
|
114 |
+
time.sleep(1)
|
115 |
+
elapsed = time.time() - start_time
|
116 |
+
remaining = max(0, refresh_interval - elapsed)
|
117 |
+
timer_placeholder.write(f"Next scan: {int(remaining)}s")
|
118 |
+
st.experimental_rerun()
|
119 |
+
except Exception as e:
|
120 |
+
st.error(f"Error: {e}")
|
121 |
+
break
|
122 |
|
123 |
+
# Tab 3: YouTube
|
124 |
with tabs[2]:
|
125 |
col1, col2 = st.columns([1, 1])
|
126 |
with col1:
|
127 |
+
st.markdown("**YouTube Live**")
|
128 |
+
st.write("Enter a live YouTube URL to auto-analyze the stream.")
|
129 |
+
youtube_url = st.text_input("YouTube URL", "https://www.youtube.com/watch?v=<id>", label_visibility="collapsed")
|
130 |
+
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="yt_conf")
|
131 |
with col2:
|
132 |
+
if youtube_url and youtube_url != "https://www.youtube.com/watch?v=<id>":
|
133 |
+
st.write("Analyzing live stream...")
|
134 |
+
try:
|
135 |
+
streams = streamlink.streams(youtube_url)
|
136 |
+
if not streams:
|
137 |
+
st.error("No streams found. Check the URL.")
|
138 |
+
else:
|
139 |
+
stream_url = streams["best"].to_url()
|
140 |
+
cap = cv2.VideoCapture(stream_url)
|
141 |
+
if not cap.isOpened():
|
142 |
+
st.error("Unable to open stream.")
|
143 |
+
else:
|
144 |
+
frame_placeholder = st.empty()
|
145 |
+
while cap.isOpened():
|
146 |
+
ret, frame = cap.read()
|
147 |
+
if not ret:
|
148 |
+
st.error("Stream interrupted.")
|
149 |
+
break
|
150 |
+
results = model.predict(frame, conf=confidence)
|
151 |
+
detected_frame = results[0].plot()[:, :, ::-1]
|
152 |
+
frame_placeholder.image(detected_frame, use_column_width=True)
|
153 |
+
st.write(f"Objects detected: {len(results[0].boxes)}")
|
154 |
+
time.sleep(1) # Check every second
|
155 |
+
cap.release()
|
156 |
+
except Exception as e:
|
157 |
+
st.error(f"Error: {e}")
|