-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfire-detector.py
76 lines (53 loc) · 1.82 KB
/
fire-detector.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
import cv2
import numpy as np
import smtplib
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 = "Enter_Recipient_Email"
recipientEmail = recipientEmail.lower()
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("Enter_Your_Email (System Email)", 'Enter_Your_Email_Password (System Email')
server.sendmail('Enter_Your_Email (System Email)', recipientEmail, "Warning A Fire Accident has been reported on ABC Company")
print("sent to {}".format(recipientEmail))
server.close()
except Exception as e:
print(e)
video = cv2.VideoCapture("video_file") # 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) > 15000:
Fire_Reported = Fire_Reported + 1
cv2.imshow("output", output)
if Fire_Reported >= 1:
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
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
video.release()