-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-video2.lua
169 lines (153 loc) · 5.28 KB
/
test-video2.lua
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
torch.setdefaulttensortype('torch.FloatTensor')
require 'opticalflow'
require 'image'
require 'matching'
require 'prettydisplay'
require 'framegrabber'
require 'filtering'
require 'planning'
require 'ardrone'
local hwin = 10
local wwin = 16
local filters0 = torch.LongTensor{
{0, 4, 8, 8, 12, 0, 16, 8},
{0, 8, 8, 12, 12, 4, 16, 12},
{8, 0, 12, 12, 4, 4, 16, 8},
{0, 0, 4, 8, 8, 0, 16, 4},
{8, 4, 12, 16, 4, 8, 16, 12},
{4, 0, 8, 12, 0, 4, 12, 8},
{0, 12, 8, 16, 12, 8, 16, 16},
{4, 4, 8, 16, 0, 8, 12, 12},
{0, 4, 8, 12, 0, 0, 4, 16},
{0, 4, 4, 12, 8, 4, 16, 8},
{4, 0, 12, 16, 0, 4, 16, 12},
{4, 0, 8, 4, 0, 0, 4, 4},
{4, 4, 12, 12, 4, 0, 8, 16},
{4, 4, 8, 8, 0, 4, 4, 8},
{0, 8, 4, 16, 8, 8, 16, 12},
{8, 0, 12, 4, 4, 0, 8, 4},
{4, 8, 8, 12, 0, 8, 4, 12},
{4, 12, 8, 16, 0, 12, 4, 16},
{8, 4, 12, 8, 4, 4, 8, 8},
{8, 4, 16, 12, 8, 0, 12, 16},
{8, 8, 12, 12, 4, 8, 8, 12},
{8, 12, 12, 16, 4, 12, 8, 16},
{12, 0, 16, 8, 0, 4, 8, 8},
{0, 4, 12, 8, 0, 0, 4, 12},
{12, 0, 16, 4, 8, 0, 12, 4},
{12, 4, 16, 12, 0, 8, 8, 12},
{4, 0, 12, 4, 0, 0, 8, 4},
{0, 8, 12, 12, 0, 4, 4, 16},
{12, 4, 16, 8, 8, 4, 12, 8},
{12, 8, 16, 12, 8, 8, 12, 12},
{12, 12, 16, 16, 8, 12, 12, 16},
{4, 4, 12, 8, 0, 4, 8, 8},
}
--[[
local filters0= torch.LongTensor{{ 0, 0, 8, 8, 8, 8,16,16},
{ 8, 0,16, 8, 0, 8, 8,16},
{ 0, 0, 8,16, 8, 0,16,16},
{ 0, 0,16, 8, 0, 8,16,16},
{ 4, 4, 8, 8, 8, 8,12,12},
{ 8, 4,12, 8, 4, 8, 8,12},
{ 4, 0, 8,16, 8, 0,12,16},
{ 0, 4,16, 8, 0, 8,16,12},
{ 0, 0, 4,16, 4, 0, 8,16},
{ 8, 0,12,16, 12, 0,16,16},
{ 0, 0,16, 4, 0, 4,16, 8},
{ 0, 8,16,12, 0,12,16,16},
}
--]]
local filters = nn.Sequential()
--filters:add(nn.SpatialContrastiveNormalization(n_chans,image.gaussian1D(k_norm)))
filters:add(nn.BlockFilter(filters0))
local filters2 = filters:clone()
--TODO depends on the size of the filters
local matcher = nn.BinaryMatching(hwin, wwin, 7, 8, 7, 8)
local matcher2 = nn.BinaryMatching(hwin, wwin, 7, 8, 7, 8)
function loadImgFile(i)
return image.rgb2y(image.scale(image.load(string.format("data/%09d.jpg", i)),
320, 180, 'bilinear'))
end
local fg
function loadImgCam(i)
fg = fg or new_framegrabber(240, 320)
return image.rgb2y(fg:grab())
end
ardrone.initvideo()
function loadImgAR(i)
fr = ardrone.getframe(fr)
return image.scale(fr, 320, 180):resize(1, 180, 320)
end
loadImg = loadImgAR
local i_img = 1
local im1 = loadImg(i_img)
local im12 = image.scale(im1, im1:size(3)/2, im1:size(2)/2)
local im1filtered = filters:forward(im1):clone()
local im1filtered2 = filters2:forward(im12):clone()
local win, win2, win3, win4, win5
local timer = torch.Timer()
local flow = torch.ByteTensor()
local flowreal = torch.Tensor()
local flowrealdisp = torch.Tensor()
local meantfilter = 0
local nfilter = 0
local totaltime = 0
while true do
--sys.execute('sleep 1')
i_img = i_img + 1
local im2 = loadImg(i_img)
local im22 = image.scale(im2, im1:size(3)/2, im1:size(2)/2)
timer:reset()
local im2filtered = filters:forward(im2)
local im2filtered2 = filters2:forward(im22)
meantfilter = nfilter/(nfilter+1)*meantfilter + timer:time()['real']/(nfilter+1)
nfilter = nfilter + 1
print("toc filters : ", timer:time()['real'])
print("mean filters : ", meantfilter)
local flow1, score1 = matcher:forward{im1filtered, im2filtered}
local flow2, score2 = matcher2:forward{im1filtered2, im2filtered2}
print("toc match : ", timer:time()['real'])
--local tmed = torch.Timer()
MergeFlow(flow1, score1, flow2, score2, flow,
math.floor((hwin-1)/2), math.floor((wwin-1)/2), 1,1)
--print("merge ", tmed:time()['real'])
MedianFilter(flow, 3)
print("toc median : ", timer:time()['real'])
totaltime = totaltime + timer:time()['real']
print(" ===== FPS : ",1/(timer:time()['real']), " =====")
flowreal:resize(flow:size())
flowreal:copy(flow)
flowreal[1]:add(-math.floor((hwin-1)/2)*2)
flowreal[2]:add(-math.floor((wwin-1)/2)*2)
flowrealdisp:resizeAs(flowreal)
flowrealdisp:copy(flowreal)
local norm = (flowreal[1]:cmul(flowreal[1])+flowreal[2]:cmul(flowreal[2])):sqrt()
local x1,y1,x2,y2 = planningOld(norm)
--norm[{{y1+1,y2},{x1+1,x2}}]:add(10)
local score2b = image.scale(score2:real(), score1:size(2),
score1:size(1), 'simple')
local select = score1:real():gt(score2b)
local maxflow = math.max(math.ceil((hwin-1)/2)*2,math.ceil((wwin-1)/2)*2)
im2c = torch.Tensor(3, im2:size(2), im2:size(3))
for i = 1,3 do
im2c[i]:copy(im2[1])
end
im2c[{2,{y1+1,y2},{x1+1,x2}}]:zero()
local todisp=prettydisplay({{im2c,norm,flow1},
{flowrealdisp,flow2},
{score1:real(), score2b:real(),select}},
{{{},{},{}},
{{-maxflow,maxflow},{}},
{{0,10},{0,10},{}}})
win=image.display{image=todisp, win=win}
--image.save(string.format('output_video/%07d.jpg', i_img), todisp)
--[[win = image.display{image=norm, win=win, min=0, max=20}
win2 = image.display{image=im2, win=win2}
win3 = image.display{image=flow1, win=win3}
win4 = image.display{image=flow2, win=win4}
win5 = image.display{image=flow, win=win5}--]]
im1filtered = im2filtered:clone()
im1filtered2 = im2filtered2:clone()
print("---")
end