Spaces:
Sleeping
Sleeping
Download app.py from qamarekamil/Fire_Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/qamarekamil/Fire_Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/qamarekamil/Fire_Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/qamarekamil/Fire_Detection/resolve/main/app.py
1.41 kB
| import gradio as gr | |
| from transformers import pipeline | |
| from PIL import Image | |
| from playsound import playsound | |
| import threading | |
| # Load the fire detection model | |
| fire_detector = pipeline("image-classification", model="your-huggingface-model") | |
| # Function to play alarm sound | |
| def play_alarm(): | |
| playsound("alarm.mp3") # Ensure you have an alarm.mp3 file in the same directory | |
| # Function to detect fire in an image | |
| def detect_fire(image): | |
| # Perform fire detection | |
| results = fire_detector(image) | |
| # Check if fire is detected | |
| fire_detected = False | |
| for result in results: | |
| if result['label'] == 'fire' and result['score'] > 0.9: # Adjust threshold as needed | |
| fire_detected = True | |
| break | |
| # Display results | |
| if fire_detected: | |
| # Trigger alarm in a separate thread | |
| threading.Thread(target=play_alarm).start() | |
| return "Fire Detected! 🔥 Alarm Triggered!", image | |
| else: | |
| return "No Fire Detected. ✅", image | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=detect_fire, | |
| inputs=gr.Image(label="Upload or Capture Image", source="webcam"), # Enable webcam | |
| outputs=[gr.Textbox(label="Detection Result"), gr.Image(label="Processed Image")], | |
| title="Fire Detection System", | |
| description="Use your webcam to detect fire in real-time. If fire is detected, an alarm will sound." | |
| ) | |
| # Launch the app | |
| interface.launch() |