ArchiMathur commited on
Commit
42df645
·
verified ·
1 Parent(s): 7d7fdc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -26
app.py CHANGED
@@ -1,48 +1,49 @@
1
-
2
-
3
- from ultralytics import YOLO
4
  import streamlit as st
 
5
  import numpy as np
6
- from PIL import Image
7
- model=YOLO("best.pt")
 
 
 
8
  # Set the title of the app
9
- st.title("Fire Detection App")
10
 
11
- # Sidebar for input options
12
- input_option = st.sidebar.selectbox("Select Input Method", ["Upload Image"])
13
 
14
- if input_option == "Upload Image":
15
- # Upload Image
16
- uploaded_file = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])
 
17
 
18
- if uploaded_file is not None:
19
- # Open and display the uploaded image
20
- img = Image.open(uploaded_file)
21
- st.image(img, caption='User Image', use_column_width=True)
22
-
23
- # Convert the image to a numpy array
24
- img_np = np.array(img)
25
 
26
- # Make predictions
27
- results = model.predict(source=img_np, conf=0.5) # Adjust confidence threshold as needed
28
 
29
- # Variable to check if fire is detected
30
- fire_detected = False
31
 
32
- # Draw bounding boxes on the image
 
33
  for result in results:
34
  boxes = result.boxes.xyxy # Bounding boxes
35
  for box in boxes:
36
  x1, y1, x2, y2 = box[:4].astype(int)
37
- img_np = cv2.rectangle(img_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
38
 
39
  # Check if the detected class is "fire" (adjust based on your model's class mapping)
40
  class_id = int(box[5]) # Assuming class ID is at the 6th position
41
  if class_id == 0: # Replace 0 with the actual class ID for fire if different
42
  fire_detected = True
43
 
44
- # Show the resulting image with bounding boxes
45
- st.image(img_np, caption='Detected Fire', use_column_width=True)
46
 
47
  # Display message based on fire detection
48
  if fire_detected:
@@ -50,3 +51,14 @@ if input_option == "Upload Image":
50
  else:
51
  st.warning("No Fire Detected.")
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import cv2
3
  import numpy as np
4
+ from ultralytics import YOLO
5
+
6
+ # Load the YOLO model
7
+ model = YOLO("best.pt") # Ensure the path to your model is correct
8
+
9
  # Set the title of the app
10
+ st.title("Live Fire Detection App")
11
 
12
+ # Create a placeholder for the video stream
13
+ video_placeholder = st.empty()
14
 
15
+ # Function to perform live fire detection
16
+ def detect_fire():
17
+ # Open a connection to the webcam (0 is the default camera)
18
+ cap = cv2.VideoCapture(0)
19
 
20
+ while True:
21
+ ret, frame = cap.read()
22
+ if not ret:
23
+ st.error("Failed to capture video.")
24
+ break
 
 
25
 
26
+ # Convert the frame to RGB (as YOLO expects RGB input)
27
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
28
 
29
+ # Make predictions on the current frame
30
+ results = model.predict(source=rgb_frame, conf=0.5)
31
 
32
+ # Draw bounding boxes on the frame
33
+ fire_detected = False
34
  for result in results:
35
  boxes = result.boxes.xyxy # Bounding boxes
36
  for box in boxes:
37
  x1, y1, x2, y2 = box[:4].astype(int)
38
+ rgb_frame = cv2.rectangle(rgb_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
39
 
40
  # Check if the detected class is "fire" (adjust based on your model's class mapping)
41
  class_id = int(box[5]) # Assuming class ID is at the 6th position
42
  if class_id == 0: # Replace 0 with the actual class ID for fire if different
43
  fire_detected = True
44
 
45
+ # Display the frame in the Streamlit app
46
+ video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
47
 
48
  # Display message based on fire detection
49
  if fire_detected:
 
51
  else:
52
  st.warning("No Fire Detected.")
53
 
54
+ # Break the loop if the user presses 'q'
55
+ if cv2.waitKey(1) & 0xFF == ord('q'):
56
+ break
57
+
58
+ # Release the webcam and close windows
59
+ cap.release()
60
+ cv2.destroyAllWindows()
61
+
62
+ # Start the detection process
63
+ if st.button("Start Live Detection"):
64
+ detect_fire()