-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion-det2.py
126 lines (110 loc) · 5.09 KB
/
motion-det2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import threading
import cv2 # pip install opencv-python
# import imutils #pip install imutils
import time
# importing mail module
from send_mail import prepare_and_send_email
src = "http://192.168.29.34:8080/video"
# start the webcam
cap = cv2.VideoCapture(src)
# skipping the first frame
_, _ = cap.read()
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) # Considering the width as 640
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) # Considering the height as 480
# reading the 2nd frame
_, start_frame = cap.read()
start_frame = cv2.resize(start_frame, (500, 480)) # Resize the first frame
start_frame = cv2.cvtColor(start_frame, cv2.COLOR_BGR2GRAY) # Change the color image to gray scale image
# cv2.imshow("cam" , start_frame)
# cv2.waitKey(5000)
start_frame = cv2.GaussianBlur(start_frame, (5, 5), 0) # Smoothen the image ie;blur
# cv2.imshow("cam" , start_frame)
# cv2.waitKey(5000)
next_send_mail = True
count_frame = 0 # to keep track of frames in which motion is observed
def alert(frame):
global next_send_mail
next_send_mail = False
prepare_and_send_email('[email protected]',
'Alert Alert Alert',
'Motion is detected at the premises',
frame)
time.sleep(120)
next_send_mail = True
def motion_detection():
global count_frame, start_frame, next_send_mail
while True:
success, frame = cap.read() # Reads the frame
if success:
frame = cv2.resize(frame, (500, 480)) # resize the frame to width 500
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # change color to gray scale
# cv2.imshow("cam" , gray_frame)
# cv2.waitKey(5000)
gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0) # it will smoothen the image
difference = cv2.absdiff(gray_frame,
start_frame) # it will find the absolute difference two gray scaled images
'''
difference depicts difference in the pixels of two images'''
# print(difference)
# print(type(difference))
threshold = cv2.threshold(difference, 50, 255, cv2.THRESH_BINARY)[1]
'''
Applied general thresholding
para1 : image,
para2 : threshold value it lies [0,255],
para3 : max value of pixel,
para4 : type of thresholding - simple
return:
first-op : the threshold value,
second-op : the threshold image'''
# print(type(threshold))
# print(threshold)
threshold = cv2.dilate(threshold, None, iterations=3)
'''
'''
cnts, res = cv2.findContours(threshold.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print(cnts)
# contours are the boundaries that separate an object from its background.
for contour in cnts:
if cv2.contourArea(contour) >= 3000:
(x, y, w, h) = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (0, 255, 0), 3)
'''cv2.putText(frame, "STATUS: {}".format('MOTION DETECTED'), (5, 40), cv2.FONT_HERSHEY_SIMPLEX,
1, (217, 10, 10), 2)'''
'''for contour in cnts:
if cv2.contourArea(contour) >= 1500:
count_frame += 1
if count_frame >= 5 and next_send_mail is True:
count_frame = 0
thread_1 = threading.Thread(target=Alert, args=(frame_copy,))
thread_1.start()
(x,y,w,h) = cv2.boundingRect(contour)
cv2.rectangle(frame , (x,y) ,((x+w) , (y+h)) , (0,255,0) , 3)
cv2.putText(frame, "STATUS: {}".format('MOTION DETECTED'), (10, 60), cv2.FONT_HERSHEY_SIMPLEX,
1, (217, 10, 10), 2)'''
if threshold.sum() > 1000: # threshold.sum() is the sum of the pixels
if count_frame < 5:
count_frame += 1
elif count_frame == 5 and next_send_mail is True:
frame = cv2.putText(frame, "STATUS: {}".format('MOTION DETECTED'), (10, 60),
cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 2) # (217,10,10)
# time.sleep(30)
threading.Thread(target=alert,
args=(frame,)).start()
count_frame = 0
start_frame =gray_frame
cv2.imshow("stream", frame)
cv2.imshow("threshold stream" , threshold)
start_frame = gray_frame
# key_pressed = cv2.waitKey(0)
# key = cv2.waitKey(0)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if not cap.isOpened():
print("Error: Could not connect to your camera.Kindly try again")
else:
motion_detection()