UNPKG

speedy-vision

Version:

GPU-accelerated Computer Vision for JavaScript

154 lines (130 loc) 4.63 kB
<!-- 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. normalize-demo.html Image normalization 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>Normalize camera stream</title> <script src="../dist/speedy-vision.js"></script> <link href="style.css" rel="stylesheet"> </head> <body> <h1>Normalize camera stream</h1> <form autocomplete="off"> <div> Map pixels to range: &nbsp;&nbsp; </div> <div> <label for="min-value">min</label> <input type="range" id="min-value" min="0" max="255" value="0" step="1"> </div> <div> <label for="max-value">max</label> <input type="range" id="max-value" min="0" max="255" value="255" step="1"> </div> </form> <div> <span id="status"></span> <canvas id="canvas-demo"></canvas> </div> <script> window.onload = async function() { /* Our pipeline: Image ---> Convert to ---> Normalize ---> Image Source greyscale image Sink | | +-------------> Image Sink */ // get camera stream const camera = await Speedy.camera(); // setup the pipeline const pipeline = Speedy.Pipeline(); const source = Speedy.Image.Source(); const sink1 = Speedy.Image.Sink('grey'); const sink2 = Speedy.Image.Sink('norm'); const greyscale = Speedy.Filter.Greyscale(); const normalize = Speedy.Filter.Normalize(); source.output().connectTo(greyscale.input()); greyscale.output().connectTo(normalize.input()); greyscale.output().connectTo(sink1.input()); normalize.output().connectTo(sink2.input()); pipeline.init(source, sink1, sink2, greyscale, normalize); source.media = camera; normalize.minValue = 0; normalize.maxValue = 255; // Main loop (function() { let grey = null, norm = null, frameReady = false; const canvas = createCanvas(camera.width, camera.height * 2); async function update() { const result = await pipeline.run(); grey = result.grey; norm = result.norm; frameReady = true; setTimeout(update, 1000 / 60); } update(); function render() { if(frameReady) { draw(norm, canvas); draw(grey, canvas, 0, norm.height); } frameReady = false; requestAnimationFrame(render); } render(); setInterval(renderStatus, 200); })(); // setup sliders const minSlider = document.getElementById('min-value'); const maxSlider = document.getElementById('max-value'); minSlider.oninput = () => normalize.minValue = minSlider.value; maxSlider.oninput = () => normalize.maxValue = maxSlider.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 renderStatus() { const status = document.getElementById('status'); status.innerText = `FPS: ${Speedy.fps}`; } 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>