Python script(s) to test OpenCV capabilities for motion detection
-
OpenCV 4.6+ from here Releases - OpenCV - choose your platform. This script was written on Windows. I can't remember what the name of the python wrapper libraries was. If you can't remember or never knew, here is the pip command line and the name:
pip install OpenCV-Python
-
2 other important libraries are numpy and imutils these can be obtained with pip using:
pip install numpy pip install imutils
-
I use the library mjpeg_streamer to have a simple web server to stream the output:
pip install mjpeg_streamer
-
Don't forget to update your libraries from time to time by adding the --upgrade attribute to them
pip install what_I_already_installed_to --upgrade
-
Windows system
pip install pywin32 pip install pygetwindow
-
MacOS system
pip install pyobjc-framework-Quartz
- Linux system
pip install python-xlib
This is really a lot of code for a small amount of features.
-
you only need to change this variable to 1, 2 or 3 in main script:
analyse_mode = 1 # 1 = contour, 2 = eraseBackground, 3 = mask_motion
-
To use the maximum video size of your camera you have to tell OpenCV which backend to use. The VideoCaptureApi can be set on windows to one of these:
cv2.CaptureVideo(0, cv2.CAP_DSHOW)
-
and this is how you set the width and height of the video
cap.set(cv2.CAP_PROP_FRAME_WIDTH , cam_width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cam_height)
-
A captured image should be converted to a gray image as it is more effective for calculations than leaving it in colour.
grayImg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-
Blurring images allows for better contour detection (is that so?)
grayImg = cv2.GaussianBlur(grayImg, (21, 21), 0)
-
If I increase the exposure of my webcam, the frame rate I get from the camera decreases noticeably.
-
The image noise of the camera is often recognised as movement. Which is the most effective way to reduce noise? Is it ultimately more effective to take better hardware?
-
Motion detection becomes unreliable when the camera is in automatic mode for brightness, contrast, saturation or exposure. **Solution **found on Windows: by using
cv2.CAP_DSHOW
and set the camera propertycap.set(cv2.CAP_PROP_SETTINGS, 0)
, windows will open a dialog window.
- when using the erase Background feature of OpenCV, a 'ghost image' is always calculated, Even if nothing has changed in the scene.