How to detect moving objects on CCTV footage using Python
Pic credit: neurohive.io

How to detect moving objects on CCTV footage using Python


No alt text provided for this image

In this Article, we will see how to detect a moving object using a simple python script which uses only OpenCV library

import cv2

The above snippet imports the OpenCV library. If you don't have the opencv library then you can install it by typing "pip install opencv-python" at command prompt

(If you don't know what a library is, check my previous article "What is a library in python")

cap = cv2.VideoCapture('carshot.mp4')

The above line creates a VideoCapture object. The VideoCapture takes argument as 0 (for your own webcam), 1 (for external webcam) or the file path in which you have stored your video. You can find the video which I have used, at https://www.youtube.com/watch?v=_JL_m48SFXI


ret, frame1 = cap.read()
ret, frame2 = cap.read()

The ret variable is boolean and hence gets True or False value. If the frame is read correctly then it gets True value else it gets False value. Here we are assigning frame1 and frame2 same values, it will make sense as we proceed.

while cap.isOpened(): #this returns true as long as the frame is being read
  difference = cv2.absdiff(frame1,frame2)
  gray_image = cv2.cvtColor(difference,cv2.COLOR_BGR2GRAY)
  blur = cv2.GaussianBlur(gray_image,(25,25),0)
  ret,thresh = cv2.threshold(blur,18,255,cv2.THRESH_BINARY)
  dilated = cv2.dilate(thresh,None,iterations=3)
  contours =cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  for contour in contours:
    (x,y,w,h) = cv2.boundingRect(contour)
    if cv2.contourArea(contour)>200:
      cv2.rectangle(frame1,(x,y),(x+w,y+h),(0,255,0),2)
      continue
  cv2.imshow("Detect",frame1)
  frame1=frame2
  ret,frame2 = cap.read()

  if cv2.waitKey(30) == 27:
    break
  

 In the above snippet the difference variable gets arrays in which all value is zero, since our frame1 and frame2 contains image matrix with same values. Thus the difference variable gets the value 0 on the first execution.

The gray_image variable holds the frame in black and white picture. We are converting the frame into gray frame for easy computation (colored images has three channels, blue, green and red) .Also it is easy to find contours in gray scale mode than the colored mode (remember contour is a line connecting points of equal color or intensity)

The gaussian blur is applied in order to smoothen the image by taking out noise if any. Thresholding is a technique to segment for separating a object from its background. Selecting the thresh parameter (18 in this case) will come with practice

The cv2.dilate(..) helps in finding the contours better. We pass in the source image as thresh, kernel as None and iterations as 3 as of now, iterations can be increased based on the output.

cv2.findContours(..) helps in finding the contours, Once we are able to find contours, we will display rectangles around it using the command cv2.boundingRect(contours), but since we don't want to display contours for all tiny movements within the video, we use a if statement to filter out the small movements and draw rectangles around it. In this script the contour area we have taken is 200, you can adjust it based on your requirements

cv2.rectangle(...) draws rectangles on frame1 with top left co-ordinate at (x,y) and bottom right co-ordinate at (x+w,y+h) in green colour (since we have specified as (0,255,0)) with line thickness of 2

The cv2.imshow(..) displays the object detection in a window named 'detect'. frame1=frame2 assigns frame2 value to frame1 (which is the same on first execution) ret,frame2 = cap.read() gives new value into frame2 and hence when the loop executes for the second time the values at frame1 and frame2 will be different

The value within cv2.waitkey() ('30' in this case) determines how fast/slow a video will be played

cap.release()
cv2.destroyAllWindows()

The above two lines of code releases the cap object and closes the windows opened.

Disadvantage of above code:

The above code can be done for some static video recording like a CCTV footage. When the video is moving while capturing, then the above code doesn't work fine, since the logic behind this code is identifying the frame difference and plotting rectangles around it

Bonus Section:

The above code can be extended such that, we can receive a notification in our mobile if anything moves withing our CCTV footage. I have posted the extended version of code at "How to detect moving object on a CCTV footage". Find other similar articles at Transport Pythonified

Conclusion:

Before learning and using CNN algorithms for image classification, it is good to know about OpenCV module and it's important methods which can be used in the image classification script. Hence, do have a firm grip on the OpenCV methods used in the above article before jumping into higher level programming

Animeshkumar Nayak

AI Specialist-Advisory and Consulting to Govt in Tech | Digital Platforms, Cloud Data and AI | Computer Vision | Image Processing | Machine Learning (All Views are my personal)

2 年

I would like to know how to deal with object detection on multiple CCTV camera.

回复
Adabala Narasimha Murthy

Traffic & Transportation Engineering Studies

4 年

Helpful! This will developing for Videographic Traffic analysis..

Vignesh Kathirkamar

MLOPs Engineer at Eli Lilly and Company

4 年

You are right Vishal Devalalikar, I too have seen the performance of YOLO model in image classification. But the one which I have posted just uses the changes in frame and detects movement of objects, which is very basic in nature and doesn't uses any algorithms. I haven't explored YOLO yet though. Will try to post a article on YOLO soon in future.

Vishal Devalalikar

Traffic Modeller/ Engineer

4 年

There is one more model called YOLO model ...which has shown better results than R CNN model for object detection . especially YOLO v5 version is fastest for detection within 1 or 2 sec. with? less cost function than? R-CNN model?

Vignesh Kathirkamar

MLOPs Engineer at Eli Lilly and Company

4 年

Find the extended version of getting alert on mail when an object is detected, at https://vigneshkathirkamar.blogspot.com/2020/08/how-to-detect-moving-objects-using.html

要查看或添加评论,请登录

社区洞察

其他会员也浏览了