project
Motion detection built from scratch, twice
A NumPy baseline and then a full OpenCV pipeline, so I'd know what every stage actually does.
The honest reason I started this was frustration with using libraries I didn’t fully understand. So I built motion detection twice.
The first version is only NumPy: grayscale conversion, absolute frame difference, thresholding, pixel counting. It works, and building it teaches you exactly what motion detection is at the pixel level.
The second adds the production techniques — Gaussian blur before differencing, morphological dilation to merge fragmented regions into coherent blobs, and contour detection with area filtering to separate a person crossing the frame from leaves moving outside a window. Both run in real time on PiCamera2’s dual-stream API: 640×480 for detection, 1080p held available for captures. That split turned out to be the single most important performance decision in the project.
What I took away
Gaussian blur is not optional. Sensor noise alone makes 10–20% of pixels vary randomly between frames, and no threshold value compensates for that without blur first.
Resolution is the biggest performance lever available — 1080p to 480p bought a 10× speedup with no meaningful loss in detection quality.
Contour area is a surprisingly good classifier. Leaves produce contours under 300 px²,
people produce 2000+, so one MIN_AREA parameter separates them cleanly. Thresholds
themselves are environment-specific, though: values tuned indoors (20–25) failed outdoors
and needed 30–40.