Add basic motion detection script#13363
Add basic motion detection script#13363shivamkb17 wants to merge 10 commits intoTheAlgorithms:masterfrom
Conversation
Introduces motion_detection.py with frame differencing and background subtraction (MOG2) for motion detection using OpenCV. The script provides real-time motion mask and annotated frame display, with adjustable parameters for contour filtering and morphology.
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| DISPLAY_SCALE = 1.0 # resize factor for display | ||
|
|
||
|
|
||
| def create_background_subtractor() -> cv2.BackgroundSubtractor: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function create_background_subtractor
| return cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True) | ||
|
|
||
|
|
||
| def preprocess_frame(frame: cv2.Mat) -> cv2.Mat: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function preprocess_frame
| return blurred | ||
|
|
||
|
|
||
| def frame_difference(prev_gray: cv2.Mat, curr_gray: cv2.Mat) -> cv2.Mat: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function frame_difference
computer_vision/motion_detection.py
Outdated
| return closed | ||
|
|
||
|
|
||
| def background_subtraction_mask(subtractor: cv2.BackgroundSubtractor, frame: cv2.Mat) -> cv2.Mat: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function background_subtraction_mask
| return closed | ||
|
|
||
|
|
||
| def annotate_motion(frame: cv2.Mat, motion_mask: cv2.Mat) -> cv2.Mat: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function annotate_motion
| return annotated | ||
|
|
||
|
|
||
| def main() -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file computer_vision/motion_detection.py, please provide doctest for the function main
for more information, see https://pre-commit.ci
Doctests were added to all major functions in motion_detection.py to provide usage examples and enable easier testing. This improves code reliability and documentation by demonstrating expected input and output for each function.
for more information, see https://pre-commit.ci
Inserted 'import numpy as np' in doctest examples for functions in motion_detection.py to ensure doctests run without import errors.
Introduces motion_detection.py with frame differencing and background subtraction (MOG2) for motion detection using OpenCV. The script provides real-time motion mask and annotated frame display, with adjustable parameters for contour filtering and morphology.
Describe your change:
Checklist: