Prathamesh1420 commited on
Commit
9d95b96
·
verified ·
1 Parent(s): 226383e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -2
app.py CHANGED
@@ -43,7 +43,10 @@ if image is not None:
43
  # Display the annotated image
44
  st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
45
  '''
46
- import cv2
 
 
 
47
  import numpy as np
48
  import torch
49
  import streamlit as st
@@ -108,4 +111,78 @@ if image is not None:
108
  st.success("✅ No Fire Detected")
109
  if fire_detected:
110
  pygame.mixer.music.stop() # Stop alarm
111
- fire_detected = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # Display the annotated image
44
  st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
45
  '''
46
+
47
+
48
+
49
+ '''import cv2
50
  import numpy as np
51
  import torch
52
  import streamlit as st
 
111
  st.success("✅ No Fire Detected")
112
  if fire_detected:
113
  pygame.mixer.music.stop() # Stop alarm
114
+ fire_detected = False
115
+ '''
116
+ import cv2
117
+ import numpy as np
118
+ import torch
119
+ import streamlit as st
120
+ import os
121
+ from ultralytics import YOLO
122
+ from camera_input_live import camera_input_live
123
+
124
+ # Load YOLO fire detection model
125
+ model_path = "last.pt"
126
+ device = "cuda" if torch.cuda.is_available() else "cpu"
127
+ model = YOLO(model_path)
128
+ model.to(device)
129
+
130
+ # Streamlit app title
131
+ st.title("Live Fire Detection with Camera")
132
+ st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
133
+
134
+ # Capture live camera input
135
+ image = camera_input_live()
136
+ fire_detected = False # Track fire detection state
137
+
138
+ # HTML & JS for Alarm Sound
139
+ alarm_html = """
140
+ <audio id="fireAlarm" src="alarm.mp3"></audio>
141
+ <script>
142
+ function playAlarm() {
143
+ var alarm = document.getElementById("fireAlarm");
144
+ if (alarm.paused) {
145
+ alarm.play();
146
+ }
147
+ }
148
+ function stopAlarm() {
149
+ var alarm = document.getElementById("fireAlarm");
150
+ alarm.pause();
151
+ alarm.currentTime = 0;
152
+ }
153
+ </script>
154
+ """
155
+
156
+ if image is not None:
157
+ # Convert the image to OpenCV format
158
+ bytes_data = image.getvalue()
159
+ cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
160
+
161
+ # Perform fire detection
162
+ results = model(cv2_img)
163
+
164
+ fire_present = False # Temporary flag for fire detection in this frame
165
+
166
+ # Draw bounding boxes for detected fires
167
+ for result in results:
168
+ boxes = result.boxes
169
+ for box in boxes:
170
+ b = box.xyxy[0].cpu().numpy().astype(int)
171
+ label = f'Fire {box.conf[0]:.2f}'
172
+ cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
173
+ cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
174
+ fire_present = True # Fire detected
175
+
176
+ # Display the annotated image
177
+ st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
178
+
179
+ # Display logs and trigger alarm
180
+ if fire_present:
181
+ st.error("🔥 Fire Detected! 🔥")
182
+ st.markdown(alarm_html + "<script>playAlarm();</script>", unsafe_allow_html=True)
183
+ fire_detected = True
184
+ else:
185
+ st.success("✅ No Fire Detected")
186
+ if fire_detected:
187
+ st.markdown(alarm_html + "<script>stopAlarm();</script>", unsafe_allow_html=True)
188
+ fire_detected = False