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

Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …

Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …

KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …

Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …

Snake Game Using C++ With Source Code Introduction : The Snake Game is one of the most well-known arcade games, having its …

C++ Hotel Management System With Source Code Introduction : It is very important to manage the hotels effectively to avail better customer …

Get Huge Discounts
More Python Projects

Download From Telegram