MOTION DETECTION AND TRACKING USING OPENCV AND PYTHON

In this post, we are going to see how to detect and track movements(simply motion detection and tracking) using the OpenCV module.

At first, you need to install the OpenCV-python module, to install the module just open your command prompt and type,
  • pip install OpenCV-python
  • Then hit enter, it will install the module automatically.

After installing the module, just import the module and write the basic code to read the video.
# import the opencv module
import cv2
# capturing video
capture = cv2.VideoCapture("videos/input.mp4")

After that, we have to get the two frames from the video or webcam and find the difference between the two frames, which is nothing but if there is a movement that occurs between the frames there might be a difference. 

So, here we are trying to get the difference using the cv2.absdiff() function in the OpenCV module and this method takes two parameters which are the two frames.
# find difference between two frames
diff = cv2.absdiff(img_1, img_2)

Then, convert the diff image to grayscale this will make more sense than the RGB image and smoothen the image to get the clear binary image using cv2.GaussianBlur() function in the OpenCV module.
# to convert the frame to grayscale
diff_gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

# apply some blur to smoothen the frame
diff_blur = cv2.GaussianBlur(diff_gray, (5, 5), 0)

After smoothening the image, convert the image to a binary image to find contours using the cv2.threshold() function in the OpenCV module.
# to get the binary image
_, thresh_bin = cv2.threshold(diff_blur, 20, 255, cv2.THRESH_BINARY)

Now, we are ready to detect and track movements by simply following the step given below,
  • find contours using the cv2.findContours() function in the OpenCV module.
  • using the details of the contours, you can outline the contours in different colors by cv2.drawContours() or draw a rectangle around the contours by getting the x, y position, width, and height using cv2.boundingRect() which takes contours as a parameter.

In this case, we are going to draw a rectangle around the contours.
# to find contours
contours, hierarchy = cv2.findContours(thresh_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# to draw the bounding box when the motion is detected
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if cv2.contourArea(contour) > 300:
cv2.rectangle(img_1, (x, y), (x+w, y+h), (0, 255, 0), 2)
# cv2.drawContours(img_1, contours, -1, (0, 255, 0), 2)

And finally, display the output.

Entire Program:

# import the opencv module
import cv2

# capturing video
capture = cv2.VideoCapture("videos/input.mp4")

while capture.isOpened():
# to read frame by frame
_, img_1 = capture.read()
_, img_2 = capture.read()

# find difference between two frames
diff = cv2.absdiff(img_1, img_2)

# to convert the frame to grayscale
diff_gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

# apply some blur to smoothen the frame
diff_blur = cv2.GaussianBlur(diff_gray, (5, 5), 0)

# to get the binary image
_, thresh_bin = cv2.threshold(diff_blur, 20, 255, cv2.THRESH_BINARY)

# to find contours
contours, hierarchy = cv2.findContours(thresh_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# to draw the bounding box when the motion is detected
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if cv2.contourArea(contour) > 300:
cv2.rectangle(img_1, (x, y), (x+w, y+h), (0, 255, 0), 2)
# cv2.drawContours(img_1, contours, -1, (0, 255, 0), 2)

# display the output
cv2.imshow("Detecting Motion...", img_1)
if cv2.waitKey(100) == 13:
exit()

Input:

Output:

 

Comments

Popular posts from this blog

BASIC HAND TRACKING USING PYTHON

FACE DETECTION USING MEDIAPIPE AND PYTHON