We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4df87fc commit ec3b8bdCopy full SHA for ec3b8bd
computer_vision/motion_detection.py
@@ -1,5 +1,6 @@
1
import cv2
2
3
+
4
def detect_motion(video_source=0):
5
cap = cv2.VideoCapture(video_source)
6
ret, frame1 = cap.read()
@@ -8,7 +9,7 @@ def detect_motion(video_source=0):
8
9
while cap.isOpened():
10
diff = cv2.absdiff(frame1, frame2)
11
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
- blur = cv2.GaussianBlur(gray, (5,5), 0)
12
+ blur = cv2.GaussianBlur(gray, (5, 5), 0)
13
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
14
dilated = cv2.dilate(thresh, None, iterations=3)
15
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
@@ -17,7 +18,7 @@ def detect_motion(video_source=0):
17
18
if cv2.contourArea(contour) < 1000:
19
continue
20
(x, y, w, h) = cv2.boundingRect(contour)
- cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)
21
+ cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
22
23
cv2.imshow("Motion Detection", frame1)
24
frame1 = frame2
0 commit comments