-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.py
More file actions
executable file
·91 lines (70 loc) · 2.77 KB
/
security.py
File metadata and controls
executable file
·91 lines (70 loc) · 2.77 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
import cv2
import time as tme
from pygame import mixer
# Webcam Motion Detector
# The BACKGROUND_REFRESH_RATE helps to control the sensitivity of the motion.
# Lower values means less sensitivity
BACKGROUND_REFRESH_RATE = 10
# This value controls how much difference there has to be between the pixels
# before it is considered different. Higher values means less sensitivity.
SENSITIVITY_THRESHOLD = 80
# Helps to only alert on large motion patches. Higher number means less sensitivity.
MOTION_SIZE = 10000
# This will be used to compare the current frame to the frame from some time ago.
static_background = None
# Get some sound ready to play on motion detection
mixer.init()
mixer.music.load("./buzzer.mp3")
# Capturing video
# on my setup
# 0 is laptop Webcam
# 1 is a usb webcam
video = cv2.VideoCapture(0)
# Keep track of an iteration, so we know how often to refresh the
# background image we are comparing to.
iteration = BACKGROUND_REFRESH_RATE
# Infinite while loop to treat stack of image as video
while True:
# Reading frame(image) from video
check, frame = video.read()
# Converting color image to gray_scale image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Converting gray scale image to GaussianBlur
# so that change can be found easily
grayblur = cv2.GaussianBlur(gray, (21, 21), 0)
# Reset the value of static_background, and reset the iteration count
# periodically so we don't get too big of a number.
if iteration % BACKGROUND_REFRESH_RATE == 0:
iteration = 1
static_background = grayblur
continue
# Difference between static background
# and current frame(which is GaussianBlur)
diff_frame = cv2.absdiff(static_background, grayblur)
# If change in between static background and current frame is greater
# than SENSITIVITY_THRESHOLD it will show white color(255)
thresh_frame = cv2.threshold(diff_frame, SENSITIVITY_THRESHOLD, 255, cv2.THRESH_BINARY)[1]
thresh_frame = cv2.dilate(thresh_frame, None, iterations = 0)
# Finding shapes of moving object
shapes,_ = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for shape in shapes:
# Ignore small motion areas
if cv2.contourArea(shape) < MOTION_SIZE:
print("motion not detected")
continue
print("motion detected")
# mixer.music.play()
# Making green rectangle around the moving object
(x, y, w, h) = cv2.boundingRect(shape)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
iteration += 1
# Displaying debugging images
cv2.imshow("Gray Frame", gray)
cv2.imshow("Grayblur Frame", grayblur)
cv2.imshow("Difference Frame", diff_frame)
cv2.imshow("Threshold Frame", thresh_frame)
cv2.imshow("Color Frame", frame)
cv2.waitKey(1) # Without this, the images don't show
video.release()
# Destroying all the windows
cv2.destroyAllWindows()