FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NormalMapTFJS/OpticalFlowNode.js at main · Graphnull/NormalMapTFJS · GitHub
Graphnull
/
NormalMapTFJS
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NormalMapTFJS
/
OpticalFlowNode.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
236 lines (192 loc) · 10.1 KB
Breadcrumbs
NormalMapTFJS
/
OpticalFlowNode.js
Copy path
File metadata and controls
236 lines (192 loc) · 10.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
232
233
234
235
236
//import { env } from '@tensorflow/tfjs-core';
//import { getGlslDifferences } from '@tensorflow/tfjs-backend-webgl/dist/glsl_version';
//import { getKernel } from '@tensorflow/tfjs-core/dist/kernel_registry';
let
tf
=
require
(
'@tensorflow/tfjs-node'
)
;
//import '@tensorflow/'
let
fs
=
require
(
'fs'
)
let
{
opticalFlowFind
,
blur
}
=
require
(
'nodleten/src/kernels/opticatFlowKernels'
)
let
custBlur
=
(
fld
,
ks
,
step
)
=>
{
fld
=
tf
.
conv2d
(
tf
.
stack
(
fld
.
unstack
(
-
1
)
.
map
(
v
=>
v
.
reshape
(
[
v
.
shape
[
0
]
,
v
.
shape
[
1
]
,
1
]
)
)
)
,
tf
.
fill
(
[
ks
,
ks
,
1
,
1
]
,
1
/
(
ks
*
ks
)
)
,
[
step
,
step
]
,
'valid'
)
fld
=
tf
.
concat
(
fld
.
unstack
(
)
,
-
1
)
return
fld
;
}
let
minIndex
=
(
arr
)
=>
{
let
i
=
0
;
let
minV
=
Infinity
;
for
(
let
j
=
0
;
j
<
arr
.
length
;
j
++
)
{
if
(
minV
>
arr
[
j
]
)
{
minV
=
arr
[
j
]
;
i
=
j
}
}
return
i
;
}
//import '@tensorflow/tfjs-backend-webgl/dist/register_all_kernels.js';
//const glsl = getGlslDifferences();
let
ks
=
7
let
hks
=
Math
.
floor
(
ks
/
2
)
let
dil
=
1
;
let
dk
=
ks
/
dil
let
step
=
4
let
height
=
436
/
4
let
width
=
1024
/
4
let
run
=
async
(
)
=>
{
//console.log('tf', tf.engine().registryFactory, tf.getBackend());
let
log
=
console
.
log
log
(
'started'
)
try
{
let
video
=
tf
.
node
.
decodePng
(
fs
.
readFileSync
(
'./public/frame_0002.png'
)
,
3
)
//await new Promise((res) => { let img = new Image(); img.onload = () => res(img); img.src = '/NormalMapTFJS/frame_0002.png' })
let
video2
=
tf
.
node
.
decodePng
(
fs
.
readFileSync
(
'./public/frame_0001.png'
)
,
3
)
//await new Promise((res) => { let img = new Image(); img.onload = () => res(img); img.src = '/NormalMapTFJS/frame_0001.png' })
//let canvas = document.getElementById('result')
//let canvas2 = document.getElementById('result2')
await
tf
.
setBackend
(
'tensorflow'
)
;
await
new
Promise
(
(
res
)
=>
setTimeout
(
res
,
100
)
)
let
height
=
video
.
shape
[
0
]
;
let
width
=
video
.
shape
[
1
]
;
let
flowData
=
tf
.
tensor
(
new
Float32Array
(
fs
.
readFileSync
(
'./public/frame_0001.flo'
)
.
buffer
.
slice
(
3
*
4
)
)
,
[
height
,
width
,
2
]
)
flowData
=
tf
.
concat
(
[
flowData
,
tf
.
zeros
(
[
height
,
width
,
1
]
)
]
,
-
1
)
console
.
log
(
'height: '
,
height
,
width
)
;
//let current = tf.zeros([1, height, width, 3]);
//let pred = tf.zeros([1, height, width, 3]);
let
current2
=
tf
.
zeros
(
[
1
,
height
/
2
,
width
/
2
,
3
]
)
;
let
pred2
=
tf
.
zeros
(
[
1
,
height
/
2
,
width
/
2
,
3
]
)
;
let
current4
=
tf
.
zeros
(
[
1
,
height
/
4
,
width
/
4
,
3
]
)
;
let
pred4
=
tf
.
zeros
(
[
1
,
height
/
4
,
width
/
4
,
3
]
)
;
let
flowData2
=
flowData
.
resizeBilinear
(
[
height
/
2
,
width
/
2
]
)
;
let
flowData4
=
flowData
.
resizeBilinear
(
[
height
/
4
,
width
/
4
]
)
;
let
backend
=
tf
.
backend
(
)
let
uv
=
tf
.
tensor
(
[
0
,
0
,
1
,
0
,
2
,
0
,
3
,
0
,
4
,
0
,
5
,
0
,
6
,
0
,
0
,
1
,
1
,
1
,
2
,
1
,
3
,
1
,
4
,
1
,
5
,
1
,
6
,
1
,
0
,
2
,
1
,
2
,
2
,
2
,
3
,
2
,
4
,
2
,
5
,
2
,
6
,
2
,
0
,
3
,
1
,
3
,
2
,
3
,
3
,
3
,
4
,
3
,
5
,
3
,
6
,
3
,
0
,
4
,
1
,
4
,
2
,
4
,
3
,
4
,
4
,
4
,
5
,
4
,
6
,
4
,
0
,
5
,
1
,
5
,
2
,
5
,
3
,
5
,
4
,
5
,
5
,
5
,
6
,
5
,
0
,
6
,
1
,
6
,
2
,
6
,
3
,
6
,
4
,
6
,
5
,
6
,
6
,
6
]
,
[
ks
*
ks
,
2
]
)
.
sub
(
hks
)
let
update
=
(
)
=>
{
let
result
=
null
;
let
time
=
new
Date
(
)
try
{
// if (pred) {
// pred.dispose();
// }
// pred = current;
current2
=
video
.
expandDims
(
)
.
div
(
255
)
let
temp
=
current2
;
current2
=
current2
.
resizeBilinear
(
[
height
/
2
,
width
/
2
]
)
temp
.
dispose
(
)
;
temp
=
current4
current4
=
current2
.
resizeBilinear
(
[
height
/
4
,
width
/
4
]
)
temp
.
dispose
(
)
;
pred2
=
video2
.
expandDims
(
)
.
div
(
255
)
temp
=
pred2
;
pred2
=
pred2
.
resizeBilinear
(
[
height
/
2
,
width
/
2
]
)
temp
.
dispose
(
)
;
temp
=
pred4
;
pred4
=
pred2
.
resizeBilinear
(
[
height
/
4
,
width
/
4
]
)
temp
.
dispose
(
)
;
result
=
tf
.
tidy
(
(
)
=>
{
let
time
=
new
Date
(
)
let
x
=
0
;
let
y
=
0
;
let
t
;
let
result
=
opticalFlowFind
(
[
tf
.
tensor
(
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
]
,
[
2
,
2
,
2
]
)
,
pred4
,
current4
]
,
{
step
:
step
,
kernelSize
:
ks
}
)
;
let
px
=
result
.
shape
[
1
]
let
py
=
result
.
shape
[
0
]
let
shift
=
result
.
reshape
(
[
py
*
px
,
ks
*
ks
*
1
]
)
.
mean
(
-
2
)
;
//tf.node.encodePng(shift.reshape([ks, ks, 1]).sub(0.01).mul(300).toInt()).then((data) => fs.writeFileSync('./resultshift.png', data));
let
shiftRes
=
minIndex
(
shift
.
dataSync
(
)
)
;
x
=
Math
.
floor
(
shiftRes
%
ks
)
-
hks
;
y
=
Math
.
floor
(
shiftRes
/
ks
)
-
hks
;
result
=
opticalFlowFind
(
[
tf
.
tensor
(
[
x
,
y
,
x
,
y
,
x
,
y
,
x
,
y
]
,
[
2
,
2
,
2
]
)
,
pred4
,
current4
]
,
{
step
:
step
,
kernelSize
:
ks
}
)
;
result
=
custBlur
(
result
.
reshape
(
[
py
,
px
,
ks
*
ks
]
)
,
3
,
2
)
;
result
=
result
.
reshape
(
[
result
.
shape
[
0
]
,
result
.
shape
[
1
]
,
ks
*
ks
,
1
]
)
;
let
pos
=
result
.
argMin
(
-
2
)
;
let
xp
=
pos
.
mod
(
ks
)
.
sub
(
hks
)
.
add
(
x
)
;
let
yp
=
pos
.
floorDiv
(
ks
)
.
sub
(
hks
)
.
add
(
y
)
;
let
out
=
tf
.
concat
(
[
xp
,
yp
,
tf
.
zeros
(
xp
.
shape
)
]
,
-
1
)
.
mul
(
27
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
;
let
fld
=
flowData4
.
slice
(
[
3
,
3
,
0
]
,
[
Math
.
floor
(
(
flowData4
.
shape
[
0
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
Math
.
floor
(
(
flowData4
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
3
]
)
;
fld
=
custBlur
(
fld
,
ks
,
step
)
fld
=
custBlur
(
fld
,
3
,
2
)
tf
.
node
.
encodePng
(
fld
.
mul
(
27
)
.
mul
(
0.5
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result1f.png'
,
data
)
)
;
tf
.
node
.
encodePng
(
out
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result1.png'
,
data
)
)
;
// 2 step
let
back2
=
tf
.
concat
(
[
xp
,
yp
]
,
-
1
)
console
.
log
(
'22'
,
pred2
.
slice
(
[
0
,
hks
,
hks
,
0
]
,
[
1
,
Math
.
floor
(
(
pred2
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
-
hks
-
hks
,
Math
.
floor
(
(
pred2
.
shape
[
2
]
-
ks
-
hks
)
/
step
)
*
step
-
hks
-
hks
,
2
]
)
)
;
result
=
opticalFlowFind
(
[
back2
,
pred2
.
slice
(
[
0
,
hks
,
hks
,
0
]
,
[
1
,
Math
.
floor
(
(
pred2
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
Math
.
floor
(
(
pred2
.
shape
[
2
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
2
]
)
,
current2
.
slice
(
[
0
,
hks
,
hks
,
0
]
,
[
1
,
Math
.
floor
(
(
current2
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
Math
.
floor
(
(
current2
.
shape
[
2
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
2
]
)
]
,
{
step
:
step
,
kernelSize
:
ks
}
)
;
px
=
result
.
shape
[
1
]
;
py
=
result
.
shape
[
0
]
;
console
.
log
(
11
)
;
result
=
custBlur
(
result
.
reshape
(
[
py
,
px
,
ks
*
ks
]
)
,
3
,
2
)
;
console
.
log
(
'back2: '
,
back2
,
result
)
;
pos
=
result
.
argMin
(
-
1
)
xp
=
pos
.
mod
(
dk
)
.
sub
(
hks
)
.
expandDims
(
-
1
)
;
yp
=
pos
.
floorDiv
(
dk
)
.
sub
(
hks
)
.
expandDims
(
-
1
)
;
out
=
tf
.
concat
(
[
xp
,
yp
]
,
-
1
)
//.add(back2.resizeBilinear(xp.shape.slice(0, 2)))
.
concat
(
[
tf
.
zeros
(
xp
.
shape
)
]
,
-
1
)
.
mul
(
27
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
;
tf
.
node
.
encodePng
(
out
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result2.png'
,
data
)
)
fld
=
flowData2
.
slice
(
[
hks
,
hks
,
0
]
,
[
Math
.
floor
(
(
flowData2
.
shape
[
0
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
Math
.
floor
(
(
flowData2
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
3
]
)
;
fld
=
fld
.
slice
(
[
hks
,
hks
,
0
]
,
[
Math
.
floor
(
(
fld
.
shape
[
0
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
Math
.
floor
(
(
fld
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
3
]
)
;
fld
=
custBlur
(
fld
,
ks
,
step
)
fld
=
custBlur
(
fld
,
3
,
2
)
tf
.
node
.
encodePng
(
fld
.
mul
(
27
)
.
mul
(
0.5
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result2f.png'
,
data
)
)
;
// 3 step
let
current
=
video
.
expandDims
(
)
.
div
(
255
)
let
pred
=
video2
.
expandDims
(
)
.
div
(
255
)
let
back3
=
tf
.
concat
(
[
xp
,
yp
]
,
-
1
)
result
=
opticalFlowFind
(
[
back3
,
pred
.
slice
(
[
0
,
hks
,
hks
,
0
]
,
[
1
,
Math
.
floor
(
(
pred
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
Math
.
floor
(
(
pred
.
shape
[
2
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
2
]
)
,
current
.
slice
(
[
0
,
hks
,
hks
,
0
]
,
[
1
,
Math
.
floor
(
(
current
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
Math
.
floor
(
(
current
.
shape
[
2
]
-
ks
-
hks
)
/
step
)
*
step
-
6
,
2
]
)
]
,
{
step
:
step
,
kernelSize
:
ks
}
)
;
px
=
result
.
shape
[
1
]
;
py
=
result
.
shape
[
0
]
;
console
.
log
(
11
)
;
result
=
custBlur
(
result
.
reshape
(
[
py
,
px
,
ks
*
ks
]
)
,
3
,
2
)
;
pos
=
result
.
reshape
(
[
result
.
shape
[
0
]
,
result
.
shape
[
1
]
,
ks
*
ks
]
)
.
argMin
(
-
1
)
xp
=
pos
.
mod
(
dk
)
.
sub
(
hks
)
.
expandDims
(
-
1
)
;
yp
=
pos
.
floorDiv
(
dk
)
.
sub
(
hks
)
.
expandDims
(
-
1
)
;
out
=
tf
.
concat
(
[
xp
,
yp
]
,
-
1
)
//.add(back3.resizeBilinear(xp.shape.slice(0, 2)))
.
concat
(
[
tf
.
zeros
(
xp
.
shape
)
]
,
-
1
)
.
mul
(
27
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
;
tf
.
node
.
encodePng
(
out
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result3.png'
,
data
)
)
fld
=
flowData
.
slice
(
[
3
,
3
,
0
]
,
[
Math
.
floor
(
(
flowData
.
shape
[
0
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
Math
.
floor
(
(
flowData
.
shape
[
1
]
-
ks
-
hks
)
/
step
)
*
step
+
hks
,
3
]
)
;
fld
=
custBlur
(
fld
,
ks
,
step
)
fld
=
custBlur
(
fld
,
3
,
2
)
tf
.
node
.
encodePng
(
fld
.
mul
(
27
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
)
.
then
(
(
data
)
=>
fs
.
writeFileSync
(
'./result3f.png'
,
data
)
)
;
//tf.browser.toPixels(out, canvas2)
//console.log('out: ', out);
console
.
log
(
'time'
,
new
Date
(
)
-
time
)
return
out
;
let
res
=
t
//modelOpticalFlow.predict(t)
let
sum
=
res
.
reshape
(
[
res
.
shape
[
1
]
,
res
.
shape
[
2
]
,
dk
*
dk
,
1
]
)
.
relu
(
)
.
pow
(
2
)
.
sum
(
-
2
)
let
p
=
res
.
reshape
(
[
res
.
shape
[
1
]
,
res
.
shape
[
2
]
,
dk
,
dk
,
1
]
)
p
=
p
.
concat
(
[
p
]
,
-
1
)
.
pow
(
2
)
.
mul
(
uv
)
.
sum
(
-
2
)
.
sum
(
-
2
)
sum
=
sum
.
concat
(
[
sum
]
,
-
1
)
let
result2
=
tf
.
concat
(
[
p
.
div
(
sum
)
.
add
(
[
y
,
x
]
)
,
tf
.
zeros
(
p
.
shape
)
]
,
-
1
)
.
mul
(
3
)
return
result2
.
mul
(
9
)
.
add
(
127
)
.
maximum
(
0
)
.
minimum
(
255
)
.
toInt
(
)
;
}
)
}
catch
(
err
)
{
console
.
log
(
'err: '
,
err
)
;
log
(
err
.
message
)
log
(
err
.
stack
)
}
// tf.browser.toPixels(result, canvas).then(() => {
// ctime = ctime * 0.9 + (new Date() - time) * 0.1
// console.log('time', ctime)
// //log('req3')
// result.dispose();
// requestAnimationFrame(update)
// }).catch(err => {
// console.log('err: ', err);
// log(err.message)
// log(err.stack)
// })
}
update
(
)
;
//requestAnimationFrame(update)
}
catch
(
err
)
{
console
.
log
(
'err: '
,
err
)
;
log
(
err
.
message
)
log
(
err
.
stack
)
}
}
run
(
)
Back
|
FazBrowse Home
|
New Git URL