-
Notifications
You must be signed in to change notification settings - Fork 0
/
fire_detection.py
87 lines (68 loc) · 2.27 KB
/
fire_detection.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cv2
import numpy as np
import smtplib
from datetime import datetime
#import playsound
import threading
#Alarm_Status = False
Email_Status = False
Fire_Reported = 0
#def play_alarm_sound_function():
# while True:
# playsound.playsound('alarm-sound.mp3',True)
def send_mail_function():
recipientEmail = "saptajitbanerjee2002@gmail.com"
recipientEmail = recipientEmail.lower()
try:
smtpUser='jcomponentrasberrypibot@gmail.com'
smtpPass='waumkjgbiebmgmua'
toAdd='saptajitbanerjee2002@gmail.com'
fromAdd = smtpUser
subject='WARNING! FIRE Detected!'
header='To:'+toAdd+'\n'+'From:'+fromAdd+'\n'+'Subject:'+subject
now = datetime.now()
body='Fire has been detected at time: '+now.strftime("%m/%d/%Y, %H:%M:%S")
print(header+'\n'+body)
s = smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpUser,smtpPass)
print("Login Successful")
s.sendmail(fromAdd,toAdd,header+'\n\n'+body)
s.quit()
except Exception as e:
print(e)
video = cv2.VideoCapture("http://192.168.215.45:8000/stream.mjpg") # If you want to use webcam use Index like 0,1.
while True:
(grabbed, frame) = video.read()
if not grabbed:
break
frame = cv2.resize(frame, (960, 540))
blur = cv2.GaussianBlur(frame, (21, 21), 0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower = [18, 50, 50]
upper = [35, 255, 255]
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
mask = cv2.inRange(hsv, lower, upper)
#output = cv2.bitwise_and(frame, hsv, mask=mask)
no_red = cv2.countNonZero(mask)
if int(no_red) > 20000:
Fire_Reported = Fire_Reported + 1
#cv2.imshow("output", output)
#ret, frame = video.read()
#cv2.imshow('frame', frame)
if Fire_Reported >= 1:
print("Fire Detected")
#if Alarm_Status == False:
#threading.Thread(target=play_alarm_sound_function).start()
#Alarm_Status = True
if Email_Status == False:
threading.Thread(target=send_mail_function).start()
Email_Status = True
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
video.release()