-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtools.py
208 lines (171 loc) · 7.3 KB
/
tools.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
import cv2
import os
import shutil
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Polygon
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid
from importlib import import_module
import vot
from matplotlib import cm
from numpy import linspace
def Tracking(Sequence, tracker_list, visualize = True):
if not os.path.exists('results/'):
os.mkdir("results")
print 'generate images.txt and region.txt files...'
with open("images.txt","w") as f:
while Sequence._frame < len(Sequence._images):
f.write(Sequence.frame()+'\n')
Sequence._frame+=1
Sequence._frame = 0
with open("region.txt", "w") as f:
f.write(open(os.path.join(Sequence.seqdir, 'groundtruth.txt'), 'r').readline())
print 'start tracking...'
for str in tracker_list:
print 'tracking using: '+str
import_module(str)
if not os.path.exists('results/'+str+'/'+Sequence.name):
os.makedirs('results/'+str+'/'+Sequence.name)
shutil.move("output.txt", 'results/'+str+'/'+Sequence.name+'/output.txt')
os.remove("images.txt")
os.remove("region.txt")
print 'Done!!'
if visualize:
visulize_result(Sequence, tracker_list)
def visulize_result(Sequence, tracker_list = None, visualize_gt = True):
fig = plt.figure(1)
if tracker_list:
assert (type(tracker_list) == list)
result = {}
start = 0.0
stop = 1.0
number_of_lines = 1000
cm_subsection = linspace(start, stop, number_of_lines)
colors = [cm.jet(x) for x in cm_subsection]
for str in tracker_list:
result[str] = open('results/' + str + '/' + Sequence.name+'/output.txt').readlines()
while Sequence._frame < len(Sequence._images):
img_rgb = cv2.imread(Sequence.frame())
plt.clf()
gt_data = Sequence.groundtruth[Sequence._frame]
if tracker_list == None:
pass
else:
for str in tracker_list:
tr_data = vot.convert_region(vot.parse_region(result[str][Sequence._frame]), Sequence._region_format)
if Sequence._region_format == 'rectangle':
tracking_figure_axes = plt.axes()
tracking_figure_axes.add_patch(Rectangle(
xy=(tr_data.x, tr_data.y),
width=tr_data.width,
height=tr_data.height,
facecolor='none',
edgecolor=colors[tracker_list.index(str)*number_of_lines / len(tracker_list)],
))
tracking_figure_axes.text(100, 20*(tracker_list.index(str)+1), str,
verticalalignment='bottom', horizontalalignment='right',
color=colors[tracker_list.index(str)*number_of_lines / len(tracker_list)], fontsize=15)
else:
a = []
for point in tr_data.points:
a.append([point.x, point.y])
tr_rect = Polygon(
xy=np.array(a),
facecolor='none',
edgecolor=colors[tracker_list.index(str)*number_of_lines / len(tracker_list)],
)
tracking_figure_axes = plt.axes()
tracking_figure_axes.add_patch(tr_rect)
if visualize_gt:
if Sequence._region_format == 'rectangle':
gt_rect = Rectangle(
xy=(gt_data.x, gt_data.y),
width=gt_data.width,
height=gt_data.height,
facecolor='none',
edgecolor='r',
)
tracking_figure_axes = plt.axes()
tracking_figure_axes.add_patch(gt_rect)
else:
a = []
for point in gt_data.points:
a.append([point.x, point.y])
gt_rect = Polygon(
xy=np.array(a),
facecolor='none',
edgecolor='r',
)
tracking_figure_axes = plt.axes()
tracking_figure_axes.add_patch(gt_rect)
plt.imshow(img_rgb)
plt.draw()
plt.waitforbuttonpress()
Sequence._frame += 1
def precision_plot(Sequence, tracker_list):
start = 0.0
stop = 1.0
number_of_lines = 1000
cm_subsection = linspace(start, stop, number_of_lines)
colors = [cm.jet(x) for x in cm_subsection]
fig = plt.Figure()
plt.xlabel('Threshold')
plt.ylabel('Precision')
plt.ylim(0, 1)
max_threshold = 50
gt = [[data.y + data.height / 2, data.x + data.width / 2] for data in Sequence.groundtruth]
gt = np.array(gt)
for str in tracker_list:
precisions = np.zeros(shape = [max_threshold])
result = np.loadtxt('results/' + str + '/' + Sequence.name + '/output.txt',delimiter=',')
positions = result[:,[1,0]]+result[:,[3,2]]/2
distance = np.sqrt(np.sum(np.power(positions-gt,2),1))
for p in range(max_threshold):
precisions[p] = float(np.count_nonzero(distance<(p+1)))/distance.shape[0]
plt.plot(precisions,color =colors[tracker_list.index(str)*number_of_lines / len(tracker_list)], label =str)
plt.legend()
plt.show()
def overlap_plot(Sequence, tracker_list):
start = 0.0
stop = 1.0
number_of_lines = 1000
cm_subsection = linspace(start, stop, number_of_lines)
colors = [cm.jet(x) for x in cm_subsection]
fig = plt.Figure()
plt.xlabel('Threshold')
plt.ylabel('Overlap')
plt.ylim(0, 1)
plt.xlim(0, 1)
inter_p = 100
gt = [[data.x, data.y, data.width, data.height] for data in Sequence.groundtruth]
gt = np.array(gt)
for str in tracker_list:
Thresholds = np.arange(0,1,1.0/inter_p)+1.0/inter_p
overlap_precision = np.zeros(shape=[inter_p])
result = np.loadtxt('results/' + str + '/' + Sequence.name + '/output.txt', delimiter=',')
endX = np.max(np.vstack((result[:,0]+result[:,2],gt[:,0]+gt[:,2])),axis=0)
startX = np.min(np.vstack((result[:,0], gt[:,0])),axis=0)
width = result[:,2]+gt[:,2]-(endX-startX)
width[width < 0] = 0
endY = np.max(np.vstack((result[:, 1] + result[:, 3], gt[:, 1] + gt[:, 3])), axis=0)
startY = np.min(np.vstack((result[:, 1], gt[:, 1])), axis=0)
height = result[:, 3] + gt[:, 3] - (endY - startY)
height[height < 0] = 0
Area = np.multiply(width,height)
Area1 = np.multiply(result[:,2],result[:,3])
Area2 = np.multiply(gt[:,2],gt[:,3])
overlap_ratio = np.divide(Area,Area1+Area2-Area)
for p in range(inter_p):
overlap_precision[p] = float(np.count_nonzero(overlap_ratio > Thresholds[p])) / overlap_ratio.shape[0]
plt.plot(overlap_precision, Thresholds, color=colors[tracker_list.index(str)*number_of_lines / len(tracker_list)], label=str)
plt.legend()
plt.show()
def imshow_grid(images, shape=[3, 10]):
"""Plot images in a grid of a given shape."""
fig = plt.figure(1)
grid = ImageGrid(fig, 111, nrows_ncols=shape, axes_pad=0.05)
size = shape[0] * shape[1]
for i in range(size):
grid[i].axis('off')
grid[i].imshow(images[i]/np.max(images[i]),cmap=plt.cm.gray) # The AxesGrid object work as a list of axes.
plt.show()