UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

124 lines (123 loc) 5.36 kB
/** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ /// <amd-module name="@tensorflow/tfjs-core/dist/ops/browser" /> import { Tensor2D, Tensor3D } from '../tensor'; import { DrawOptions, PixelData, TensorLike } from '../types'; /** * 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. 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}` * @param numChannels 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 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 * * @doc {heading: 'Browser', namespace: 'browser', ignoreCI: true} */ declare function fromPixels_(pixels: PixelData | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap, numChannels?: number): Tensor3D; /** * Creates a `tf.Tensor` from an image in async way. * * ```js * const image = new ImageData(1, 1); * image.data[0] = 100; * image.data[1] = 150; * image.data[2] = 200; * image.data[3] = 255; * * (await tf.browser.fromPixelsAsync(image)).print(); * ``` * This API is the async version of fromPixels. The API will first * check |WRAP_TO_IMAGEBITMAP| flag, and try to wrap the input to * imageBitmap if the flag is set to true. * * @param pixels 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}` * @param numChannels 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). * * @doc {heading: 'Browser', namespace: 'browser', ignoreCI: true} */ export declare function fromPixelsAsync(pixels: PixelData | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap, numChannels?: number): Promise<Tensor3D>; /** * Draws a `tf.Tensor` of pixel values to a byte array or optionally a * canvas. * * When the dtype of the input is 'float32', we assume values in the range * [0-1]. Otherwise, when input is 'int32', we assume values in the range * [0-255]. * * Returns a promise that resolves when the canvas has been drawn to. * * @param img A rank-2 tensor with shape `[height, width]`, or a rank-3 tensor * of shape `[height, width, numChannels]`. If rank-2, draws grayscale. If * rank-3, must have depth of 1, 3 or 4. When depth of 1, draws * grayscale. When depth of 3, we draw with the first three components of * the depth dimension corresponding to r, g, b and alpha = 1. When depth of * 4, all four components of the depth dimension correspond to r, g, b, a. * @param canvas The canvas to draw to. * * @doc {heading: 'Browser', namespace: 'browser'} */ export declare function toPixels(img: Tensor2D | Tensor3D | TensorLike, canvas?: HTMLCanvasElement): Promise<Uint8ClampedArray>; /** * Draws a `tf.Tensor` to a canvas. * * When the dtype of the input is 'float32', we assume values in the range * [0-1]. Otherwise, when input is 'int32', we assume values in the range * [0-255]. * * @param image The tensor to draw on the canvas. Must match one of * these shapes: * - Rank-2 with shape `[height, width`]: Drawn as grayscale. * - Rank-3 with shape `[height, width, 1]`: Drawn as grayscale. * - Rank-3 with shape `[height, width, 3]`: Drawn as RGB with alpha set in * `imageOptions` (defaults to 1, which is opaque). * - Rank-3 with shape `[height, width, 4]`: Drawn as RGBA. * @param canvas The canvas to draw to. * @param options The configuration arguments for image to be drawn and the * canvas to draw to. * * @doc {heading: 'Browser', namespace: 'browser'} */ export declare function draw(image: Tensor2D | Tensor3D | TensorLike, canvas: HTMLCanvasElement, options?: DrawOptions): void; export declare const fromPixels: typeof fromPixels_; export {};