-
Notifications
You must be signed in to change notification settings - Fork 0
/
testfile1.py
60 lines (47 loc) · 3.03 KB
/
testfile1.py
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
import cv2
import threading # Library for threading -- which allows code to run in backend
import playsound # Library for alarm sound
from playsound import playsound
import smtplib # Library for email sending
fire_cascade = cv2.CascadeClassifier('C:\\Users\\ICT_LAB\\AppData\\Local\\Programs\\Python\\Python310\\python_fire_project\\fire_detection_cascade_model.xml') # To access xml file which includes positive and negative images of fire. (Trained images)
# File is also provided with the code.
vid = cv2.VideoCapture(0) # To start camera this command is used "0" for laptop inbuilt camera and "1" for USB attahed camera
runOnce = False # created boolean
def play_alarm_sound_function(): # defined function to play alarm post fire detection using threading
playsound('C:\\Users\\ICT_LAB\\AppData\\Local\\Programs\\Python\\Python310\\python_fire_project\\Fire_alarm.mp3',True) # to play alarm # mp3 audio file is also provided with the code.
print("Fire alarm end") # to print in consol
def send_mail_function(): # defined function to send mail post fire detection using threading
recipientmail = "localfirestation@gmail.com" # recipients mail/ please use a real email while testing
recipientmail = recipientmail.lower() # To lower case mail
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("senderemail@gmail.com", 'password') # Senders mail ID and password / please use a real email while testing
server.sendmail('localfirestation@gmail.com', recipientmail, "Test mail Warning fire accident has been reported") # recipients mail with mail message / please use a real email while testing
print("Alert mail sent sucesfully to {}".format(recipientmail)) # to print in consol to whome mail is sent
server.close() ## To close server
except Exception as e:
print(e) # To print error if any
while(True):
Alarm_Status = False
ret, frame = vid.read() # Value in ret is True # To read video frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # To convert frame into gray color
fire = fire_cascade.detectMultiScale(frame, 1.2, 5) # to provide frame resolution
## 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 alarm 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