File size: 12,438 Bytes
0707d05
f98a043
0707d05
f0f9dff
 
debd205
0ba77f1
 
2f1733d
f0f9dff
24fa59e
0707d05
36fbec5
0707d05
36fbec5
 
 
 
0707d05
d44cea7
cac62cc
 
d44cea7
36fbec5
 
 
0707d05
cac62cc
36fbec5
 
 
24c4f17
 
 
 
 
cac62cc
24c4f17
 
 
 
 
0707d05
24c4f17
 
36fbec5
 
 
 
9d79b23
24fa59e
2f1733d
0ba77f1
 
 
0707d05
 
24fa59e
0ba77f1
cac62cc
 
 
 
 
 
36fbec5
0707d05
 
 
 
 
f0f9dff
24fa59e
0707d05
f0f9dff
3bcd916
0ba77f1
0707d05
24fa59e
36fbec5
0707d05
 
36fbec5
3bcd916
 
0707d05
24fa59e
13a0ff7
24c4f17
3bcd916
2f1733d
 
3bcd916
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1733d
3bcd916
 
 
 
2f1733d
3bcd916
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0f9dff
2f1733d
0ba77f1
24fa59e
 
36fbec5
d44cea7
5c1fa0a
36fbec5
d44cea7
36fbec5
cac62cc
 
24fa59e
cac62cc
 
 
 
 
 
 
24c4f17
 
 
 
cac62cc
d44cea7
 
 
c0792b2
 
0707d05
c0792b2
d44cea7
 
 
 
0707d05
 
 
 
 
c0792b2
0707d05
2f1733d
c0792b2
 
 
 
 
 
5c1fa0a
c0792b2
 
 
0707d05
 
 
d44cea7
5c1fa0a
d44cea7
 
 
 
 
 
 
 
0707d05
 
c0792b2
0707d05
c0792b2
 
 
5c1fa0a
0707d05
 
5c1fa0a
 
 
c0792b2
 
 
 
0707d05
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import PIL
import cv2
import streamlit as st
from ultralytics import YOLO
import tempfile
import time
import requests
import numpy as np
import os

# Page Config
st.set_page_config(page_title="WildfireWatch", page_icon="🔥", layout="wide")

