Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,12 +17,15 @@ st.set_page_config(
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
model = YOLO(model_path)
|
| 23 |
except Exception as ex:
|
| 24 |
st.error(f"Unable to load model from: {model_path}")
|
| 25 |
st.error(ex)
|
|
|
|
| 26 |
|
| 27 |
# -------------------------------
|
| 28 |
# App Title and Description
|
|
@@ -37,7 +40,7 @@ Use this app to detect wildfires in images, videos, updating image URLs, or live
|
|
| 37 |
tabs = st.tabs(["File Upload", "Image URL", "YouTube Live Stream"])
|
| 38 |
|
| 39 |
# ===============================
|
| 40 |
-
# Tab 1: File Upload Mode
|
| 41 |
with tabs[0]:
|
| 42 |
st.header("Detect Wildfire from Uploaded File")
|
| 43 |
col_input, col_result = st.columns(2)
|
|
@@ -52,19 +55,26 @@ with tabs[0]:
|
|
| 52 |
if uploaded_file:
|
| 53 |
file_type = uploaded_file.type.split('/')[0]
|
| 54 |
if file_type == "image":
|
|
|
|
| 55 |
image = PIL.Image.open(uploaded_file)
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
results = model.predict(image, conf=file_confidence)
|
| 59 |
annotated = results[0].plot()[:, :, ::-1]
|
| 60 |
col_result.image(annotated, caption="Detection Result", use_column_width=True)
|
| 61 |
with col_result.expander("Detection Details"):
|
| 62 |
for box in results[0].boxes:
|
| 63 |
col_result.write("Box (xywh):", box.xywh)
|
|
|
|
| 64 |
elif file_type == "video":
|
| 65 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 66 |
tfile.write(uploaded_file.read())
|
| 67 |
cap = cv2.VideoCapture(tfile.name)
|
|
|
|
| 68 |
if st.button("Detect Wildfire in Video", key="detect_file_video"):
|
| 69 |
while cap.isOpened():
|
| 70 |
ret, frame = cap.read()
|
|
@@ -114,7 +124,6 @@ with tabs[1]:
|
|
| 114 |
annotated = results[0].plot()[:, :, ::-1]
|
| 115 |
placeholder.image(annotated, channels="BGR", use_column_width=True)
|
| 116 |
time.sleep(1) # update interval in seconds
|
| 117 |
-
# Rerun the loop by breaking out of script execution cycle.
|
| 118 |
st.experimental_rerun()
|
| 119 |
except Exception as e:
|
| 120 |
col_result.error(f"Error fetching image: {e}")
|
|
@@ -149,7 +158,6 @@ with tabs[2]:
|
|
| 149 |
if not cap.isOpened():
|
| 150 |
st.error("Unable to open YouTube live stream.")
|
| 151 |
else:
|
| 152 |
-
# Provide a stop button for live stream detection
|
| 153 |
stop_live = st.button("Stop Live Detection", key="yt_stop")
|
| 154 |
while cap.isOpened() and not stop_live:
|
| 155 |
ret, frame = cap.read()
|
|
@@ -160,7 +168,6 @@ with tabs[2]:
|
|
| 160 |
annotated = results[0].plot()[:, :, ::-1]
|
| 161 |
col_result.image(annotated, channels="BGR", use_column_width=True)
|
| 162 |
time.sleep(0.05)
|
| 163 |
-
# Check for stop command during loop
|
| 164 |
stop_live = st.button("Stop Live Detection", key="yt_stop_loop")
|
| 165 |
cap.release()
|
| 166 |
except Exception as e:
|
|
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# IMPORTANT: Ensure the model file is a valid PyTorch model file.
|
| 21 |
+
# If using a URL, it must directly point to the raw .pt file, not an HTML page.
|
| 22 |
+
model_path = 'best.pt' # Change this to a valid path or direct URL to your model file
|
| 23 |
try:
|
| 24 |
model = YOLO(model_path)
|
| 25 |
except Exception as ex:
|
| 26 |
st.error(f"Unable to load model from: {model_path}")
|
| 27 |
st.error(ex)
|
| 28 |
+
st.stop() # Stop the app if the model isn't loaded
|
| 29 |
|
| 30 |
# -------------------------------
|
| 31 |
# App Title and Description
|
|
|
|
| 40 |
tabs = st.tabs(["File Upload", "Image URL", "YouTube Live Stream"])
|
| 41 |
|
| 42 |
# ===============================
|
| 43 |
+
# Tab 1: File Upload Mode (with auto-detection for images)
|
| 44 |
with tabs[0]:
|
| 45 |
st.header("Detect Wildfire from Uploaded File")
|
| 46 |
col_input, col_result = st.columns(2)
|
|
|
|
| 55 |
if uploaded_file:
|
| 56 |
file_type = uploaded_file.type.split('/')[0]
|
| 57 |
if file_type == "image":
|
| 58 |
+
# Load image and allow toggle between Original and Detection result
|
| 59 |
image = PIL.Image.open(uploaded_file)
|
| 60 |
+
display_option = st.radio("Display Mode", options=["Original", "Detection"], index=1)
|
| 61 |
+
|
| 62 |
+
if display_option == "Original":
|
| 63 |
+
col_result.image(image, caption="Original Image", use_column_width=True)
|
| 64 |
+
else:
|
| 65 |
+
# Auto-detect without a button click
|
| 66 |
results = model.predict(image, conf=file_confidence)
|
| 67 |
annotated = results[0].plot()[:, :, ::-1]
|
| 68 |
col_result.image(annotated, caption="Detection Result", use_column_width=True)
|
| 69 |
with col_result.expander("Detection Details"):
|
| 70 |
for box in results[0].boxes:
|
| 71 |
col_result.write("Box (xywh):", box.xywh)
|
| 72 |
+
|
| 73 |
elif file_type == "video":
|
| 74 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 75 |
tfile.write(uploaded_file.read())
|
| 76 |
cap = cv2.VideoCapture(tfile.name)
|
| 77 |
+
# For videos, detection is triggered with a button due to processing cost
|
| 78 |
if st.button("Detect Wildfire in Video", key="detect_file_video"):
|
| 79 |
while cap.isOpened():
|
| 80 |
ret, frame = cap.read()
|
|
|
|
| 124 |
annotated = results[0].plot()[:, :, ::-1]
|
| 125 |
placeholder.image(annotated, channels="BGR", use_column_width=True)
|
| 126 |
time.sleep(1) # update interval in seconds
|
|
|
|
| 127 |
st.experimental_rerun()
|
| 128 |
except Exception as e:
|
| 129 |
col_result.error(f"Error fetching image: {e}")
|
|
|
|
| 158 |
if not cap.isOpened():
|
| 159 |
st.error("Unable to open YouTube live stream.")
|
| 160 |
else:
|
|
|
|
| 161 |
stop_live = st.button("Stop Live Detection", key="yt_stop")
|
| 162 |
while cap.isOpened() and not stop_live:
|
| 163 |
ret, frame = cap.read()
|
|
|
|
| 168 |
annotated = results[0].plot()[:, :, ::-1]
|
| 169 |
col_result.image(annotated, channels="BGR", use_column_width=True)
|
| 170 |
time.sleep(0.05)
|
|
|
|
| 171 |
stop_live = st.button("Stop Live Detection", key="yt_stop_loop")
|
| 172 |
cap.release()
|
| 173 |
except Exception as e:
|