MCPcopy Create free account
hub / github.com/tensorflow/tfjs / fromPixelsAsync

Function fromPixelsAsync

tfjs-core/src/ops/browser.ts:227–270  ·  view source on GitHub ↗
(
    pixels: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement|
    HTMLVideoElement|ImageBitmap,
    numChannels = 3)

Source from the content-addressed store, hash-verified

225 * @doc {heading: 'Browser', namespace: 'browser', ignoreCI: true}
226 */
227export async function fromPixelsAsync(
228 pixels: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement|
229 HTMLVideoElement|ImageBitmap,
230 numChannels = 3) {
231 let inputs: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement|
232 HTMLVideoElement|ImageBitmap = null;
233
234 // Check whether the backend needs to wrap |pixels| to imageBitmap and
235 // whether |pixels| can be wrapped to imageBitmap.
236 if (env().getBool('WRAP_TO_IMAGEBITMAP') &&
237 canWrapPixelsToImageBitmap(pixels)) {
238 // Force the imageBitmap creation to not do any premultiply alpha
239 // ops.
240 let imageBitmap;
241
242 try {
243 // wrap in try-catch block, because createImageBitmap may not work
244 // properly in some browsers, e.g.
245 // https://bugzilla.mozilla.org/show_bug.cgi?id=1335594
246 // tslint:disable-next-line: no-any
247 imageBitmap = await (createImageBitmap as any)(
248 pixels as ImageBitmapSource, {premultiplyAlpha: 'none'});
249 } catch (e) {
250 imageBitmap = null;
251 }
252
253 // createImageBitmap will clip the source size.
254 // In some cases, the input will have larger size than its content.
255 // E.g. new Image(10, 10) but with 1 x 1 content. Using
256 // createImageBitmap will clip the size from 10 x 10 to 1 x 1, which
257 // is not correct. We should avoid wrapping such resouce to
258 // imageBitmap.
259 if (imageBitmap != null && imageBitmap.width === pixels.width &&
260 imageBitmap.height === pixels.height) {
261 inputs = imageBitmap;
262 } else {
263 inputs = pixels;
264 }
265 } else {
266 inputs = pixels;
267 }
268
269 return fromPixels_(inputs, numChannels);
270}
271
272function validateImgTensor(img: Tensor2D|Tensor3D) {
273 if (img.rank !== 2 && img.rank !== 3) {

Callers

nothing calls this directly

Calls 4

envFunction · 0.90
fromPixels_Function · 0.85
getBoolMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…