-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotion_detector.py
More file actions
60 lines (44 loc) · 1.67 KB
/
Motion_detector.py
File metadata and controls
60 lines (44 loc) · 1.67 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
import cv2,time
video = cv2.VideoCapture(0)
first_frame = None
while True:
check,frame = video.read()
#convert the frames into gray else is will not supported by
# findCoutours etc.
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame = gray
continue
# Calculate the difference between the first frame and next
# other frames
delta_frame = cv2.absdiff(first_frame,gray)
# Provide the threshold value such that less than 30 value will be
# black and greater than 30 pixels will be white.
thresh_delta = cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1]
thresh_delta = cv2.dilate(thresh_delta,None,iterations=0)
# the counter area to add the borders.
(_,cnts,_) = cv2.findContours(thresh_delta.copy(),
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# To remove shadows , basically it will keep that part which
# is white
for coutour in cnts:
if cv2.contourArea(coutour) < 999:
continue
# Create a rectangular box
(x,y,w,h) = cv2.boundingRect(coutour)
cv2.rectangle(frame, (x,y),(x+w,y+h),(255,5,10),3)
# It will show normal video with box around the object
cv2.imshow("Frame",frame)
#Tr will show gray video
cv2.imshow("Capturing",gray)
#Gaussian blur image
cv2.imshow("Delta",delta_frame)
# Avobe 30 value object it will show while less blackq
cv2.imshow("Thresh",thresh_delta)
key = cv2.waitKey(1)
# when user press 'q' then all windows will closed.
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()