• Category
  • >Artificial Intelligence

Streaming Live Motion Detection using OpenCV

  • Ripul Agrawal
  • Jul 14, 2020
Streaming Live Motion Detection using OpenCV title banner

“It seems probable that once the machine thinking method had started, it would not take long to outstrip our feeble powers… They would be able to converse with each other to sharpen their wits. At some stage, therefore, we should have to expect the machines to take control.” - Alan Turing




In the last blog, we featured OpenCV along with the functions it provides and the areas of application. OpenCV is a Computer Vision library, written in two languages, i.e., C++ & Python to deal with both images and videos. 


In addition, it provides real-time support using the camera or web cameras in many applications as can be seen in Yolo, Face Recognition, and many more.


Some of the common functions available by OpenCV are,

  • Read, write and display the image,

  • Different color formats for image i.e. BGR, RGB, grayscale, HSV, etc,

  • Data Augmentation i.e. rotation, shrinking, resize, magnification, etc, 

  • Image Thresholding,

  • Edge Detection, and

  • Contour Detection will help in detecting different shapes from the image.


Common Areas of application are,

  • Image Processing,

  • Face Recognition,  

  • Object Detection,

  • Gesture Recognition, 

  • Can create a high-resolution image from different images,

  • Track moving objects,

  • Track 3D models,

  • Track camera movements,

  • Track human motions, and

  • Motion Detection.



Comparison with Other Languages


No doubt there are other languages available for computer vision but none of them compete with OpenCV, as its an open-source and also there is no restriction on its use as you can use it without a license while some language requires a license to use like Matlab. To use Matlab on a large scale you have to purchase a license that makes it less economical. 


It also fits best in terms of execution as OpenCV is much faster as its speed can reach up to 80 in some cases. So in this way, anyone who wants to explore the field of computer vision or image processing can easily use Open CV whereas Matlab is more feasible for the researchers who can afford its license.



Understanding Motion Detection


The core concept is to detect motion in frame i.e. if any change will occur in the frame using OpenCV. It can be done for either recorded video or live streaming i.e web-camera or any other camera mounted. 


In this article, we will detect motion from the web camera using OpenCV and a bounding box will be rendered to the motion detected as if any new object will introduce in the frame then a bounding box will appear surrounding the object.


In videos, multiple frames are stack together to form video. For motion detection, we calculate the difference between two continuous frames and if it’s higher than the set threshold, it means motion detection has been observed there.


Code your own Motion Detector


  • Import the Libraries

Import the required libraries to be used i.e. cv2 (OpenCV), NumPy. As the video is comprised of images, and OpenCV will be used to preprocess images i.e. starting the web camera, to read video as a stack of frames, deal with the color formatting of the frame, find the gaussian blur of the image, calculation of the difference between frames, image thresholding, dilation, contour detection, and bounding boxes over the motion detected.

This image demonstrates the code to import libraries.

Code Snippet: 1

  • Assignment of Static Frame

To perform motion detection, we will compare every frame with the first frame that will be termed as a static frame. So every time each frame will be compared with the static frame and based on the difference motion in the frames will be detected.

Initially, the static frame will be None and will initialize with the first frame once the web camera opened.

This image demonstrates the code to initialize the static frame to None before starting off the camera.

Code snippet: 2

  • Create video capture Object

To capture a live stream from the web camera using OpenCV the first step is to create a VideoCapture object by passing  0 as an argument to signify the camera to use. Set the window size to a particular dimension.

Next, we will run while loop, to capture every frame from the web camera and run until the camera will be closed using waitkey.

This image demonstrates the code to initialize the camera object using VideoCapture for live streaming using web camera.

Code Snippet: 3

  • Read the frames and convert into grayscale

Now read the frames using object initialized above and further convert it into the grayscale format.

This image demonstrates the code to read the frames while live streaming and then conversion of each frame to the grayscale format.

Code Snippet: 4

  • Image Blurring

Now the next step is to remove the noise from each frame basically remove high frequencies from the images using GaussianBlur function by CV2. Pass the height and width of the low pass filter kernel as an argument along with the standard deviation in both directions.

Also, pass the grayscale image as input to the function. After blurring the image, motion in the consecutive frames will be detected easily.

This image demonstrates the code to perform image blurring on the grayscale image to remove noise from that.

Code Snippet: 5

  • Update Static Frame

