MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / tensorToCanvas

Function tensorToCanvas

custom-layer/index.js:36–54  ·  view source on GitHub ↗
(tensor, canvas)

Source from the content-addressed store, hash-verified

34}
35
36function tensorToCanvas(tensor, canvas) {
37 const ctx = canvas.getContext('2d');
38 const [batch, height, width, nChan] = tensor.shape;
39 console.assert(nChan == 3);
40 console.assert(batch == 1);
41 canvas.width = width;
42 canvas.height = height;
43 const imageData = new ImageData(width, height);
44 const data = tensor.dataSync();
45 for (let i = 0; i < height * width; ++i) {
46 const i4 = i * 4;
47 const i3 = i * 3;
48 imageData.data[i4 + 0] = data[i3 + 0] * 2;
49 imageData.data[i4 + 1] = data[i3 + 1] * 2;
50 imageData.data[i4 + 2] = data[i3 + 2] * 2;
51 imageData.data[i4 + 3] = 255;
52 }
53 ctx.putImageData(imageData, 0, 0);
54};
55
56customLayerDemo();

Callers 1

customLayerDemoFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected