📜  Tensorflow.js tf.browser.fromPixelsAsync()函数

📅  最后修改于: 2022-05-13 01:56:35.072000             🧑  作者: Mango

Tensorflow.js tf.browser.fromPixelsAsync()函数

Tensorflow.js是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

tf.browser.fromPixelsAsync()函数用于以异步方式创建指定图像的像素值的张量。

句法:

tf.browser.fromPixelsAsync (pixels, numChannels)

参数:此函数接受两个参数,如下所示。

  • 像素:将构建张量的输入图像的像素。支持的图像类型都是 4 通道。
  • numchannels:输出Tensor的通道数。默认值为 3,上限为 4。

返回值:此函数返回指定图像的像素值的创建张量。

示例 1:

Javascript
// Creating a image from some specified
// pixels values
const image = new ImageData(2, 2);
image.data[0] = 5;
image.data[1] = 10;
image.data[2] = 15;
image.data[3] = 20;
 
// Calling the .fromPixelsAsync() function
// over the above image as its parameter
// without using numChannels value so
// it print only 3 pixels value as
// the default value of numchannels
// parameter is 3
(await tf.browser.fromPixelsAsync(image)).print();


Javascript
// Creating a image from some specified
// pixels values
const image = new ImageData(1, 1);
image.data[0] = 5;
image.data[1] = 10;
image.data[2] = 15;
image.data[3] = 20;
 
// Calling the .fromPixelsAsync() function
// over the above image as its parameter
// along with 4 value for numChannels parameter
(await tf.browser.fromPixelsAsync(image, 4)).print();


输出:

Tensor
   [[[5, 10, 15],
     [0, 0 , 0 ]],

    [[0, 0 , 0 ],
     [0, 0 , 0 ]]]

示例 2:

Javascript

// Creating a image from some specified
// pixels values
const image = new ImageData(1, 1);
image.data[0] = 5;
image.data[1] = 10;
image.data[2] = 15;
image.data[3] = 20;
 
// Calling the .fromPixelsAsync() function
// over the above image as its parameter
// along with 4 value for numChannels parameter
(await tf.browser.fromPixelsAsync(image, 4)).print();

输出:

Tensor
    [ [[5, 10, 15, 20],]]

参考: https://js.tensorflow.org/api/latest/#browser.fromPixelsAsync