* Creates a `tf.Tensor` from an image. * * ```js * 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(); * ``` * * @param pixels The input image to construct the tensor from
(
pixels: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement|
HTMLVideoElement|ImageBitmap,
numChannels = 3)
| 64 | * @doc {heading: 'Browser', namespace: 'browser', ignoreCI: true} |
| 65 | */ |
| 66 | function fromPixels_( |
| 67 | pixels: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement| |
| 68 | HTMLVideoElement|ImageBitmap, |
| 69 | numChannels = 3): Tensor3D { |
| 70 | // Sanity checks. |
| 71 | if (numChannels > 4) { |
| 72 | throw new Error( |
| 73 | 'Cannot construct Tensor with more than 4 channels from pixels.'); |
| 74 | } |
| 75 | if (pixels == null) { |
| 76 | throw new Error('pixels passed to tf.browser.fromPixels() can not be null'); |
| 77 | } |
| 78 | let isPixelData = false; |
| 79 | let isImageData = false; |
| 80 | let isVideo = false; |
| 81 | let isImage = false; |
| 82 | let isCanvasLike = false; |
| 83 | let isImageBitmap = false; |
| 84 | if ((pixels as PixelData).data instanceof Uint8Array) { |
| 85 | isPixelData = true; |
| 86 | } else if ( |
| 87 | typeof (ImageData) !== 'undefined' && pixels instanceof ImageData) { |
| 88 | isImageData = true; |
| 89 | } else if ( |
| 90 | typeof (HTMLVideoElement) !== 'undefined' && |
| 91 | pixels instanceof HTMLVideoElement) { |
| 92 | isVideo = true; |
| 93 | } else if ( |
| 94 | typeof (HTMLImageElement) !== 'undefined' && |
| 95 | pixels instanceof HTMLImageElement) { |
| 96 | isImage = true; |
| 97 | // tslint:disable-next-line: no-any |
| 98 | } else if ((pixels as any).getContext != null) { |
| 99 | isCanvasLike = true; |
| 100 | } else if ( |
| 101 | typeof (ImageBitmap) !== 'undefined' && pixels instanceof ImageBitmap) { |
| 102 | isImageBitmap = true; |
| 103 | } else { |
| 104 | throw new Error( |
| 105 | 'pixels passed to tf.browser.fromPixels() must be either an ' + |
| 106 | `HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData ` + |
| 107 | `in browser, or OffscreenCanvas, ImageData in webworker` + |
| 108 | ` or {data: Uint32Array, width: number, height: number}, ` + |
| 109 | `but was ${(pixels as {}).constructor.name}`); |
| 110 | } |
| 111 | // If the current backend has 'FromPixels' registered, it has a more |
| 112 | // efficient way of handling pixel uploads, so we call that. |
| 113 | const kernel = getKernel(FromPixels, ENGINE.backendName); |
| 114 | if (kernel != null) { |
| 115 | const inputs: FromPixelsInputs = {pixels}; |
| 116 | const attrs: FromPixelsAttrs = {numChannels}; |
| 117 | return ENGINE.runKernel( |
| 118 | FromPixels, inputs as unknown as NamedTensorMap, |
| 119 | attrs as unknown as NamedAttrMap); |
| 120 | } |
| 121 | |
| 122 | const [width, height] = isVideo ? |
| 123 | [ |
no test coverage detected
searching dependent graphs…