-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_motion.py
77 lines (52 loc) · 1.47 KB
/
find_motion.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
import cv2
from spot_diff import spot_diff
import time
import numpy as np
def find_motion():
motion_detected = False
is_start_done = False
cap = cv2.VideoCapture(0)
check = []
print("waiting for 2 seconds")
time.sleep(2)
frame1 = cap.read()
_, frm1 = cap.read()
frm1 = cv2.cvtColor(frm1, cv2.COLOR_BGR2GRAY)
while True:
_, frm2 = cap.read()
frm2 = cv2.cvtColor(frm2, cv2.COLOR_BGR2GRAY)
diff = cv2.absdiff(frm1, frm2)
_, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
contors = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
#look at it
contors = [c for c in contors if cv2.contourArea(c) > 25]
if len(contors) > 5:
cv2.putText(thresh, "motion detected", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
motion_detected = True
is_start_done = False
elif motion_detected and len(contors) < 3:
if (is_start_done) == False:
start = time.time()
is_start_done = True
end = time.time()
end = time.time()
print(end-start)
if (end - start) > 4:
frame2 = cap.read()
cap.release()
cv2.destroyAllWindows()
x = spot_diff(frame1, frame2)
if x == 0:
print("running again")
return
else:
print("found motion")
return
else:
cv2.putText(thresh, "no motion detected", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
cv2.imshow("winname", thresh)
_, frm1 = cap.read()
frm1 = cv2.cvtColor(frm1, cv2.COLOR_BGR2GRAY)
if cv2.waitKey(1) == 27:
break
return