Object Tracking With Opencv and Python With Just 5 Steps (1 Bonus)

by Visionary Robotics in Circuits > Computers

3621 Views, 0 Favorites, 0 Comments

Object Tracking With Opencv and Python With Just 5 Steps (1 Bonus)

WhatsApp Video 2021-10-30 at 23.36.36.gif

Hey everyone ! Today we will learn how to track objects with OPENCV and PYTHON

Above is the Demonstration of the heatsink being tracked with a good accuracy and average speed of 91 FPS

So let's Start.......

Supplies

Python-https://www.python.org

Pycharm- https://www.jetbrains.com/pycharm

Webcam (optional, only For Live Video Testing)

Here's the video That I used

Downloads

Creating Environment

environment.png
  1. Create a Pycharm project with Python 3.9
  2. Install Libraries-------- Opencv-python,Opencv-contrib-python,Cvzone

Now our environment has been configured . Let's CODE

Writing Base of Code

First we will Import the libraries

import cv2
import cvzone
import statistics

Then we will create the video capture for the tracker

cap=cv2.VideoCapture("video.mp4")#for Live webcam write cap=cv2.VideoCapture(0)

After that we will create a While loop for extracting our frames from the video/webcam and then display them

while True:
  sucess,img=cap.read()
  cv2.imshow("output",img)
  k=cv2.waitKey(1)
  if k==27:#press "ESC" key to exit
      break


Initializing Tracker

First we will take a frame from video/webcam to tell our tracker where out ROI (region of interest) is

success, img = cap.read()

Then we will create our tracker

tracker=cv2.legacy.TrackerMOSSE_create()

After that we will select our ROI with a cv2 function

bbox=cv2.selectROI("output",img,False)

Now initialize the tracker

tracker.init(img ,bbox)

Updating the Tracker and Drawing Bounding Box

Now we have to update our tracker

success,bbox=tracker.update(img)

After that if the object is being detected by tracker it has to be highlighted by a rectangle and if the object is not found then we need to display a "lost" message on window

if sucess:
    draw(img,bbox)#we will write this UDF later
else:
    cv2.putText(img,"lost",(75,55),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,255,0),2)

Note: All the above code was in the While loop

Now we will create a function to draw the bounding box

def draw(img,bbox):
    x,y,w,h=int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3])
    cv2.rectangle(img,(x,y),((x+w),(y+h)),(0,255,255),3,1)

after creating the UDF (User Defined Function) our tracker has been completed

You can run this code to try your program

Getting FPS and Average FPS

You might remember the Cvzone and Statistics module that we have imported earlier

It was for getting the FPS and average FPS of our tracker respectively.

So let's use it

1.Creating list for storing FPS and calling FPS class of Cvzone module

f=[]
fps=cvzone.FPS()

2.Displaying FPS and storing FPS values

fps, img = fpsr.update(img,pos=(75,90),color=(255,255,0),scale=2,thickness=2)
f.append(int(fps))#these two lines are in while loop

3 Getting average FPS

For This change this code from step 2

cv2.imshow("output",img)
k=cv2.waitKey(1)
if k==27:
    break

to this

cv2.imshow("output",img)
k=cv2.waitKey(1)
if k==27:
    print(int(statistics.mean(f)))
    break



HURRAY we have created an object tracking program

Analysis of All Trackers

CSRT - accurate | average FPS-7

MIL - accurate | average FPS-3

KCF - very less accurate | average FPS-44

MOSSE - accurate(depends on video conditions) | average FPS-91 best ONE(For my case)

Boosting - less accurate | average FPS-14

Median Flow - less accurate | average FPS-62

TLD - very less accurate | average FPS-5

Full Code

Here is the Full Code

import cv2
import cvzone
import statistics
cap=cv2.VideoCapture("video.mp4")
f=[]
fpsr=cvzone.FPS()
success, img = cap.read()
tracker=cv2.legacy.TrackerMOSSE_create()
bbox=cv2.selectROI("output",img,False)
tracker.init(img ,bbox)
def draw(img,bbox):
    x,y,w,h=int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3])
    cv2.rectangle(img,(x,y),((x+w),(y+h)),(0,255,255),3,1)
while True:
    success,img=cap.read()
    fps, img = fpsr.update(img,pos=(75,90),color=(255,255,0),scale=2,thickness=2)
    f.append(int(fps))
    sucess,bbox=tracker.update(img)
    if sucess:
        draw(img,bbox)
    else:
        cv2.putText(img,"lost",(75,55),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,255,0),2)
    cv2.imshow("output",img)
    k=cv2.waitKey(1)
    if k==27:
        print(int(statistics.mean(f)))
        break