-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
optical-flow.html
315 lines (274 loc) · 11 KB
/
optical-flow.html
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!--
speedy-vision.js
GPU-accelerated Computer Vision for JavaScript
Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
optical-flow.html
Feature tracking demo
-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
<meta name="author" content="Alexandre Martins">
<title>Optical flow - feature tracking</title>
<link href="demo-base.css" rel="stylesheet">
<script src="demo-base.js"></script>
<script src="../dist/speedy-vision.js"></script>
</head>
<body>
<h1>LK optical flow</h1>
<p><em>Click on a <strong>moving region</strong> of the video to track it!</em></p>
<form autocomplete="off">
<div>
<label for="window-size">Window size</label>
<select id="window-size">
<option value="5">5x5</option>
<option value="7">7x7</option>
<option value="9">9x9</option>
<option value="11" selected>11x11</option>
<option value="13">13x13</option>
<option value="15">15x15</option>
<option value="21">21x21</option>
</select>
<label for="levels">Pyramid levels</label>
<select id="levels">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<label for="discard-threshold">Discard threshold</label>
<select id="discard-threshold">
<option value="0.0">0.0</option>
<option value="0.0001" selected>0.0001</option>
<option value="0.001">0.001</option>
<option value="0.01">0.01</option>
</select>
</div>
<div class="separator"></div>
<div>
<label for="number-of-iterations">Max. iterations</label>
<select id="number-of-iterations">
<option value="3">3</option>
<option value="5">5</option>
<option value="8">8</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="30" selected>30</option>
</select>
<label for="epsilon">Epsilon for early termination</label>
<select id="epsilon">
<option value="0">0</option>
<option value="0.01" selected>0.01</option>
<option value="0.03">0.03</option>
</select>
<span>
<a href="javascript:reset()">Reset values</a>
</span>
</div>
<div class="separator"></div>
<div>
<label for="speed-slider">Video speed</label>
<input type="range" id="speed-slider" min="0.10" max="2" value="1" step="0.01">
</div>
</form>
<div>
<span id="status"></span>
<canvas id="canvas-demo" style="cursor:pointer"></canvas>
</div>
<div>
<button id="play">Play / pause</button>
<button id="detect">Detect features</button>
<button id="clear">Clear features</button>
</div>
<video id="video"
poster="../assets/loading.jpg"
width="640" height="360"
preload="auto"
muted hidden playsinline
title="Free to use video by Free Videos, https://www.pexels.com/pt-br/foto/853889/">
<source src="../assets/people.webm" type="video/webm" />
<source src="../assets/people.mp4" type="video/mp4" />
</video>
<script>
let userPoints = [];
window.onload = async function()
{
// get DOM elements
const windowSize = document.getElementById('window-size');
const discardThreshold = document.getElementById('discard-threshold');
const levels = document.getElementById('levels');
const numberOfIterations = document.getElementById('number-of-iterations');
const epsilon = document.getElementById('epsilon');
// load the video
const video = document.getElementById('video');
const media = await Speedy.load(video);
video.play();
// create the pipeline
const pipeline = Speedy.Pipeline();
const imgsrc = Speedy.Image.Source();
const kpsrc = Speedy.Keypoint.Source();
const grey = Speedy.Filter.Greyscale();
const pyr = Speedy.Image.Pyramid();
const harris = Speedy.Keypoint.Detector.Harris();
const clipper = Speedy.Keypoint.Clipper();
const buf = Speedy.Image.Buffer();
const bufpyr = Speedy.Image.Pyramid();
const lk = Speedy.Keypoint.Tracker.LK();
const mixer = Speedy.Keypoint.Mixer();
const sink = Speedy.Keypoint.SinkOfTrackedKeypoints();
imgsrc.media = media;
harris.quality = 0.10;
harris.capacity = 0;
imgsrc.output().connectTo(grey.input());
grey.output().connectTo(pyr.input());
grey.output().connectTo(buf.input());
buf.output().connectTo(bufpyr.input());
bufpyr.output().connectTo(harris.input());
harris.output().connectTo(clipper.input());
clipper.output().connectTo(mixer.input('in1'));
kpsrc.output().connectTo(mixer.input('in0'));
bufpyr.output().connectTo(lk.input('previousImage'));
pyr.output().connectTo(lk.input('nextImage'));
mixer.output().connectTo(lk.input('previousKeypoints'));
lk.output().connectTo(sink.input());
lk.output('flow').connectTo(sink.input('flow'));
pipeline.init(imgsrc, grey, pyr, harris, kpsrc, clipper, buf, bufpyr, lk, mixer, sink);
// Main loop
let detect = false, clear = false;
(function() {
const canvas = addMouseListener(setupCanvas('canvas-demo', media.width, media.height, video.title));
let keypoints = [], frameReady = false;
async function update()
{
// prevent rendering before running the pipeline
frameReady = false;
// find new keypoints
harris.capacity = 0;
if(detect) {
if(keypoints.length < 400)
harris.capacity = 2048;
detect = false;
}
// handle new points added by the user
if(userPoints.length > 0) {
keypoints.push(...userPoints);
userPoints.length = 0;
}
// clear all keypoints
if(clear) {
keypoints.length = userPoints.length = 0;
clear = false;
}
// update parameters
kpsrc.keypoints = keypoints;
lk.windowSize = Speedy.Size(Number(windowSize.value), Number(windowSize.value));
lk.levels = Number(levels.value);
lk.discardThreshold = Number(discardThreshold.value);
lk.numberOfIterations = Number(numberOfIterations.value);
lk.epsilon = Number(epsilon.value);
clipper.size = 200;
// run the pipeline
const result = await pipeline.run();
keypoints = result.keypoints;
// repeat
frameReady = true;
setTimeout(update, 1000 / 60);
}
update();
function render()
{
if(frameReady) {
renderMedia(media, canvas);
renderFlowVectors(canvas, keypoints, '#f22', 3);
renderKeypoints(canvas, keypoints, '#f22');
}
frameReady = false;
requestAnimationFrame(render);
}
render();
setInterval(() => renderStatus(keypoints), 200);
})();
// play/pause
const playButton = document.getElementById('play');
playButton.onclick = () => video.paused ? video.play() : video.pause();
// video speed
const speedSlider = document.getElementById('speed-slider');
speedSlider.oninput = () => video.playbackRate = speedSlider.value;
// detect features
const detectButton = document.getElementById('detect');
detectButton.onclick = () => detect = true;
// clear features
const clearButton = document.getElementById('clear');
clearButton.onclick = () => clear = true;
// restart the video and clear the features
video.onended = () => {
clear = true;
video.currentTime = 0.2;
video.play();
};
}
function renderFlowVectors(canvas, features, color = 'red', length = 1)
{
const context = canvas.getContext('2d');
context.beginPath();
for(let i = 0; i < features.length; i++) {
const feature = features[i];
const flow = feature.flow;
// draw flow vector
context.moveTo(feature.x - flow.x * length, feature.y - flow.y * length);
context.lineTo(feature.x, feature.y);
}
context.strokeStyle = color;
context.lineWidth = 2;
context.stroke();
}
function addMouseListener(canvas)
{
canvas.addEventListener('mousedown', ev => {
const position = cursorPosition(canvas, ev);
userPoints.push({ position });
});
return canvas;
}
function cursorPosition(canvas, event)
{
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
return { x, y };
}
function reset()
{
const options = document.querySelectorAll('select option');
for(const option of options)
option.selected = option.defaultSelected;
const slider = document.getElementById('speed-slider');
slider.value = slider.defaultValue;
slider.dispatchEvent(new Event('input'));
}
</script>
<section id="speedy-vision">
<span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
<a href="https://github.com/sponsors/alemart" target="_blank" class="button">
<img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
</a>
</section>
</body>
</html>