-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion.py
32 lines (23 loc) · 993 Bytes
/
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
import cv2
def noise():
cap = cv2.VideoCapture(0)
while True:
_, frame1 = cap.read()
_, frame2 = cap.read()
diff = cv2.absdiff(frame2, frame1)
diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
diff = cv2.blur(diff, (5,5))
_, thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)
contr, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(contr) > 0:
max_cnt = max(contr, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(max_cnt)
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0,255,0), 2)
cv2.putText(frame1, "MOTION", (10,80), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
else:
cv2.putText(frame1, "NO-MOTION", (10,80), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2)
cv2.imshow("esc. to exit", frame1)
if cv2.waitKey(1) == 27:
cap.release()
cv2.destroyAllWindows()
break