Prathamesh1420 commited on
Commit
f55134b
·
verified ·
1 Parent(s): d4b30d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -16,17 +16,21 @@ model.to(device)
16
  st.title("🔥 Live Fire Detection with Alarm 🔥")
17
  st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
18
 
19
- # Load alarm sound from a direct MP3 URL
20
- alarm_url = "https://docs.google.com/uc?export=download&id=16IzsnQDmWkfYSeb_AjOTx79NEgkOpz88" # Replace with a working direct MP3 URL
21
 
22
- # JavaScript to control alarm playback
23
  js_code = f"""
24
  <script>
25
  var alarm = new Audio("{alarm_url}");
 
 
26
  function playAlarm() {{
27
- alarm.loop = true;
28
- alarm.play();
 
29
  }}
 
30
  function stopAlarm() {{
31
  alarm.pause();
32
  alarm.currentTime = 0;
@@ -34,24 +38,29 @@ js_code = f"""
34
  </script>
35
  """
36
 
37
- # Inject JavaScript once
38
  components.html(js_code, height=0)
39
 
40
  # Capture live camera input
41
  image = camera_input_live()
42
 
43
  if image is not None:
44
- # Convert image to OpenCV format
45
  bytes_data = image.getvalue()
46
  cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
47
 
48
  # Perform fire detection
49
  results = model(cv2_img)
 
 
50
 
51
  # Check if fire is detected
52
- fire_present = any(len(result.boxes) > 0 for result in results)
 
 
 
53
 
54
- # Show logs in UI & trigger alarm
55
  if fire_present:
56
  st.error("🔥 Fire Detected! 🔥")
57
  components.html("<script>playAlarm();</script>", height=0)
 
16
  st.title("🔥 Live Fire Detection with Alarm 🔥")
17
  st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
18
 
19
+ # Load alarm sound (must be a direct MP3 URL)
20
+ alarm_url = "https://www.soundjay.com/button/beep-07.wav" # Replace with your hosted alarm
21
 
22
+ # JavaScript to auto-play alarm when fire is detected
23
  js_code = f"""
24
  <script>
25
  var alarm = new Audio("{alarm_url}");
26
+ alarm.loop = true;
27
+
28
  function playAlarm() {{
29
+ alarm.play().catch(error => {{
30
+ console.log("Autoplay failed. User interaction required.");
31
+ }});
32
  }}
33
+
34
  function stopAlarm() {{
35
  alarm.pause();
36
  alarm.currentTime = 0;
 
38
  </script>
39
  """
40
 
41
+ # Inject JavaScript
42
  components.html(js_code, height=0)
43
 
44
  # Capture live camera input
45
  image = camera_input_live()
46
 
47
  if image is not None:
48
+ # Convert the image to OpenCV format
49
  bytes_data = image.getvalue()
50
  cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
51
 
52
  # Perform fire detection
53
  results = model(cv2_img)
54
+
55
+ fire_present = False # Flag for fire detection
56
 
57
  # Check if fire is detected
58
+ for result in results:
59
+ if len(result.boxes) > 0:
60
+ fire_present = True
61
+ break # No need to check further
62
 
63
+ # Display logs & trigger alarm
64
  if fire_present:
65
  st.error("🔥 Fire Detected! 🔥")
66
  components.html("<script>playAlarm();</script>", height=0)