-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFire.py
More file actions
72 lines (56 loc) · 2.58 KB
/
Copy pathFire.py
File metadata and controls
72 lines (56 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import cv2 # Library for openCV
import threading # Library for threading -- which allows code to run in backend
import playsound # Library for alarm sound
import smtplib # Library for email sending
# To access xml file which includes positive and negative images of fire. (Trained images)
fire_cascade = cv2.CascadeClassifier('fire_detection.xml')
# To start camera this command is used "0" for laptop inbuilt camera and "1" for USB attahed camera
vid = cv2.VideoCapture(0)
runOnce = False
def play_alarm_sound_function():
# defined function to play alarm post fire detection using threading
playsound.playsound('fire_Alarm.mp3',True)
# to play alarm # mp3 audio file is also provided with the code.
print("Fire alarm end")
def send_mail_function():
# defined function to send mail post fire detection using threading
recipientmail = # recipients mail
recipientmail = recipientmail.lower() # To lower case mail
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
# Senders mail ID and password
server.login("sender's mail", Password)
# recipients mail with mail message
server.sendmail('recipient mail', recipientmail, "Warning fire accident has been reported")
print("Alert mail sent sucesfully to {}".format(recipientmail))
server.close()
except Exception as e:
print("error") #For error
while(True):
Alarm_Status = False
# Value in ret is True
# To read video frame
ret, frame = vid.read()
# To convert frame into gray color
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# to provide frame resolution
fire = fire_cascade.detectMultiScale(frame, 1.2, 5)
## to highlight fire with square
for (x,y,w,h) in fire:
cv2.rectangle(frame,(x-20,y-20),(x+w+20,y+h+20),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
print("Fire alarm initiated")
threading.Thread(target=play_alarm_sound_function).start() # To call alarm thread
if runOnce == False:
print("Mail send initiated")
threading.Thread(target=send_mail_function).start() # To call mail thread
runOnce = True
if runOnce == True:
print("Mail is already sent once")
runOnce = True
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break