• Creates a tf.Tensor from an image.

    const image = new ImageData(1, 1);
    image.data[0] = 100;
    image.data[1] = 150;
    image.data[2] = 200;
    image.data[3] = 255;

    tf.browser.fromPixels(image).print();

    Parameters

    • pixels:
          | HTMLCanvasElement
          | HTMLImageElement
          | HTMLVideoElement
          | ImageData
          | ImageBitmap
          | PixelData

      The input image to construct the tensor from. The supported image types are all 4-channel. You can also pass in an image object with following attributes: {data: Uint8Array; width: number; height: number}

    • OptionalnumChannels: number

      The number of channels of the output tensor. A numChannels value less than 4 allows you to ignore channels. Defaults to 3 (ignores alpha channel of input image).

    Returns Tensor3D

    A Tensor3D with the shape [height, width, numChannels].

    Note: fromPixels can be lossy in some cases, same image may result in slightly different tensor values, if rendered by different rendering engines. This means that results from different browsers, or even same browser with CPU and GPU rendering engines can be different. See discussion in details: https://github.com/tensorflow/tfjs/issues/5482