Skip to content

Commit

Permalink
Added fire detection alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishisht16 committed Oct 6, 2024
1 parent 2ccb5c1 commit cf6f88b
Show file tree
Hide file tree
Showing 5 changed files with 1,428 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
15 changes: 15 additions & 0 deletions Computer Vision/Fire Detection Alarm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Fire Alarm Detection System

This project is a real-time fire detection system using OpenCV, threading, and email alerts. It utilizes a pre-trained fire detection cascade model and sends an alert via email and plays an alarm sound when fire is detected from video input.

## Features
- **Real-time fire detection** using OpenCV.
- **Alarm sound** triggered upon detection.
- **Email alert system** that sends notifications in case of fire.
- Simple and effective fire detection from live video or video files.

## Requirements
- OpenCV
- Threading
- Playsound
- smtplib
153 changes: 153 additions & 0 deletions Computer Vision/Fire Detection Alarm/fire detection system.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "7f5f4af0-a698-4933-9729-4496f379a53b",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import threading \n",
"import playsound \n",
"import smtplib "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2266a293-7af0-4ebd-a45d-5791991cee9c",
"metadata": {},
"outputs": [],
"source": [
"fire_cascade = cv2.CascadeClassifier('fire-cascade.xml')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "499a0627-345c-4b0a-ae94-8dfed4f93b40",
"metadata": {},
"outputs": [],
"source": [
"# Use \"0\" for laptop inbuilt camera and \"1\" for USB attached camera or webcam\n",
"# vid = cv2.VideoCapture(0) \n",
"\n",
"vid = cv2.VideoCapture(\"videos\\\\fire2.mp4\")\n",
"runOnce = False # created boolean"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e8fe66e3-62fe-493b-82be-7b58963f575d",
"metadata": {},
"outputs": [],
"source": [
"# Alarm is played after fire detection\n",
"\n",
"def play_alarm_sound_function(): \n",
" playsound.playsound('fire-alarm.mp3',True) \n",
" print(\"Fire alarm end\") # for testing"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f1e4051f-1c22-423e-a06f-62225110181d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Failed to grab frame\n"
]
}
],
"source": [
"# Mail is sent after fire detection\n",
"\n",
"def send_mail_function(): \n",
" \n",
" recipientmail = \"[email protected]\" # recipients mail\n",
" recipientmail = recipientmail.lower()\n",
" \n",
" try:\n",
" server = smtplib.SMTP('smtp.gmail.com', 587)\n",
" server.ehlo()\n",
" server.starttls()\n",
" server.login(\"asender email\", 'sender password') # Logs using sender's credentials\n",
" server.sendmail('add recipients mail', recipientmail, \"REPORT! FIRE ACCIDENT DETECTED\") \n",
" print(\"Alert mail sent sucesfully to {}\".format(recipientmail)) # for testing\n",
" server.close()\n",
" \n",
" except Exception as e:\n",
" print(e)\n",
"\t\t\n",
"while(True):\n",
" Alarm_Status = False\n",
" ret, frame = vid.read() \n",
" # Checks for empty frame, always prints during tests without video input\n",
" if not ret:\n",
" print(\"Failed to grab frame\")\n",
" break\n",
" \n",
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) \n",
" fire = fire_cascade.detectMultiScale(frame, 1.2, 5) \n",
"\n",
" ## to highlight fire with square \n",
" for (x,y,w,h) in fire:\n",
" cv2.rectangle(frame,(x-20,y-20),(x+w+20,y+h+20),(255,0,0),2)\n",
" roi_gray = gray[y:y+h, x:x+w]\n",
" roi_color = frame[y:y+h, x:x+w]\n",
"\n",
" print(\"Fire alarm initiated\")\n",
" # To call alarm thread\n",
" threading.Thread(target=play_alarm_sound_function).start() \n",
"\n",
" if runOnce == False:\n",
" print(\"Mail send initiated\")\n",
" # To call alarm thread\n",
" threading.Thread(target=send_mail_function).start() \n",
" runOnce = True\n",
" if runOnce == True:\n",
" print(\"Mail is already sent once\")\n",
" runOnce = True\n",
"\n",
" cv2.imshow('frame', frame)\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e94e5f77-7234-45f3-91c1-8d47ce91d593",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file not shown.
Loading

0 comments on commit cf6f88b

Please sign in to comment.