Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,12 +24,16 @@ st.markdown(
|
|
24 |
}
|
25 |
.stTabs > div > button {
|
26 |
background-color: #e0e0e0;
|
27 |
-
color: #
|
28 |
font-weight: bold;
|
29 |
}
|
30 |
.stTabs > div > button:hover {
|
31 |
background-color: #d0d0d0;
|
32 |
-
color: #
|
|
|
|
|
|
|
|
|
33 |
}
|
34 |
.stButton > button {
|
35 |
background-color: #e0e0e0;
|
@@ -40,13 +44,22 @@ st.markdown(
|
|
40 |
background-color: #d0d0d0;
|
41 |
color: #1a1a1a;
|
42 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
</style>
|
44 |
""",
|
45 |
unsafe_allow_html=True
|
46 |
)
|
47 |
|
48 |
# Load Model
|
49 |
-
model_path = 'https://huggingface.co/spaces/
|
50 |
try:
|
51 |
model = YOLO(model_path)
|
52 |
except Exception as ex:
|
@@ -58,10 +71,12 @@ if 'monitoring' not in st.session_state:
|
|
58 |
st.session_state.monitoring = False
|
59 |
if 'current_webcam_url' not in st.session_state:
|
60 |
st.session_state.current_webcam_url = None
|
|
|
|
|
61 |
|
62 |
# Header
|
63 |
st.title("AI Fire Watch")
|
64 |
-
st.markdown("Monitor fire and smoke in real-time with AI
|
65 |
|
66 |
# Tabs
|
67 |
tabs = st.tabs(["Upload", "Webcam", "YouTube"])
|
@@ -75,19 +90,20 @@ with tabs[0]:
|
|
75 |
uploaded_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
76 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
|
77 |
with col2:
|
|
|
|
|
78 |
if uploaded_file:
|
79 |
file_type = uploaded_file.type.split('/')[0]
|
80 |
if file_type == 'image':
|
81 |
image = PIL.Image.open(uploaded_file)
|
82 |
results = model.predict(image, conf=confidence)
|
83 |
detected_image = results[0].plot()[:, :, ::-1]
|
84 |
-
|
85 |
-
|
86 |
elif file_type == 'video':
|
87 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
88 |
tfile.write(uploaded_file.read())
|
89 |
cap = cv2.VideoCapture(tfile.name)
|
90 |
-
frame_placeholder = st.empty()
|
91 |
while cap.isOpened():
|
92 |
ret, frame = cap.read()
|
93 |
if not ret:
|
@@ -95,6 +111,7 @@ with tabs[0]:
|
|
95 |
results = model.predict(frame, conf=confidence)
|
96 |
detected_frame = results[0].plot()[:, :, ::-1]
|
97 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
|
|
98 |
time.sleep(0.05)
|
99 |
cap.release()
|
100 |
|
@@ -118,11 +135,11 @@ with tabs[1]:
|
|
118 |
st.session_state.current_webcam_url = None
|
119 |
|
120 |
with col2:
|
|
|
|
|
|
|
|
|
121 |
if st.session_state.monitoring and st.session_state.current_webcam_url:
|
122 |
-
frame_placeholder = st.empty()
|
123 |
-
status_placeholder = st.empty()
|
124 |
-
timer_placeholder = st.empty()
|
125 |
-
|
126 |
# Try video stream first
|
127 |
cap = cv2.VideoCapture(webcam_url)
|
128 |
is_video_stream = cap.isOpened()
|
@@ -136,7 +153,6 @@ with tabs[1]:
|
|
136 |
status_placeholder.error("Video stream interrupted.")
|
137 |
break
|
138 |
else:
|
139 |
-
# Fallback to image-based webcam
|
140 |
response = requests.get(webcam_url, timeout=5)
|
141 |
if response.status_code != 200:
|
142 |
status_placeholder.error(f"Fetch failed: HTTP {response.status_code}")
|
@@ -159,7 +175,7 @@ with tabs[1]:
|
|
159 |
if not is_video_stream:
|
160 |
time.sleep(remaining)
|
161 |
else:
|
162 |
-
time.sleep(0.1)
|
163 |
|
164 |
except Exception as e:
|
165 |
status_placeholder.error(f"Error: {e}")
|
@@ -180,18 +196,16 @@ with tabs[2]:
|
|
180 |
start_yt = st.button("Start Analysis", key="yt_start")
|
181 |
stop_yt = st.button("Stop Analysis", key="yt_stop")
|
182 |
|
183 |
-
if 'yt_monitoring' not in st.session_state:
|
184 |
-
st.session_state.yt_monitoring = False
|
185 |
-
|
186 |
if start_yt:
|
187 |
st.session_state.yt_monitoring = True
|
188 |
if stop_yt:
|
189 |
st.session_state.yt_monitoring = False
|
190 |
|
191 |
with col2:
|
|
|
|
|
|
|
192 |
if st.session_state.yt_monitoring and youtube_url and youtube_url != "https://www.youtube.com/watch?v=<id>":
|
193 |
-
status_placeholder = st.empty()
|
194 |
-
frame_placeholder = st.empty()
|
195 |
try:
|
196 |
status_placeholder.write("Initializing stream...")
|
197 |
streams = streamlink.streams(youtube_url)
|
@@ -213,7 +227,7 @@ with tabs[2]:
|
|
213 |
detected_frame = results[0].plot()[:, :, ::-1]
|
214 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
215 |
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
216 |
-
time.sleep(0.1)
|
217 |
cap.release()
|
218 |
except Exception as e:
|
219 |
status_placeholder.error(f"Error: {e}")
|
|
|
24 |
}
|
25 |
.stTabs > div > button {
|
26 |
background-color: #e0e0e0;
|
27 |
+
color: #333333; /* Darker tab text */
|
28 |
font-weight: bold;
|
29 |
}
|
30 |
.stTabs > div > button:hover {
|
31 |
background-color: #d0d0d0;
|
32 |
+
color: #333333;
|
33 |
+
}
|
34 |
+
.stTabs > div > button[aria-selected="true"] {
|
35 |
+
background-color: #ffffff;
|
36 |
+
color: #333333;
|
37 |
}
|
38 |
.stButton > button {
|
39 |
background-color: #e0e0e0;
|
|
|
44 |
background-color: #d0d0d0;
|
45 |
color: #1a1a1a;
|
46 |
}
|
47 |
+
/* Fix container height to prevent scrolling */
|
48 |
+
.main .block-container {
|
49 |
+
max-height: 100vh;
|
50 |
+
overflow-y: auto;
|
51 |
+
}
|
52 |
+
.stImage > img {
|
53 |
+
max-height: 50vh; /* Limit image height */
|
54 |
+
object-fit: contain;
|
55 |
+
}
|
56 |
</style>
|
57 |
""",
|
58 |
unsafe_allow_html=True
|
59 |
)
|
60 |
|
61 |
# Load Model
|
62 |
+
model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt'
|
63 |
try:
|
64 |
model = YOLO(model_path)
|
65 |
except Exception as ex:
|
|
|
71 |
st.session_state.monitoring = False
|
72 |
if 'current_webcam_url' not in st.session_state:
|
73 |
st.session_state.current_webcam_url = None
|
74 |
+
if 'yt_monitoring' not in st.session_state:
|
75 |
+
st.session_state.yt_monitoring = False
|
76 |
|
77 |
# Header
|
78 |
st.title("AI Fire Watch")
|
79 |
+
st.markdown("Monitor fire and smoke in real-time with AI vision models.")
|
80 |
|
81 |
# Tabs
|
82 |
tabs = st.tabs(["Upload", "Webcam", "YouTube"])
|
|
|
90 |
uploaded_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
|
91 |
confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
|
92 |
with col2:
|
93 |
+
frame_placeholder = st.empty() # Pre-create placeholder
|
94 |
+
status_placeholder = st.empty()
|
95 |
if uploaded_file:
|
96 |
file_type = uploaded_file.type.split('/')[0]
|
97 |
if file_type == 'image':
|
98 |
image = PIL.Image.open(uploaded_file)
|
99 |
results = model.predict(image, conf=confidence)
|
100 |
detected_image = results[0].plot()[:, :, ::-1]
|
101 |
+
frame_placeholder.image(detected_image, use_column_width=True)
|
102 |
+
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
103 |
elif file_type == 'video':
|
104 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
105 |
tfile.write(uploaded_file.read())
|
106 |
cap = cv2.VideoCapture(tfile.name)
|
|
|
107 |
while cap.isOpened():
|
108 |
ret, frame = cap.read()
|
109 |
if not ret:
|
|
|
111 |
results = model.predict(frame, conf=confidence)
|
112 |
detected_frame = results[0].plot()[:, :, ::-1]
|
113 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
114 |
+
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
115 |
time.sleep(0.05)
|
116 |
cap.release()
|
117 |
|
|
|
135 |
st.session_state.current_webcam_url = None
|
136 |
|
137 |
with col2:
|
138 |
+
frame_placeholder = st.empty()
|
139 |
+
status_placeholder = st.empty()
|
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()
|
|
|
153 |
status_placeholder.error("Video stream interrupted.")
|
154 |
break
|
155 |
else:
|
|
|
156 |
response = requests.get(webcam_url, timeout=5)
|
157 |
if response.status_code != 200:
|
158 |
status_placeholder.error(f"Fetch failed: HTTP {response.status_code}")
|
|
|
175 |
if not is_video_stream:
|
176 |
time.sleep(remaining)
|
177 |
else:
|
178 |
+
time.sleep(0.1)
|
179 |
|
180 |
except Exception as e:
|
181 |
status_placeholder.error(f"Error: {e}")
|
|
|
196 |
start_yt = st.button("Start Analysis", key="yt_start")
|
197 |
stop_yt = st.button("Stop Analysis", key="yt_stop")
|
198 |
|
|
|
|
|
|
|
199 |
if start_yt:
|
200 |
st.session_state.yt_monitoring = True
|
201 |
if stop_yt:
|
202 |
st.session_state.yt_monitoring = False
|
203 |
|
204 |
with col2:
|
205 |
+
frame_placeholder = st.empty()
|
206 |
+
status_placeholder = st.empty()
|
207 |
+
|
208 |
if st.session_state.yt_monitoring and youtube_url and youtube_url != "https://www.youtube.com/watch?v=<id>":
|
|
|
|
|
209 |
try:
|
210 |
status_placeholder.write("Initializing stream...")
|
211 |
streams = streamlink.streams(youtube_url)
|
|
|
227 |
detected_frame = results[0].plot()[:, :, ::-1]
|
228 |
frame_placeholder.image(detected_frame, use_column_width=True)
|
229 |
status_placeholder.write(f"Objects detected: {len(results[0].boxes)}")
|
230 |
+
time.sleep(0.1)
|
231 |
cap.release()
|
232 |
except Exception as e:
|
233 |
status_placeholder.error(f"Error: {e}")
|