Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,44 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
|
|
3 |
import streamlit as st
|
|
|
4 |
from camera_input_live import camera_input_live
|
5 |
|
6 |
-
# Load
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Streamlit app title
|
10 |
-
st.title("Live
|
11 |
-
st.subheader("Hold
|
12 |
|
13 |
# Capture live camera input
|
14 |
image = camera_input_live()
|
15 |
|
16 |
if image is not None:
|
17 |
-
|
18 |
# Convert the image to OpenCV format
|
19 |
bytes_data = image.getvalue()
|
20 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
# Detect faces in the image
|
26 |
-
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
|
27 |
|
28 |
-
# Draw
|
29 |
-
for
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Display the annotated image
|
34 |
-
st.image(cv2_img, channels="BGR", caption="Detected
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
import torch
|
4 |
import streamlit as st
|
5 |
+
from ultralytics import YOLO
|
6 |
from camera_input_live import camera_input_live
|
7 |
|
8 |
+
# Load YOLO fire detection model
|
9 |
+
model_path = "last.pt"
|
10 |
+
if not torch.cuda.is_available():
|
11 |
+
device = "cpu"
|
12 |
+
else:
|
13 |
+
device = "cuda"
|
14 |
+
|
15 |
+
model = YOLO(model_path)
|
16 |
+
model.to(device)
|
17 |
|
18 |
# Streamlit app title
|
19 |
+
st.title("Live Fire Detection with Camera")
|
20 |
+
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
|
21 |
|
22 |
# Capture live camera input
|
23 |
image = camera_input_live()
|
24 |
|
25 |
if image is not None:
|
|
|
26 |
# Convert the image to OpenCV format
|
27 |
bytes_data = image.getvalue()
|
28 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
29 |
|
30 |
+
# Perform fire detection
|
31 |
+
results = model(cv2_img, device=device)
|
|
|
|
|
|
|
32 |
|
33 |
+
# Draw bounding boxes for detected fires
|
34 |
+
for result in results:
|
35 |
+
boxes = result.boxes
|
36 |
+
for box in boxes:
|
37 |
+
b = box.xyxy[0].cpu().numpy().astype(int)
|
38 |
+
c = int(box.cls[0])
|
39 |
+
label = f'Fire {box.conf[0]:.2f}'
|
40 |
+
cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
|
41 |
+
cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
42 |
|
43 |
# Display the annotated image
|
44 |
+
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
|