What to do inside the code:
- Start the camera so it can take video.
- Wait a little bit so the camera gets ready.
- Write down my email address and app password to send emails.
- Write down the email address where I want to send alerts.
- Set up the email sending system with a safe connection.
- Take the very first picture from the camera.
- Change this picture to black and white (gray scale).
- Blur the picture a little to ignore small light changes or noise.
- Save this blurred gray picture as the background to compare with later pictures.
- Start a loop that keeps taking new pictures from the camera again and again.
- Make the new picture smaller so it’s faster to check.
- Change the new picture to black and white.
- Blur this new picture the same way to reduce small changes.
- Compare the new blurred gray picture with the first saved background picture.
- See where the pixels are different — that means there might be motion.
- Turn the difference picture into black and white — white means motion, black means no motion.
- Make the white motion parts thicker so it’s easier to find.
- Find the outlines (contours) of the white motion parts.
- For each motion part found, check how big it is.
- If it’s too small (like small flickers), ignore it.
- If it’s big enough, say “motion detected.”
- Draw a green rectangle around the motion part on the picture to show where the motion is.
- Get the current date and time to make a unique file name.
- Save the current picture with the green rectangle as an image file.
- Create an email message with a subject saying motion is detected.
- Attach the saved image to the email.
- Send the email to the person I want to alert.
- Wait for 1 second so I don’t send too many emails too fast.
- Keep repeating the loop to check for more motion.
- Optionally, update the background picture sometimes or after sending an alert to adjust to changes.
- When I want to stop, close the camera.
- Clean up all open windows and resources.