-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect_noise.py
68 lines (48 loc) · 1.8 KB
/
rect_noise.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
import cv2
donel = False
doner = False
x1,y1,x2,y2 = 0,0,0,0
def select(event, x, y, flag, param):
global x1,x2,y1,y2,donel, doner
if event == cv2.EVENT_LBUTTONDOWN:
x1,y1 = x,y
donel = True
elif event == cv2.EVENT_RBUTTONDOWN:
x2,y2 = x,y
doner = True
print(doner, donel)
def rect_noise():
global x1,x2,y1,y2, donel, doner
cap = cv2.VideoCapture(0)
cv2.namedWindow("select_region")
cv2.setMouseCallback("select_region", select)
while True:
_, frame = cap.read()
cv2.imshow("select_region", frame)
if cv2.waitKey(1) == 27 or doner == True:
cv2.destroyAllWindows()
print("gone--")
break
while True:
_, frame1 = cap.read()
_, frame2 = cap.read()
frame1only = frame1[y1:y2, x1:x2]
frame2only = frame2[y1:y2, x1:x2]
diff = cv2.absdiff(frame2only, frame1only)
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+x1, y+y1), (x+w+x1, y+h+y1), (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.rectangle(frame1, (x1,y1), (x2, y2), (0,0,255), 1)
cv2.imshow("esc. to exit", frame1)
if cv2.waitKey(1) == 27:
cap.release()
cv2.destroyAllWindows()
break