-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.py
60 lines (49 loc) · 2.14 KB
/
watch.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
import cv2 # importing Python OpenCV
import imutils
delta_thresh = 5
min_area = 5000
threshold = 81500 # Threshold for triggering "motion detection"
cam = cv2.VideoCapture(0) # Lets initialize capture on webcam
avg = None
while True:
frame = cam.read()[1]
text = "Not Moving"
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
grey = cv2.GaussianBlur(grey, (21, 21), 0)
# if the average frame is None, initialize it
if avg is None:
print("[INFO] starting background model...")
avg = grey.copy().astype("float")
continue
# accumulate the weighted average between the current frame and
# previous frames, then compute the difference between the current
# frame and running average
cv2.accumulateWeighted(grey, avg, 0.5)
frameDelta = cv2.absdiff(grey, cv2.convertScaleAbs(avg))
# threshold the delta image, dilate the thresholded image to fill
# in holes, then find contours on thresholded image
thresh = cv2.threshold(frameDelta, delta_thresh, 255,
cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
# print cv2.contourArea(c)
if cv2.contourArea(c) < min_area:
continue
# compute the bounding box for the contour, draw it on the frame,
# and update the text
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Moving"
cv2.putText(frame, text, (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow( "Movement Indicator", frame ) # comment to hide window
cv2.imshow( 'frameDelta', frameDelta)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName) # comment to hide window
break