You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, thank you for this amazing work that allows people not too familiar with GPU architectures to start playing with it anyway !
I would like to stream the video from a local camera (getUserMedia) to a texture that would be then processed by a ti.kernel (filter effects, image tracking etc.) and rendered to the canvas.
Here is a minimum working example of what I managed to achieve using ti.Texture.createFromHtmlImage() (here the video processing is just a simple edge detection kernel):
// CREATE THE CANVASconstcanvas=document.createElement('canvas');document.body.appendChild(canvas);canvas.width=canvas.clientWidth;canvas.height=canvas.clientWidth;// GRAB THE VIDEO OBJECTvarvideoWidth=640;varvideoHeight=480;// some default valuesvarvideoReady=false;varvideo=null;asyncfunctionsetupVideo(){if(video==null){video=document.createElement("video");letstream=awaitnavigator.mediaDevices.getUserMedia({video: {width: videoWidth,height: videoHeight},audio: false,});video.srcObject=stream;video.onloadedmetadata=asyncfunction(e){video.play();videoReady=true;};}}setupVideo();// MAIN FUNCTIONletmain=async()=>{awaitti.init();letticanvas=newti.Canvas(canvas);// Declare an empty video textureletvideoTexture=awaitti.texture(4,[videoWidth,videoHeight]);letoutputTexture=awaitti.texture(4,[videoWidth,videoHeight]);// Kernel scopeti.addToKernelScope({
videoWidth,
videoHeight,
videoTexture,
outputTexture,});// Kernel functionletprocessVideo=ti.kernel(()=>{for(letIofti.ndrange(videoWidth,videoHeight)){letGx=ti.textureLoad(videoTexture,I+[1,0])-ti.textureLoad(videoTexture,I+[-1,0])letGy=ti.textureLoad(videoTexture,I+[0,1])-ti.textureLoad(videoTexture,I+[0,-1])letg=ti.sqrt(Gx**2+Gy**2)ti.textureStore(outputTexture,I,g);}});// Frame callbackasyncfunctionframe(){requestAnimationFrame(frame);if(videoReady){// Copy the texture from the video object to the ti.canvaslettex=awaitti.Texture.createFromHtmlImage(video);videoTexture.copyFrom(tex);// Apply the kernel functionprocessVideo();// Push the result into the canvasawaitticanvas.setImage(outputTexture);}elsereturn;}awaitframe();}main();
It works quite OK most of the times (sometimes it freezes the page..) but having to recreate a texture each frame does not sound ideal to me (maybe I am wrong). Indeed, from this tutorial or even this one it seems that there is more efficient ways to transfer the video stream to a texture, using for example device.queue.copyExternalImageToTexture. I see that this function is used in taichi.js to effectively upload bitmaps into textures (in Runtime.js) but I could not extract that part as I did not find any way to access the Runtime.device from any ti object.. Or maybe this is not the right way to achieve it ?
The text was updated successfully, but these errors were encountered:
Hello,
First, thank you for this amazing work that allows people not too familiar with GPU architectures to start playing with it anyway !
I would like to stream the video from a local camera (getUserMedia) to a texture that would be then processed by a ti.kernel (filter effects, image tracking etc.) and rendered to the canvas.
Here is a minimum working example of what I managed to achieve using
ti.Texture.createFromHtmlImage()
(here the video processing is just a simple edge detection kernel):It works quite OK most of the times (sometimes it freezes the page..) but having to recreate a texture each frame does not sound ideal to me (maybe I am wrong). Indeed, from this tutorial or even this one it seems that there is more efficient ways to transfer the video stream to a texture, using for example
device.queue.copyExternalImageToTexture
. I see that this function is used intaichi.js
to effectively upload bitmaps into textures (inRuntime.js
) but I could not extract that part as I did not find any way to access theRuntime.device
from any ti object.. Or maybe this is not the right way to achieve it ?The text was updated successfully, but these errors were encountered: