當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.browser.fromPixelsAsync()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

tf.browser.fromPixelsAsync()函數用於以異步方式創建指定圖像的像素值的張量。

用法:

tf.browser.fromPixelsAsync (pixels, numChannels)

參數:該函數接受兩個參數,如下所示。

  • pixels:它是要從中構造張量的輸入圖像的像素。支持的圖像類型均為4通道。
  • numchannels:它是輸出張量的通道數。默認值為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 pixles value as
// the default value of numchannels 
// parameter is 3
(await tf.browser.fromPixelsAsync(image)).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

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Tensorflow.js tf.browser.fromPixelsAsync() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。