Coders Packet

Detecting motion and plotting the movement in graph using OpenCV in Python

By Akshay Sai Reddy Kondu

  • MotionDetectionOpenCV/
  • A project that helps to detect any movement in front of a webcam and also plot a graph that shows how much time the object was in front of the camera, using OpenCV Python library.

    The project contains 2 '.py' files:

    1. motiondetectoropencv.py: In this, we make use of the OpenCV library to detect the motion. For detecting motion, we can only use this file.

    2. graph.py: Here, we use the Bokeh library for plotting an interactive graph that contains the data about the time intervals that an object was present in front of the camera. Also, this uses the 'motiondetectoropencv.py' file, so both files should be in the same directory. If we want to plot the graph for motion intervals, this file should be used.

    Code Explanation:

    1. Importing Libraries: In this project, we make use of OpenCV library, Bokeh library, pandas library.

    pip install opencv-python
    pip install pandas
    pip install bokeh

    After installing the above libraries, we can just import them into our code, by writing

    import cv2
    import pandas
    from bokeh.plotting import figure, show, output_file, Figure
    from bokeh.models import HoverTool, ColumnDataSource

    2. Capturing the first frame and the current frame: Here, cam.read() stores the current frame in form of a 3-D array in the 'frame' variable. Next, we convert this frame into a grayscale image i.e., we converted the 3-D array to a 2-D array. Then we converted the greyscale image into a gaussian blur image.

    3. Storing the difference in frames and dilating the frame: Here, 'delta_frame' stores the difference of intensities of first frame to the current frame. A threshold is defined such that if the intensity difference for a particular pixel is more than 30 then that pixel will be white and if the difference is less than 30 that pixel will be black. The frame is blurred using GaussianBlur to cause saturation and dilated to make the process of finding contour area easy. All this acts as saturation on the frame to get more accurate motion results.

    4. Motion Detection: Using 'findContours' we can store the coordinates that are different in two frames in an array, here it is 'cnts'. Now, since we do not want any small motion like moving lips, or talking, etc., to not be detected we will define another threshold, i.e., we will consider only those contours whose area exceeds 20000 or else, we will just ignore it. 

    5. Noting the time intervals: By using a list, 'status_list' we keep track of time when the object is detected and when it's not. We make the 'status' variable as 1 if we detect motion. Now, we keep appending this status to the list, for every frame. We can mark the time as 'start' if we have 0,1 as the last two elements in the status_list variable. And we mark the time as 'end' if we have 1,0 as the last two elements in the 'status_list'. After, closing the camera, we make a data frame that has the start and end times of the interval in which we find an object.

    6. Plotting graph: Time Intervals will be plotted using Bokeh Plot. Bokeh is an interactive visualization library that targets modern web browsers for presentation. Here, the time intervals are collected by the CSV file and then plotted using Bokeh. The red color shows that an object was under motion, time is displayed in milliseconds(ms).

     

    This can be deployed in CCTV cameras for detecting suspicious activity and also can be used by motion-sensing devices or IoT devices.

    Download Complete Code

    Comments

    Comments for the packet "Detecting motion and plotting the movement in graph using OpenCV in Python";
    No comments yet