Object Tracking In Motion

Drive Link:  https://drive.google.com/drive/folders/1USa7N0JCIeGGhZhkKP18-QuziOzh8riX?usp=share_link


Code for Single object Tracking:

import cv2
import imutils

TrDict = {'csrt': cv2.TrackerCSRT_create,
         'kcf' : cv2.TrackerKCF_create,
         'boosting' : cv2.TrackerBoosting_create,
         'mil': cv2.TrackerMIL_create,
         'tld': cv2.TrackerTLD_create,
         'medianflow': cv2.TrackerMedianFlow_create,
         'mosse':cv2.TrackerMOSSE_create}

tracker = TrDict['csrt']()
#tracker = cv2.TrackerCSRT_create()

v = cv2.VideoCapture(r'mot.mp4')


ret, frame = v.read()
#frame = imutils.resize(frame,width=600)
cv2.imshow('Frame',frame)
bb = cv2.selectROI('Frame',frame)
tracker.init(frame,bb)


while True:
    ret, frame = v.read()
    if not ret:
        break
    #frame = imutils.resize(frame,width=600)
    (success,box) = tracker.update(frame)
    if success:
        (x,y,w,h) = [int(a) for a in box]
        cv2.rectangle(frame,(x,y),(x+w,y+h),(100,255,0),2)
    cv2.imshow('Frame',frame)
    key = cv2.waitKey(5) & 0xFF
    if key == ord('q'):
        break
v.release()
cv2.destroyAllWindows()


Code for Multiple Object Tracking:

import cv2
import numpy as np

TrDict = {'csrt': cv2.TrackerCSRT_create,
         'kcf' : cv2.TrackerKCF_create,
         'boosting' : cv2.TrackerBoosting_create,
         'mil': cv2.TrackerMIL_create,
         'tld': cv2.TrackerTLD_create,
         'medianflow': cv2.TrackerMedianFlow_create,
         'mosse':cv2.TrackerMOSSE_create}

trackers = cv2.legacy.MultiTracker_create()

v = cv2.VideoCapture(r'mot.mp4')

ret, frame = v.read()

k = 4
for i in range(k):
    cv2.imshow('Frame',frame)
    bbi = cv2.selectROI('Frame',frame)
    tracker_i = TrDict['csrt']()
    trackers.add(cv2.legacy.TrackerCSRT_create(),frame,bbi)


frameNumber = 2
baseDir = r'TrackingResults'

while True:
    ret, frame = v.read()
    if not ret:
        break
    (success,boxes) = trackers.update(frame)
    np.savetxt(baseDir + '/frame_'+str(frameNumber)+'.txt',boxes,fmt='%f')
    frameNumber+=1
    for box in boxes:
        (x,y,w,h) = [int(a) for a in box]
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
       
    cv2.imshow('Frame',frame)
    key = cv2.waitKey(5) & 0xFF
    if key == ord('q'):
        break
v.release()
cv2.destroyAllWindows()


boxes = np.loadtxt(r'TrackingResults\frame_102.txt')

boxes

Comments

Popular posts from this blog

Assignment 4: Canny Edge Detection

Assignment 1.2 - Transformation

Assignment 1.1: Image Transformation in Python