-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyTAG.py
231 lines (197 loc) · 7.52 KB
/
pyTAG.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import cv2
import sys
traker_bounding_box_dictionary = {}
# Do not forget to modify the two variables below
video_source = "/video/%04d.png"
groundtruth_report_dir = "/foo1/bar1.txt"
tracker_type = 'CSRT'
tracker = cv2.TrackerCSRT_create()
selection = 0
def reinit_tracker(restart,frameNumber,video):
global tracker
global tracker_type
global selection
# Max number of frames
n_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
ok, frame = video.read()
print('#' + str('{0:04}'.format(frameNumber)))
bbox = (0, 0, 0, 0)
if not ok:
print ('Cannot read video file')
sys.exit()
# Uncomment the line below to select a different bounding box
if restart:
#frameNumber = frameNumber - 1
video.set(1,frameNumber)
ret, frame = video.read()
bbox = cv2.selectROI(frame, False)
cv2.destroyWindow("ROI selector")
tracker = selector(tracker_type)
print (bbox)
else:
bbox = (284, 130, 63, 176)
# Initialize tracker with first frame and bounding box
#tracker = cv2.TrackerTLD_create()
#tracker = cv2.TrackerCSRT_create()
print (bbox)
ok = tracker.init(frame, bbox)
"""if (frameNumber == 1):
#frameID = "frame_"+str(frameNumber)
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = '{0} {1} {2} {3}'.format(int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]))
else:
#frameID = "frame_"+str(frameNumber)
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = "0 0 0 01"
"""
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = '{0} {1} {2} {3}'.format(int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]))
while True:
# Read a new frame
ok, frame = video.read()
print('#' + str('{0:04}'.format(frameNumber)))
if not ok:
break
# Start timer
timer = cv2.getTickCount()
# Update tracker
ok, bbox = tracker.update(frame)
# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
# Draw bounding box
if ok:
frameNumber = frameNumber + 1
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
coordiante = '{0} {1} {2} {3}'.format(int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]))
#frameID = "frame_"+str(frameNumber)
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = str(coordiante)
else :
# Tracking failure
cv2.putText(frame, "The tracker has failed, please reinitialise", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
restart = True
cv2.destroyWindow("pyTAG")
print ("The tracker failed on frame number: " + str('{0:04}'.format(frameNumber)))
ok, frame = video.read()
reinit_tracker(restart,frameNumber,video)
# Display tracker type on frame
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
# Display FPS on frame
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
cv2.putText(frame, "Frame number: " + str('{0:04}'.format(frameNumber)), (100,80),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2 )
# Display result
cv2.imshow("pyTAG", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 32 :
restart = True
print ("(Manually restart) The tracker failed on frame number: " + str('{0:04}'.format(frameNumber)))
#cv2.destroyWindow("ITAGGen")
#ok, frame = video.read()
while video.isOpened():
#cv2.putText(frame, "Frame number: " + str('{0:04}'.format(frameNumber)), (100,80),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2 )
# Read video capture
#ret, frame = video.read()
# show one frame at a time
key = cv2.waitKey(0)
while key not in [32, ord('n'), ord('p'), ord('a')]:
key = cv2.waitKey(0)
if key == ord('n'):
print(key)
if frameNumber < n_frames:
#frameID = "frame_"+str(frameNumber)
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = "0 0 0 0"
frameNumber = frameNumber + 1
video.set(1,frameNumber)
ret, frame = video.read()
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
cv2.putText(frame, "Frame number: " + str('{0:04}'.format(frameNumber)), (100,80),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2 )
if key == ord('p'):
print(key)
if n_frames >= frameNumber and frameNumber != 0:
#frameID = "frame_"+str(frameNumber)
frameID = frameNumber
traker_bounding_box_dictionary[frameID] = "XXX"
frameNumber = frameNumber - 1
video.set(1,frameNumber)
ret, frame = video.read()
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
cv2.putText(frame, "Frame number: " + str('{0:04}'.format(frameNumber)), (100,80),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2 )
# Quit when 'q' is pressed
if key == ord('a'):
print(key)
selection = selection + 1
selection=(selection%6)
print(selection)
if(selection == 0):
tracker_type = 'CSRT'
tracker = cv2.TrackerCSRT_create()
elif(selection == 1):
tracker_type = 'TLD'
tracker = cv2.TrackerTLD_create()
elif(selection == 2):
tracker_type = 'KCF'
tracker = cv2.TrackerKCF_create()
elif(selection == 3):
tracker_type = 'BOOSTING'
tracker = cv2.TrackerBoosting_create()
elif(selection == 4):
tracker_type = 'MIL'
tracker = cv2.TrackerMIL_create()
elif(selection == 5):
tracker_type = 'MEDIANFLOW'
tracker = cv2.TrackerMedianFlow_create()
"""elif(selection == 6):
tracker_type = 'MOSSE'
tracker = cv2.TrackerMOSSE_create()"""
video.set(1,frameNumber)
ret, frame = video.read()
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
cv2.putText(frame, "Frame number: " + str('{0:04}'.format(frameNumber)), (100,80),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2 )
if key == 32:
video.set(1,frameNumber)
ret, frame = video.read()
#frameNumber = frameNumber - 1
#video.set(1,frameNumber)
#frameID = "frame_"+str(frameNumber)
# frameID = frameNumber
#traker_bounding_box_dictionary[frameID] = "0 0 0 0"
break
# Display each frame
cv2.imshow("pyTAG", frame)
#cv2.destroyWindow("ROI selector")
reinit_tracker(restart,frameNumber,video)
def selector(tracker_type):
if(tracker_type == 'CSRT'):
tracker = cv2.TrackerCSRT_create()
elif(tracker_type == 'TLD'):
tracker = cv2.TrackerTLD_create()
elif(tracker_type == 'KCF'):
tracker = cv2.TrackerKCF_create()
elif(tracker_type == 'BOOSTING'):
tracker = cv2.TrackerBoosting_create()
elif(tracker_type == 'MIL'):
tracker = cv2.TrackerMIL_create()
elif(tracker_type == 'MEDIANFLOW'):
tracker = cv2.TrackerMedianFlow_create()
return tracker;
if __name__ == '__main__':
frameNumber = 0
restart = False
video = cv2.VideoCapture(video_source)
# Exit if video not opened.
if not video.isOpened():
print ("Could not open video")
sys.exit()
reinit_tracker(restart,frameNumber,video)
# Write tracker ground truth file destination
#track_file = open("C:/Users/Ekincan/Desktop/MU/testing-framework-tracking/final/csrt.txt", "w")
track_file = open(ground_truth_report_dir, "w")
for key in sorted(traker_bounding_box_dictionary.keys()):
print(str(key)+" :: "+traker_bounding_box_dictionary[key])
track_file.write(traker_bounding_box_dictionary[key]+"\n")
track_file.close()