-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22_basic_motion_detection.py
More file actions
37 lines (30 loc) · 1.06 KB
/
22_basic_motion_detection.py
File metadata and controls
37 lines (30 loc) · 1.06 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
import cv2 as cv
import numpy as np
cap = cv.VideoCapture('images/vtest.avi')
ret, frame1 = cap.read()
ret, frame2 = cap.read()
while cap.isOpened():
diff = cv.absdiff(frame1, frame2)
gray = cv.cvtColor(diff, cv.COLOR_BGR2GRAY)
blur = cv.blur(gray, (5, 5), 0)
_, thresh = cv.threshold(blur, 20, 255, 0)
dilated = cv.dilate(thresh, None, iterations=3)
contours, _ = cv.findContours(dilated, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv.boundingRect(contour)
if cv.contourArea(contour) < 1000:
continue
cv.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv.putText(
frame1, 'Status: {}'.format('Movement'),
(10, 20), cv.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 1
)
# cv.drawContours(frame1, contours, -1, (0, 255, 255), 2)
cv.imshow('Inter', frame1)
frame1 = frame2
ret, frame2 = cap.read()
if cv.waitKey(40) == 27:
break
cv.destroyAllWindows()
cap.release()