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

Update app.py

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