Skip to content

Commit cc0da5f

Browse files
committed
first commit
0 parents  commit cc0da5f

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# OpenCV-Object-Tracking

index.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import cv2
2+
import sys
3+
4+
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
5+
6+
if __name__ == '__main__' :
7+
8+
# Set up tracker.
9+
# Instead of MIL, you can also use
10+
11+
tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE']
12+
tracker_type = tracker_types[5]
13+
14+
if int(minor_ver) < 3:
15+
tracker = cv2.Tracker_create(tracker_type)
16+
else:
17+
if tracker_type == 'BOOSTING':
18+
tracker = cv2.TrackerBoosting_create()
19+
if tracker_type == 'MIL':
20+
tracker = cv2.TrackerMIL_create()
21+
if tracker_type == 'KCF':
22+
tracker = cv2.TrackerKCF_create()
23+
if tracker_type == 'TLD':
24+
tracker = cv2.TrackerTLD_create()
25+
if tracker_type == 'MEDIANFLOW':
26+
tracker = cv2.TrackerMedianFlow_create()
27+
if tracker_type == 'CSRT':
28+
tracker = cv2.TrackerCSRT_create()
29+
if tracker_type == 'MOSSE':
30+
tracker = cv2.TrackerMOSSE_create()
31+
32+
# Read video
33+
video = cv2.VideoCapture("./videos/chaplin.mp4")
34+
35+
# Exit if video not opened.
36+
if not video.isOpened():
37+
print("Could not open video")
38+
sys.exit()
39+
40+
# Read first frame.
41+
ok, frame = video.read()
42+
if not ok:
43+
print('Cannot read video file')
44+
sys.exit()
45+
46+
# Define an initial bounding box
47+
bbox = (287, 23, 86, 320)
48+
49+
# Uncomment the line below to select a different bounding box
50+
bbox = cv2.selectROI(frame, False)
51+
52+
# Initialize tracker with first frame and bounding box
53+
ok = tracker.init(frame, bbox)
54+
55+
while True:
56+
# Read a new frame
57+
ok, frame = video.read()
58+
if not ok:
59+
break
60+
61+
# Start timer
62+
timer = cv2.getTickCount()
63+
64+
# Update tracker
65+
ok, bbox = tracker.update(frame)
66+
67+
# Calculate Frames per second (FPS)
68+
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
69+
70+
# Draw bounding box
71+
if ok:
72+
# Tracking success
73+
p1 = (int(bbox[0]), int(bbox[1]))
74+
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
75+
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
76+
else :
77+
# Tracking failure
78+
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
79+
80+
# Display tracker type on frame
81+
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
82+
83+
# Display FPS on frame
84+
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
85+
86+
# Display result
87+
cv2.imshow("Tracking", frame)
88+
89+
# Exit if ESC pressed
90+
k = cv2.waitKey(1) & 0xff
91+
if k == 27 : break

videos/chaplin.mp4

15.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)