Motion Detection in Python Using OpenCV on BrainyPI

by Pushkar_Shepal in Circuits > Raspberry Pi

483 Views, 1 Favorites, 0 Comments

Motion Detection in Python Using OpenCV on BrainyPI

Motion_Output.png

Motion detection is a technique used to identify and track changes in the position of objects within a video stream. It has a wide range of applications across various domains. In surveillance systems, motion detection plays a crucial role in identifying and monitoring any movement within a specific area. It can be used to trigger alarms or notifications when unauthorized activity is detected. Motion detection is also utilized in video analytics, where it enables the extraction of valuable information from video data, such as object tracking, behavior analysis, and crowd monitoring.

In this tutorial, we will focus on implementing motion detection using Python and the OpenCV (Open Source Computer Vision) library. OpenCV is a popular computer vision library that provides a rich set of tools and functions for image and video processing tasks. It offers robust features for capturing, manipulating, and analyzing video streams and image data. With its extensive set of algorithms and functions, OpenCV simplifies the implementation of motion detection techniques.

By the end of this tutorial, you will have a solid understanding of how to leverage OpenCV's capabilities to detect motion in a video stream or a recorded video file. You will learn how to process frames, identify edges, calculate the mean squared error for motion detection, and visualize the results with the frames per second (FPS) counter.

So, let's dive into the step-by-step guide and get started with motion detection using Python and OpenCV!

Supplies

BrainyPI.png
BrainyPI1.png

To follow along with this tutorial, you'll need:

  • BrainyPI
  • OpenCV library installed on BrainyPI. To Install it use the following command
pip3 install opencv-python


Setting Up the Environment

Open a text editor or an integrated development environment (IDE) and create a new Python file. Copy the code into the file and save it with a .py extension. You can name the file as per your preference. Make sure to specify the correct video file path or camera device ID in the "video_path" and "CAMERA_DEVICE_ID" variables, respectively. Adjust the "IMAGE_WIDTH" and "IMAGE_HEIGHT" variables to set the desired image resolution.

Code Overview

Create a video capture object to access the video file or camera device.

Set the image resolution using the set method of the video capture object.

Enter the main loop to process video frames.

  • Read frames from the video capture object using the read method.
  • Apply motion blur (if enabled) to the raw frame using Gaussian blur.
  • Convert the frame to grayscale using cv2.cvtColor.
  • Perform edge detection on the grayscale frame using Canny edge detection.
  • Display the edge-detected frame and the grayscale frame using cv2.imshow.
  • Calculate the mean squared error (MSE) between the current and previous grayscale frames to detect motion.
  • If motion is detected (MSE exceeds a threshold), print a message indicating the frame number and motion detection.
  • Measure the elapsed time for frame processing using time.time.
  • Calculate the frames per second (FPS) by taking the reciprocal of the elapsed time.
  • Print the FPS value for each frame.
  • Check if the 'Esc' key is pressed using cv2.waitKey.
  • If the key is pressed, exit the loop and proceed to the cleanup section.

Close all windows created by cv2.imshow using cv2.destroyAllWindows.

Release the video capture object using cap.release for cleanup.

Implementation on BrainyPI

Motion_Detection_Output.png

To implement the motion detection code on BrainyPI using GitLab, you can follow these steps:

a. Ensure that you have Git installed on your local machine. You can download Git from the official website and follow the installation instructions.

b. Create a Git repository on a GitLab server to host your motion detection code. You can create a new repository or use an existing one.

c. Clone the Git repository to your local machine using the following command:

git clone git@gitlab.com:your-username/your-repository.git

Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.

d. Copy the motion detection Python file to the local Git repository directory.

e. Commit the changes and push them to the remote GitLab repository using the following commands:

git add motion_detection.py
git commit -m "Added motion detection code"
git push origin main

Replace main with the branch name of your GitLab repository if it's different.

f. Open a terminal or command prompt on your device. Use the following command to establish an ssh connection with your BrainyPI device.

ssh -X pi@auth.iotiot.in -p 65530

g. Now, on your BrainyPI device, make sure you have Git installed. If not, you can install it using the package manager of your operating system.

h. Open a terminal or command prompt on your BrainyPI device.

i. Clone the Git repository from your GitLab server to your BrainyPI device using the following command:

git clone git@gitlab.com:your-username/your-repository.git

Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.

j. Navigate to the cloned repository directory on your BrainyPI device.

k. Run the motion detection code using the following command:

python3 motion_detection.py

The motion detection program will start running, and you will see the output on the terminal of your BrainyPI device.

Conclusion

In this tutorial, we have learned how to implement motion detection in Python using the OpenCV library. By following the step-by-step guide and understanding the code, you can apply motion detection techniques in your own projects. We can experiment with different parameters and explore additional functionalities provided by OpenCV to enhance the motion detection capabilities.