Prathamesh1420 commited on
Commit
452a698
·
verified ·
1 Parent(s): 78b0fae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import cv2
2
  import numpy as np
3
  import torch
4
  import streamlit as st
@@ -42,3 +42,66 @@ if image is not None:
42
 
43
  # Display the annotated image
44
  st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''import cv2
2
  import numpy as np
3
  import torch
4
  import streamlit as st
 
42
 
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
50
+ import pygame
51
+ from ultralytics import YOLO
52
+ from camera_input_live import camera_input_live
53
+
54
+ # Initialize Pygame mixer for audio alarm
55
+ pygame.mixer.init()
56
+ alarm_sound = "alarm.mp3" # Ensure you have an alarm.mp3 file in your directory
57
+
58
+ # Load YOLO fire detection model
59
+ model_path = "last.pt"
60
+ device = "cuda" if torch.cuda.is_available() else "cpu"
61
+
62
+ model = YOLO(model_path)
63
+ model.to(device)
64
+
65
+ # Streamlit app title
66
+ st.title("Live Fire Detection with Camera")
67
+ st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
68
+
69
+ # Capture live camera input
70
+ image = camera_input_live()
71
+ fire_detected = False # Track fire detection state
72
+
73
+ if image is not None:
74
+ # Convert the image to OpenCV format
75
+ bytes_data = image.getvalue()
76
+ cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
77
+
78
+ # Perform fire detection
79
+ results = model(cv2_img)
80
+
81
+ fire_present = False # Temporary flag for fire detection in this frame
82
+
83
+ # Draw bounding boxes for detected fires
84
+ for result in results:
85
+ boxes = result.boxes
86
+ for box in boxes:
87
+ b = box.xyxy[0].cpu().numpy().astype(int)
88
+ label = f'Fire {box.conf[0]:.2f}'
89
+ cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
90
+ cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
91
+ fire_present = True # Fire detected
92
+
93
+ # Display the annotated image
94
+ st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
95
+
96
+ # Display logs
97
+ if fire_present:
98
+ st.error("🔥 Fire Detected! 🔥")
99
+ if not fire_detected: # Play alarm if not already playing
100
+ pygame.mixer.music.load(alarm_sound)
101
+ pygame.mixer.music.play(-1) # Loop indefinitely
102
+ fire_detected = True # Update fire status
103
+ else:
104
+ st.success("✅ No Fire Detected")
105
+ if fire_detected:
106
+ pygame.mixer.music.stop() # Stop alarm
107
+ fire_detected = False