From 4df87fc8aff08b4fcc42e8bfd40d0be97c49cff8 Mon Sep 17 00:00:00 2001 From: Aryan Dhanuka Date: Thu, 9 Oct 2025 11:15:18 +0530 Subject: [PATCH 1/2] [FEAT] Add motion detection script using OpenCV --- computer_vision/motion_detection.py | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 computer_vision/motion_detection.py diff --git a/computer_vision/motion_detection.py b/computer_vision/motion_detection.py new file mode 100644 index 000000000000..8b75e53d37a6 --- /dev/null +++ b/computer_vision/motion_detection.py @@ -0,0 +1,30 @@ +import cv2 + +def detect_motion(video_source=0): + cap = cv2.VideoCapture(video_source) + ret, frame1 = cap.read() + ret, frame2 = cap.read() + + while cap.isOpened(): + diff = cv2.absdiff(frame1, frame2) + gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5,5), 0) + _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) + dilated = cv2.dilate(thresh, None, iterations=3) + contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + + for contour in contours: + if cv2.contourArea(contour) < 1000: + continue + (x, y, w, h) = cv2.boundingRect(contour) + cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2) + + cv2.imshow("Motion Detection", frame1) + frame1 = frame2 + ret, frame2 = cap.read() + + if not ret or cv2.waitKey(40) == 27: + break + + cap.release() + cv2.destroyAllWindows() From ec3b8bddbd2ada87a159e5cea93ffd2c1b2b6973 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 05:48:51 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/motion_detection.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/computer_vision/motion_detection.py b/computer_vision/motion_detection.py index 8b75e53d37a6..0af8e0ca02eb 100644 --- a/computer_vision/motion_detection.py +++ b/computer_vision/motion_detection.py @@ -1,5 +1,6 @@ import cv2 + def detect_motion(video_source=0): cap = cv2.VideoCapture(video_source) ret, frame1 = cap.read() @@ -8,7 +9,7 @@ def detect_motion(video_source=0): while cap.isOpened(): diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) - blur = cv2.GaussianBlur(gray, (5,5), 0) + blur = cv2.GaussianBlur(gray, (5, 5), 0) _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) dilated = cv2.dilate(thresh, None, iterations=3) contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) @@ -17,7 +18,7 @@ def detect_motion(video_source=0): if cv2.contourArea(contour) < 1000: continue (x, y, w, h) = cv2.boundingRect(contour) - cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2) + cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow("Motion Detection", frame1) frame1 = frame2