# CSS for layout stability and dark tab text
st.markdown(
    """
    <style>
    .stApp {
        background-color: #f5f5f5;
        color: #1a1a1a;
    }
    h1 {
        color: #1a1a1a;
    }
    .stTabs > div > button {
        background-color: #e0e0e0;
        color: #333333;
        font-weight: bold;
    }
    .stTabs > div > button:hover {
        background-color: #d0d0d0;
        color: #333333;
    }
    .stTabs > div > button[aria-selected="true"] {
        background-color: #ffffff;
        color: #333333;
    }
    .main .block-container {
        max-height: 100vh;
        overflow-y: auto;
    }
    .stImage > img {
        max-height: 50vh;
        object-fit: contain;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# Load Model
model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt'
try:
    model = YOLO(model_path)
except Exception as ex:
    st.error(f"Unable to load model. Check the specified path: {model_path}")
    st.error(ex)
    st.stop()

# Initialize Session State
if 'monitoring' not in st.session_state:
    st.session_state.monitoring = False
if 'current_webcam_url' not in st.session_state:
    st.session_state.current_webcam_url = None

# Header
st.title("WildfireWatch: Detecting Wildfire using AI")
st.markdown("""
    Wildfires are a major environmental issue, causing substantial losses to ecosystems, human livelihoods, and potentially leading to loss of life. Early detection of wildfires can prevent these losses. Our application uses state-of-the-art YOLOv8 model for real-time wildfire and smoke detection.
""")
st.markdown("---")

# Tabs
tabs = st.tabs(["Upload", "Webcam"])

# Tab 1: Upload (Simplified with diagnostics)
with tabs[0]:
    col1, col2 = st.columns(2)
    with col1:
        st.markdown("**Add Your File**")
        st.write("Upload an image or video to scan for fire or smoke.")
        source_file = st.file_uploader("", type=["jpg", "jpeg", "png", "mp4"], label_visibility="collapsed")
        confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="upload_conf")
        sampling_options = {"Every Frame": 0, "1 FPS": 1, "2 FPS": 2, "5 FPS": 5}
        sampling_rate = st.selectbox("Analysis Rate", list(sampling_options.keys()), index=1, key="sampling_rate")
    
    with col2:
        frame_placeholder = st.empty()
        status_placeholder = st.empty()
        progress_placeholder = st.empty()
        download_placeholder = st.empty()
        
        if source_file:
            st.write(f"File size: {source_file.size / 1024 / 1024:.2f} MB")  # Diagnostic
            if st.button("Detect Wildfire", key="upload_detect"):
                file_type = source_file.type.split('/')[0]
                if file_type == 'image':
                    uploaded_image = PIL.Image.open(source_file)
                    res = model.predict(uploaded_image, conf=confidence)
                    detected_image = res[0].plot()[:, :, ::-1]
                    frame_placeholder.image(detected_image, use_column_width=True)
                    status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
                elif file_type == 'video':
                    try:
                        # Save input video
                        input_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
                        input_tfile.write(source_file.read())
                        input_tfile.close()

                        # Open video
                        vidcap = cv2.VideoCapture(input_tfile.name)
                        if not vidcap.isOpened():
                            status_placeholder.error("Failed to open video file.")
                        else:
                            total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
                            fps = int(vidcap.get(cv2.CAP_PROP_FPS)) or 30
                            frame_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
                            frame_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                            
                            # Frame sampling
                            target_fps = sampling_options[sampling_rate]
                            frame_skip = 1 if target_fps == 0 else max(1, int(fps / target_fps))
                            
                            # Output video
                            output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
                            fourcc = cv2.VideoWriter_fourcc(*'mp4v')
                            out = cv2.VideoWriter(output_tfile.name, fourcc, fps, (frame_width, frame_height))
                            
                            success, frame = vidcap.read()
                            frame_count = 0
                            processed_count = 0
                            last_detected_frame = None
                            
                            while success:
                                if frame_count % frame_skip == 0:
                                    res = model.predict(frame, conf=confidence)
                                    detected_frame = res[0].plot()[:, :, ::-1]
                                    last_detected_frame = detected_frame
                                    frame_placeholder.image(detected_frame, use_column_width=True)
                                    status_placeholder.write(f"Frame {frame_count}: Objects detected: {len(res[0].boxes)}")
                                    processed_count += 1
                                elif last_detected_frame is not None:
                                    frame_placeholder.image(last_detected_frame, use_column_width=True)
                                
                                if last_detected_frame is not None:
                                    out.write(last_detected_frame[:, :, ::-1])
                                
                                # Progress
                                if total_frames > 0:
                                    progress_percent = (frame_count + 1) / total_frames * 100
                                    progress_placeholder.write(f"Progress: {progress_percent:.1f}% (Processed {processed_count} frames)")
                                else:
                                    progress_placeholder.write(f"Progress: {frame_count} frames processed")
                                
                                success, frame = vidcap.read()
                                frame_count += 1
                                time.sleep(0.05)
                            
                            vidcap.release()
                            out.release()
                            
                            os.unlink(input_tfile.name)
                            with open(output_tfile.name, 'rb') as f:
                                download_placeholder.download_button(
                                    label="Download Analyzed Video",
                                    data=f,
                                    file_name="analyzed_video.mp4",
                                    mime="video/mp4"
                                )
                            status_placeholder.write(f"Video processing complete. Processed {processed_count} of {frame_count} frames.")
                    except Exception as e:
                        status_placeholder.error(f"Error processing video: {str(e)}")

# Tab 2: Webcam (Unchanged)
with tabs[1]:
    col1, col2 = st.columns([1, 1])
    with col1:
        st.markdown("**Webcam Feed**")
        st.write("Provide a webcam URL (image or video stream) to monitor for hazards.")
        webcam_url = st.text_input("Webcam URL", "http://<your_webcam_ip>/current.jpg", label_visibility="collapsed")
        confidence = st.slider("Detection Threshold", 0.25, 1.0, 0.4, key="webcam_conf")
        refresh_rate = st.slider("Refresh Rate (seconds)", 1, 60, 30, key="webcam_rate")
        start = st.button("Begin Monitoring", key="webcam_start")
        stop = st.button("Stop Monitoring", key="webcam_stop")

        if start:
            st.session_state.monitoring = True
            st.session_state.current_webcam_url = webcam_url
        if stop or (st.session_state.monitoring and webcam_url != st.session_state.current_webcam_url):
            st.session_state.monitoring = False
            st.session_state.current_webcam_url = None

    with col2:
        frame_placeholder = st.empty()
        status_placeholder = st.empty()
        timer_placeholder = st.empty()
        
        if st.session_state.monitoring and st.session_state.current_webcam_url:
            cap = cv2.VideoCapture(webcam_url)
            is_video_stream = cap.isOpened()
            
            if is_video_stream:
                status_placeholder.write("Connected to video stream...")
                while st.session_state.monitoring and cap.isOpened():
                    try:
                        ret, frame = cap.read()
                        if not ret:
                            status_placeholder.error("Video stream interrupted.")
                            break
                        if webcam_url != st.session_state.current_webcam_url:
                            status_placeholder.write("URL changed. Stopping video monitoring.")
                            break
                        res = model.predict(frame, conf=confidence)
                        detected_frame = res[0].plot()[:, :, ::-1]
                        frame_placeholder.image(detected_frame, use_column_width=True)
                        status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
                        time.sleep(0.1)
                    except Exception as e:
                        status_placeholder.error(f"Video error: {e}")
                        st.session_state.monitoring = False
                        break
                cap.release()
            else:
                status_placeholder.write("Monitoring image-based webcam...")
                while st.session_state.monitoring:
                    try:
                        start_time = time.time()
                        if webcam_url != st.session_state.current_webcam_url:
                            status_placeholder.write("URL changed. Stopping image monitoring.")
                            break
                        response = requests.get(webcam_url, timeout=5)
                        if response.status_code != 200:
                            status_placeholder.error(f"Fetch failed: HTTP {response.status_code}")
                            break
                        image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
                        frame = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
                        if frame is None:
                            status_placeholder.error("Image decoding failed.")
                            break

                        res = model.predict(frame, conf=confidence)
                        detected_frame = res[0].plot()[:, :, ::-1]
                        frame_placeholder.image(detected_frame, use_column_width=True)
                        status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
                        
                        elapsed = time.time() - start_time
                        remaining = max(0, refresh_rate - elapsed)
                        for i in range(int(remaining), -1, -1):
                            if not st.session_state.monitoring or webcam_url != st.session_state.current_webcam_url:
                                status_placeholder.write("Monitoring interrupted or URL changed.")
                                break
                            timer_placeholder.write(f"Next scan: {i}s")
                            time.sleep(1)
                    except Exception as e:
                        status_placeholder.error(f"Image fetch error: {e}")
                        st.session_state.monitoring = False
                        break
            if not st.session_state.monitoring:
                timer_placeholder.write("Monitoring stopped.")