speedy-vision
Version:
GPU-accelerated Computer Vision for JavaScript
202 lines (172 loc) • 6.52 kB
HTML
<!--
speedy-vision.js
GPU-accelerated Computer Vision for JavaScript
Copyright 2020-2022 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.
video-features.html
Feature detection in a video
-->
<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>Speedy feature detection in a video</title>
<script src="../dist/speedy-vision.js"></script>
<link href="style.css" rel="stylesheet">
</head>
<body>
<h1>Gotta go FAST!!!</h1>
<form autocomplete="off">
<div>
<label for="detection-method">Method</label>
<select id="detection-method">
<option value="fast" selected>FAST</option>
<option value="harris">Harris</option>
</select>
</div>
<div class="separator"></div>
<div>
<label for="sensitivity">Sensitivity</label>
<input type="range" min="0.0" max="0.99" value="0.50" step="0.01" id="sensitivity">
</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"></canvas>
</div>
<div>
<button id="play">Play / pause</button>
</div>
<video
src="../assets/roadrunner.webm"
poster="../assets/loading.jpg"
width="480" height="270"
preload="auto"
loop muted hidden
title="Free video by Vimeo-Free-Videos from https://pixabay.com/pt/videos/estrada-condu%C3%A7%C3%A3o-auto-estrada-1101/">
</video>
<script>
window.onload = async function()
{
// form elements
const detectionMethod = document.getElementById('detection-method');
const sensitivity = document.getElementById('sensitivity');
const playButton = document.getElementById('play');
const speedSlider = document.getElementById('speed-slider');
// load the video
const video = document.querySelector('video');
const media = await Speedy.load(video);
video.play();
// create the pipelines
function createPipelineWith(detector)
{
const pipeline = Speedy.Pipeline();
const source = Speedy.Image.Source();
const greyscale = Speedy.Filter.Greyscale();
const sink = Speedy.Keypoint.Sink();
source.media = media;
sink.turbo = true;
source.output().connectTo(greyscale.input());
greyscale.output().connectTo(detector.input());
detector.output().connectTo(sink.input());
pipeline.init(source, greyscale, detector, sink);
return pipeline;
}
const fast = Speedy.Keypoint.Detector.FAST();
const harris = Speedy.Keypoint.Detector.Harris();
const pipelines = {
fast: createPipelineWith(fast),
harris: createPipelineWith(harris)
};
// Main loop
(function() {
const canvas = createCanvas(media.width, media.height, video.title);
let keypoints = [], frameReady = false;
async function update()
{
// pick a pipeline
const pipeline = pipelines[detectionMethod.value];
// adjust the sensitivity
const x = 1.0 - Number(sensitivity.value);
fast.threshold = 255 * x + 2;
harris.quality = x;
// find the features
const result = await pipeline.run();
keypoints = result.keypoints;
// repeat
frameReady = true;
setTimeout(update, 1000 / 60);
}
update();
function render()
{
if(frameReady) {
draw(media, canvas);
renderFeatures(canvas, keypoints, 2, '#ff0', 2);
}
frameReady = false;
requestAnimationFrame(render);
}
render();
setInterval(() => renderStatus(keypoints), 200);
})();
// misc
playButton.onclick = () => video.paused ? video.play() : video.pause();
speedSlider.oninput = () => video.playbackRate = speedSlider.value;
}
function createCanvas(width, height, title)
{
const canvas = document.getElementById('canvas-demo') || document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.title = title;
if(!document.body.contains(canvas))
document.body.appendChild(canvas);
return canvas;
}
function renderFeatures(canvas, features, size = 2, color = 'yellow')
{
const context = canvas.getContext('2d');
context.beginPath();
for(let feature of features) {
let radius = size * feature.scale;
// draw scaled circle
context.moveTo(feature.x + radius, feature.y);
context.arc(feature.x, feature.y, radius, 0, Math.PI * 2.0);
// draw rotation line
const sin = Math.sin(feature.rotation);
const cos = Math.cos(feature.rotation);
context.moveTo(feature.x, feature.y);
context.lineTo(feature.x + radius * cos, feature.y + radius * sin);
}
context.strokeStyle = color;
context.stroke();
}
function renderStatus(features)
{
const status = document.getElementById('status');
status.innerText = `FPS: ${Speedy.fps} | Keypoints: ${features.length}`;
}
function draw(media, canvas, x = 0, y = 0, width = media.width, height = media.height)
{
const ctx = canvas.getContext('2d');
ctx.drawImage(media.source, x, y, width, height);
}
</script>
<mark>Powered by <a href="https://github.com/alemart/speedy-vision">speedy-vision.js</a></mark>
</body>
</html>