Now its time to update the static frame with the first frame from the live streaming as no changes in the initial level. This frame will be used as a reference to calculate the absolute difference between each frame.

This image demonstrates the code to update the value of the static frame with the very first frame which was initially set to None.

Code snippet: 6

  • Calculate the difference in the frames and detect motion

Calculate the absolute difference between the static frame and each frame from the live streaming. Hence the difference is greater than 30, apply thresholding to the grayscale so as to where ever the motion will be detected that the region will be white and later dilate that thresholded image.

This image demonstrates the code to calculate the difference between the static frame and each frame and later based on score, thresholding of the image.

Code Snippet: 7

  • Find Contours

Now the region of motion has been converted to the white using thresholding, which can be treated as contours, so use find contours to detect coordinates of the moving objects.

Apart from the motion detection, once we will get the coordinates of the moving objects to plot the rectangle (bounding box) on them and put the text “Motion Detected”.  

This image demonstrates the code to contour detection in the thresholded frames and later plotting of bounding boxes over region of motion detected.

Code Snippet: 8, Source

  • Display frames in live stream

Now the motion has been detected in the video, so the next step is for live streaming with results of motion detection.

Put the code to release the camera and to destroy all the windows.

This image demonstrates the code to display the image and to close the camera by pressing some particular key and destruction of all the windows.

Code Snippet: 9




OpenCV a computer vision library supports many functions and has many applications including facial recognition, object detection, tracking human motions, tracking objects, camera movements, motion detection, etc. In comparison to the other tools, easily accessible to everyone as its open-source and its speed is another factor.


Many companies are using OpenCV due to its real-time application as it can be integrated with different languages and some of the hardware devices i.e. raspberry pi and some others.


Out of its vast applications, this blog has been framed to cover one of that i.e. Motion Detection based on background subtraction from the live streaming, where each new frame will be keep subtracted from the very first frame after image smoothening and if any region has a value less than some threshold, that signifies the motion detection in the frames. Later using contours, plot the bounding boxes on the region where motion has been detected and put the text over there.

Latest Comments

  • Ripul Agrawal

    Dec 09, 2020

    For any queries, please reach out to me at ripulagrawal98@gmail.com

  • himanshuy69486

    Oct 21, 2021

    how to get this code

  • long33291

    Feb 01, 2022

    Waow Awesome information here ,this is unbelievable, it is my first time to be carried away by such a perfect profile article, I will immediately contact you directly and also to take the advantage to share some latest information about how to <a href="https://bexuniversal.com/buy-fake-bank-statement-buy-real-bank-statement/">buy-fake-bank-statement-buy-real-bank-statement</a> ,I will also take the advantage to ask for your permission to join our <a href="http://t.me/bexuniversal/">TELEGRAM GROUP</a>

  • long33291

    Feb 01, 2022

    Wonderful piece you got here , I feel quite happy after I read your Article because I found out that we still have intelligent informative researchers who can still take time out of their busy schedule to put all this together I will instantly grab your rss feed to stay informed of any updates you make and as well take the advantage to share some latest information about.<a href="https://travelquicks.com/services/">panic and axienty treatment</a> also weight lose medications <a href="https://travelquicks.com/2022/01/29/buy-duromine-online-paypal/">buy duromine online paypal</a> <a href="https://travelquicks.com/2022/01/29/buy-cheap-anavar-online/">Buy cheap Anavar Online</a> <a href="https:https://travelquicks.com/sample-page-buy-xanax-3mg-online/">Buy Xanax 3mg online</a> <a href="https://travelquicks.com/projects-buy-diazepam-valium-online/">Buy Diazepam Valium online</a>

  • akunnadozie

    Feb 10, 2022

    I used to be a very poor man who has always not found luck when it comes to playing the lottery. I have been playing the lottery since I was 21 years and now I am 52 meaning I have been playing the lottery for 31 years. The biggest amount I have ever won in my life was 400 bucks. But one day my story turns to history after I find the man named CHIEF UZO who is the best when it comes to winning the lottery. This man is a very strong spiritual wizard who gives out the numbers that can never fail. After all my years of laboring and struggling to win the lottery, I finally won ( $270,000) which was my first big win and I have been winning even bigger ever since then. All my cars, houses, lands, and all my wealth have been money from the lotto. I am one with the majority. Chief Uzo of Igbondu is his name, Email: uzoigbonduspiritualhome@gmail.com this is the only way to win the lottery big and the best way