speedy-vision
Version:
GPU-accelerated Computer Vision for JavaScript
93 lines (74 loc) • 2.93 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.
greyscale-image.html
Convert an image to greyscale
-->
<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>Convert image to greyscale</title>
<script src="../dist/speedy-vision.js"></script>
<link href="style.css" rel="stylesheet">
</head>
<body>
<h1>Convert image to greyscale</h1>
<img src="../assets/speedy-wall.jpg" title="Image by Bride of Frankenstein (CC-BY)">
<script>
window.onload = async function()
{
/*
This is our pipeline:
Image ---> Convert to ---> Image
Source greyscale Sink
*/
// Load an image
const img = document.querySelector('img');
const media = await Speedy.load(img);
// Setup the pipeline
const pipeline = Speedy.Pipeline(); // create the pipeline and the nodes
const source = Speedy.Image.Source();
const sink = Speedy.Image.Sink();
const greyscale = Speedy.Filter.Greyscale();
source.media = media; // set the media source
source.output().connectTo(greyscale.input()); // connect the nodes
greyscale.output().connectTo(sink.input());
pipeline.init(source, sink, greyscale); // add the nodes to the pipeline
// Run the pipeline
const { image } = await pipeline.run(); // image is a SpeedyMedia
// Display the result
const canvas = createCanvas(image.width, image.height, img.title);
draw(image, canvas);
}
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 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>