Object Tracking Using Open CV Python With Source Code

Introduction :

Object tracking in computer vision library is a crucial task . We make it using Open CV , a powerful computer vision library . Through this , object tracking becomes efficient and accessible . It determine the location of object initial frame . Open CV gave several offers to this like colour based tracking , template matching and many more advanced methods like Meanshift , Camshift , etc. This system will firstly detect the object and then track this by moving the object . This system is also used in robotics technology , sports analysis , etc. To make this system , first install packages and then paste the code given below and run the code . After this , you will see the output . 

So , Let’s Make .

Language And Interface :-
Object Tracking Using Python , A Versatile Programming Language With Source Code .

Required Modules Or Packages:-

You Need The Packages List Given Below :-

1. cv2 :- It is a library to use in Computer Vision task. You will get basic image processing tools while using cv2 for example, Image Reading and Writing .

How To Run The Code :-

Step 1 . First , You Download and Install Visual Studio Code or VS Code In your PC or Laptop by VS Code Official Website .
Step 2 . Now Open CMD As Administrator and install the above packages using pip .
Step 3 . Now Open Visual Studio Code .
Step 4. Now Make The file named as objecttracking.py .
Step 5 . Now Copy And Paste The Code from the Link Given Below ⬇️
Step 7 . After pasting The code , Save This & Click On Run Button .
Step 8 . Now You will See The Output .

Code Explanation :-

This Python code is used to Tracking Objects from Any Image Files format . 

Step-by-Step Guide Given Below:-
Imports :
• cv2
:- It is a library to use in Computer Vision task. You will get basic image processing tools while using cv2 for example, Image Reading and Writing .
Choosing the Tracker :

				
					#tracker = cv2.TrackerBoosting_create()
#tracker = cv2.TrackerMIL_create()
#tracker = cv2.TrackerKCF_create()
#tracker = cv2.TrackerTLD_create()
#tracker = cv2.TrackerMedianFlow_create()
#tracker = cv2.TrackerCSRT_create()
tracker = cv2.TrackerMOSSE_create()
				
			

Multiple Trackers are available in Open CV . TrackerMOSSE_create()is a fastest tracking method .
cap = cv2.VideoCapture(1) : This line initialize the video capture from camera .
Drawing the tracking Box :

				
					def drawBox(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)), (255, 0, 255), 3, 3)
cv2.putText(img, "Tracking", (100, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 255, 0), 2)
				
			

This code draw a rectangle around the detected objects 
Tracking success : If the object is detected and tracking , then it draw the rectangle around detected object .
Tracking Failure : If the object is not detected , then this show the text “Lost” .
FPS Calculation :

				
					fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
if fps > 60: myColor = (20, 230, 20)
elif fps > 20: myColor = (230, 20, 20)
else: myColor = (20, 20, 230)
cv2.putText(img, str(int(fps)), (75, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
myColor, 2)
				
			

This code Calculates the FPS of Objects .
Display Result :

				
					cv2.imshow("Tracking", img)
if cv2.waitKey(1) & 0xff == ord('q'):
break
				
			

This code Display the result .
Program Exit :
When The Loop is broken , then the program will exit .

Source Code :-

Your download is starting now...

Output :-

Find More Projects

Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …

Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …

Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …

Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …

Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …

Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …

Get Huge Discounts
More Python Projects

Download 1000+ Coding Projects Code