I've noticed a performance problem that many people are writing a <video>
image to the <canvas>
and then reading the pixels from the <canvas>
and then rewriting the data back to the <canvas>
to do real time image processing in HTML5/JavaScript.
Obviously the correct way would be to read the <video>
image data and then process the pixels in javascript and only write to the <canvas>
once.
It appears that the getUserMedia <video>
API doesn't support direct access to the image data even on an event, perhaps for some performance reason??!? Does this require a write to the canvas if you want to read the actual pixel information? I don't mind if the data is compresses it would be nice to have it in some form. I did notice that there is a reference to a video.src however it appears that getUserMedia doesn't include a video.data thats usable.
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
Here is some example code that works however I would like to speed it up by not writing to the <canvas>
and read directly from the <video>
data:
function DisplayCanvas (video) {
var delay = 1500; // ms
timer = setInterval(
function () {
// display is a canvas context
display.drawImage(video, 0, 0, 640, 480);
data = display.getImageData(0,0,640,480);
idata = ImageProcessing(data);
display.putImageData(idata,0,0);
}, delay);
}
Is there anyway to simplify drawImage() first problem to just one line with.
// the problem is that there isn't a video.data
display.putImageData(ImageProcessing(video.data),0,0);