This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
/
MultiObjectTracking.py
112 lines (75 loc) · 2.67 KB
/
MultiObjectTracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
##############################################################
########## Object Tracking using OpenCV and Python ##########
##############################################################
#########################################
##### Multi Object Tracking #####
#########################################
##### Importing Libraries #####
import cv2
import sys
from random import randint
##### Trackers #####
tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
##### Tracker functiton #####
def tracker_name(tracker_types):
if tracker_type == tracker_types[0]:
tracker = cv2.TrackerBoosting_create()
elif tracker_type == tracker_types[1]:
tracker = cv2.TrackerMIL_create()
elif tracker_type == tracker_types[2]:
tracker = cv2.TrackerKCF_create()
elif tracker_type == tracker_types[3]:
tracker = cv2.TrackerTLD_create()
elif tracker_type == tracker_types[4]:
tracker = cv2.TrackerMeidanFlow_create()
elif tracker_type == tracker_types[5]:
tracker = cv2.TrackerGOTURN_create()
elif tracker_type == tracker_types[6]:
tracker = cv2.TrackerMosse_create()
elif tracker_type == tracker_types[7]:
tracker = cv2.TrackerCSRT_create()
else:
tracker = None
print('No Tracker Found')
print('Choose from these trackers')
for tr in tracker_types:
print(ta)
return tracker
if __name__ == '__main__':
print("Default Tracking Algorithm MOSSE \n"
"Available Algorithms are: \n")
for tr in tracker_types:
print(tr)
trackerType = 'MOSSE'
cap = cv2.VideoCapture('Videos/Vehicles.mp4')
sucess, frame = cap.read()
if not sucess:
print('Cannot read the Video')
rects = []
colors = []
while True:
rect_box = cv2.selectROI('MultiTracker', frame)
rects.append(rect_box)
colors.append((randint(64,255), randint(64,255), randint(64,255)))
print('Press "Q" to stop selectiniig boxes and start multitracking')
print('Press any key to select another box')
if cv2.waitKey(0) & 0xFF == 113:
break
print('Selected boxes {rects}')
multitracker = cv2.MultiTracker_create()
for rect_box in rects:
multitracker.add(tracker_name(tracker_type), frame, rect_box)
while cap.isOpened():
success, frame = cap.read()
if not success:
break
success, boxes = multitracker.update(frame)
for i, newbox in enumerate(boxes):
pt1 = (int(newbox[0]), int(newbox[1]))
pt2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
cv2.rectangle(frame, pt1, pt2, colors[i], 2, 1)
cv2.imshow('MuiltiTracker', frame)
if cv2.waitKey(20) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()