|
1 | 1 | import cv2 |
2 | | -import numpy as np |
3 | 2 |
|
4 | 3 | """ |
5 | 4 | Basic Motion Detection using frame differencing and background subtraction (MOG2). |
@@ -32,9 +31,7 @@ def create_background_subtractor() -> cv2.BackgroundSubtractor: |
32 | 31 | True |
33 | 32 | """ |
34 | 33 | # history=500, varThreshold=16 are common defaults; detectShadows adds robustness |
35 | | - return cv2.createBackgroundSubtractorMOG2( |
36 | | - history=500, varThreshold=16, detectShadows=True |
37 | | - ) |
| 34 | + return cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True) |
38 | 35 |
|
39 | 36 |
|
40 | 37 | def preprocess_frame(frame: cv2.Mat) -> cv2.Mat: |
@@ -75,9 +72,7 @@ def frame_difference(prev_gray: cv2.Mat, curr_gray: cv2.Mat) -> cv2.Mat: |
75 | 72 | return closed |
76 | 73 |
|
77 | 74 |
|
78 | | -def background_subtraction_mask( |
79 | | - subtractor: cv2.BackgroundSubtractor, frame: cv2.Mat |
80 | | -) -> cv2.Mat: |
| 75 | +def background_subtraction_mask(subtractor: cv2.BackgroundSubtractor, frame: cv2.Mat) -> cv2.Mat: |
81 | 76 | """ |
82 | 77 | Apply background subtraction to obtain a motion mask. Includes morphology. |
83 | 78 |
|
@@ -111,9 +106,7 @@ def annotate_motion(frame: cv2.Mat, motion_mask: cv2.Mat) -> cv2.Mat: |
111 | 106 | >>> np.any(annotated[..., 1] == 255) # green channel from rectangle |
112 | 107 | True |
113 | 108 | """ |
114 | | - contours, _ = cv2.findContours( |
115 | | - motion_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
116 | | - ) |
| 109 | + contours, _ = cv2.findContours(motion_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
117 | 110 | annotated = frame.copy() |
118 | 111 | for contour in contours: |
119 | 112 | if cv2.contourArea(contour) < MIN_CONTOUR_AREA: |
@@ -188,3 +181,5 @@ def main() -> None: |
188 | 181 | if __name__ == "__main__": |
189 | 182 | main() |
190 | 183 | print("DONE ✅") |
| 184 | + |
| 185 | + |
0 commit comments