forked from danstowell/gmphd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessDetections.py
138 lines (106 loc) · 4.23 KB
/
ProcessDetections.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
import collections,math,numpy,os,scipy
from matplotlib import image,patches,pyplot
from numpy import linalg,random
from os import path
from scipy import special
import gmphd
relpath='flir_17_Sept_2013/ETHZ-ASL'
imwidth=324
imheight=256
sampfreq=20.0
numdim=2
maxpos=500.0
maxvel=10.0
accelnoise=5.0
obsnoise=10.0
birthrate=0.01
clutterrate=2.0
survprob=0.99
detecprob=0.95
truncthres=1.0e-12
mergethres=0.5
maxhypot=20
names=collections.defaultdict(list)
detections=collections.defaultdict(list)
abspath=path.join(path.dirname(__file__),relpath)
# Store the image names.
for file in os.listdir(path.join(abspath,'8bit')):
if file.endswith('.png'):
name,extension=file.split('.')
names[int(name)]=file
# Load the detections.
with open(path.join(abspath,'detection.txt'),mode='r') as file:
for line in file:
frame,col,row,width,height=map(int,line.split(',')[:-1])
detections[frame].append((col,row,width,height))
if numdim==2:
# Create the initial weights, means and covariances.
initweight=numpy.array([0.5,0.5])
initmean=numpy.array([(0.0,float(imwidth)),(float(imheight),float(imheight)),(0.0,0.0),(0.0,0.0)])
initcovar=numpy.diag(numpy.array([maxpos,maxpos,maxvel,maxvel])**2)[:,:,numpy.newaxis].repeat(2,axis=2)
timestep=1.0/sampfreq
# Create the transition gain and noise.
transgain=numpy.kron(numpy.array([(1.0,timestep),(0.0,1.0)]),numpy.eye(numdim))
transnoise=numpy.kron(accelnoise**2*numpy.array([(timestep**2/4.0,timestep/2.0),
(timestep/2.0,1.0)]),numpy.eye(numdim))
# Create the measurement gain and noise.
measgain=numpy.kron(numpy.array([1.0,0.0]),numpy.eye(numdim))
measnoise=numpy.kron(numpy.array([obsnoise**2]),numpy.eye(numdim))
# Define the clutter density.
area=imwidth*imheight
clutterdens=lambda x:1.0/float(area)
# Instantiate the filter.
filt=gmphd.filt(initweight,initmean,initcovar,transgain,transnoise,measgain,measnoise,
clutterdens,birthrate,clutterrate,survprob,detecprob)
scale=special.gammaincinv(numdim,0.99)
# Create a figure and a pair of axes.
fig,axes=pyplot.subplots()
axes.invert_yaxis()
for frame in range(min(names.keys()),max(names.keys())):
try:
# Perform a prediction-update step.
filt.pred()
if frame in detections:
obs=numpy.array(detections[frame],dtype=float).transpose()
obs[:2,:]+=obs[2:,:]/2.0
filt.update(obs[:numdim,:],numpy.spacing(1.0))
filt.prune(truncthres=truncthres,
mergethres=mergethres,
maxhypot=maxhypot)
axes.cla()
# Plot the image.
plot=axes.imshow(image.imread(path.join(abspath,'8bit',names[frame])))
plot.set_cmap('hot')
for weight,mean,covar in filt:
# Decompose the dispersion matrix of the position.
eigval,eigvec=linalg.eigh(covar[numpy.ix_([0,1],[0,1])])
width,height=numpy.sqrt(eigval)
angle=numpy.degrees(numpy.arctan2(*eigvec[:,0][::-1]))
# Create an ellipse depicting the position uncertainty.
ellip=patches.Ellipse(xy=mean[numpy.ix_([0,1])],
width=scale*width,
height=scale*height,
angle=angle,
alpha=min(max(weight,0.0),1.0),
facecolor='blue',
edgecolor='none')
axes.add_artist(ellip)
# Plot the detections.
if frame in detections:
for x,y,u,v in detections[frame]:
axes.plot([x,x+u,x+u,x,x],[y,y,y+v,y+v,y],
color='black',
linewidth=2.0)
axes.set_xlim(-0.5,float(imwidth)-0.5)
axes.set_ylim(float(imheight)-0.5,-0.5)
# Indicate the frame number.
axes.annotate('Frame {}'.format(frame),
xy=(0.99,0.99),
xycoords='axes fraction',
fontsize=16,
horizontalalignment='right',
verticalalignment='top')
fig.show()
pyplot.pause(0.01)
except KeyboardInterrupt:
break