-
Notifications
You must be signed in to change notification settings - Fork 1
/
in_out.py
123 lines (60 loc) · 2.25 KB
/
in_out.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
import cv2
import numpy as np
import winsound
def in_out():
cap = cv2.VideoCapture(0)
_, frame1 = cap.read()
left, center, right = False, False, False
x=300
mask = np.zeros((200, 400))
while True:
_, frame2 = cap.read()
g1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
g2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
diff = cv2.absdiff(g1, g2)
_, thresh = cv2.threshold(diff, 50, 255, cv2.THRESH_BINARY)
contr, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(contr) > 0:
contr = max(contr, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(contr)
cv2.rectangle(frame2, (x,y), (x+w, y+h), (0,255,0), 2)
if not(left) and not(right):
if x < 100:
left = True
elif x > 500:
right = True
elif left :
if x < 450 and x > 200 and not(center):
print(x," x has been set to it")
center = True
winsound.Beep(500, 200)
elif x > 500:
if center:
print("motion to left taken place")
mask = np.zeros((200,450))
cv2.putText(mask, "Exit-Left", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255), 3)
left = False
center = False
else:
right = True
elif right :
if x < 450 and x > 200 and not(center):
print(x," x has been set to it")
center = True
if x < 100:
if center:
print("motion done to the right side")
mask = np.zeros((200, 450))
cv2.putText(mask, "Enter-Right", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255), 3)
right = False
center = False
else:
left = True
cv2.imshow("window", thresh)
cv2.imshow("window2", frame2)
cv2.imshow("mask", mask)
_, frame1 = cap.read()
if cv2.waitKey(1) == 27:
cv2.destroyAllWindow
cap.release()
break