Artificial Intelligence

Streaming Live Motion Detection Using OpenCV

Although there are other computer vision languages available, none of them can match OpenCV’s open-source nature and lack of usage restrictions compared to certain languages like Matlab, which demand a licence to use. Large-scale Matlab use requires the acquisition of a licence, which reduces its cost-effectiveness.

In terms of performance, it also works the best because OpenCV runs considerably faster—it can get up to 80 mph in certain situations. Thus, Open CV makes it easy for everyone interested in studying computer vision or image processing, while Matlab is better suitable for those with the financial means to purchase a licence.

Motion Detection

The main idea is to use OpenCV to detect motion in a frame, or whether there will be any changes to the frame. It can be used for webcams or any other mounted camera, for recorded video or for live streaming.

A bounding box will be generated in response to the motion observed, and if a new object enters the frame, a bounding box will surround it.

Videos are created by stacking numerous frames together. The difference between two continuous frames is calculated for motion detection, and if it is greater than the predetermined threshold, motion detection has been detected there.

Coding the Motion Detector

  • Bring the Libraries in

Bring in the necessary libraries, such as NumPy and cv2 (OpenCV). Given that the video consists of still images, OpenCV will be used for image preprocessing, such as activating the webcam, reading the video as a stack of frames, handling the frame’s colour formatting, determining the image’s gaussian blur, computing the difference between frames, thresholding, dilation, contour detection, and bounding boxes over motion detected.

  • Selection of Static Frame

Every frame will be compared to the initial frame, which will be referred to as a static frame, in order to perform motion detection. As a result, each frame will be compared to a static frame each time, and motion in the frames will be identified based on the difference.

When the webcam opens, the static frame will start out as None and initialise with the first frame.

  • Make an Object for Video Capture

The first step in using OpenCV to record a live webcam feed is to construct a VideoCapture object and pass 0 as an argument to indicate which webcam to utilise. Select a certain dimension for the window’s size.

Next, we’ll use a while loop to record each frame from the webcam and use a waitkey to keep running until the camera closes.

  • After Reading the Frames, Convert them to Grayscale

Now, read the frames using the previously initialised object and convert them to grayscale.

  • Blurring of Images

The following stage is to use CV2’s GaussianBlur function to virtually eliminate high frequencies from each frame of noise. Pass the standard deviation in both directions as an argument, along with the low pass filter kernel’s height and width.

Additionally, supply the function with the grayscale image as input. It will be easy to identify motion in the following frames once the image has been blurred.

  • Revise the Static Frame

Since the first level hasn’t changed, it’s time to update the static frame with the first frame from the live streaming. The absolute difference between each frame will be computed using this frame as a guide.

  • Determine Motion by Calculating the Frame Difference

Determine the absolute difference between each frame from the live streaming and the static frame. Since there is a difference of more than thirty, threshold the grayscale to make any area where motion is detected white. Then, enlarge the thresholded image.

  • Identify Contours

Now that thresholding has turned the motion zone white, which may be thought of as contours, locate contours to obtain the coordinates of the moving objects.

In addition to motion detection, we will also plot a rectangle (bounding box) on the moving objects and label it with the text “Motion Detected” after we get their coordinates.

  • In a Live Feed, Display Frames

Since motion has now been recognised in the video, live streaming of the motion detection findings is the next step.

Enter the code to destroy every window and release the camera.

Among the many features and uses of OpenCV, a computer vision library, are motion detection, facial identification, object detection, tracking motions of people and things, camera movements, and many more. It is also faster than the other tools and is widely available due to its open-source nature.

Due of OpenCV’s real-time applicability and ability to be combined with other languages and hardware devices, such as the Raspberry Pi, many firms use it.

After image smoothing, each new frame is kept deducted from the first frame, and if any region has a value less than a threshold, it indicates motion detection in the frames. Plot the bounding boxes and text across the region where motion has been detected using contours later on.

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker