@@ -20,7 +20,9 @@ import {Tensor, Tensor1D, Tensor2D, Tensor3D, Tensor4D, TensorBuffer} from '../t
20
20
import { convertToTensor , convertToTensorArray } from '../tensor_util_env' ;
21
21
import { DataType , DataTypeMap , Rank , ShapeMap , TensorLike , TensorLike4D } from '../types' ;
22
22
import * as util from '../util' ;
23
+
23
24
import { getAxesPermutation , getInnerMostAxes } from './axis_util' ;
25
+ import { fromPixels as browserFromPixels , toPixels as browserToPixels } from './browser' ;
24
26
import { concat } from './concat_split' ;
25
27
import { op } from './operation' ;
26
28
import { MPRandGauss } from './rand' ;
@@ -306,17 +308,7 @@ function oneHot_(
306
308
}
307
309
308
310
/**
309
- * Creates a `tf.Tensor` from an image.
310
- *
311
- * ```js
312
- * const image = new ImageData(1, 1);
313
- * image.data[0] = 100;
314
- * image.data[1] = 150;
315
- * image.data[2] = 200;
316
- * image.data[3] = 255;
317
- *
318
- * tf.fromPixels(image).print();
319
- * ```
311
+ * Deprecated. Use `tf.browser.fromPixels`.
320
312
*
321
313
* @param pixels The input image to construct the tensor from. The
322
314
* supported image types are all 4-channel.
@@ -328,22 +320,11 @@ function oneHot_(
328
320
function fromPixels_ (
329
321
pixels : ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement ,
330
322
numChannels = 3 ) : Tensor3D {
331
- if ( numChannels > 4 ) {
332
- throw new Error (
333
- 'Cannot construct Tensor with more than 4 channels from pixels.' ) ;
334
- }
335
- return ENV . engine . fromPixels ( pixels , numChannels ) ;
323
+ return browserFromPixels ( pixels , numChannels ) ;
336
324
}
337
325
338
326
/**
339
- * Draws a `tf.Tensor` of pixel values to a byte array or optionally a
340
- * canvas.
341
- *
342
- * When the dtype of the input is 'float32', we assume values in the range
343
- * [0-1]. Otherwise, when input is 'int32', we assume values in the range
344
- * [0-255].
345
- *
346
- * Returns a promise that resolves when the canvas has been drawn to.
327
+ * Deprecated. Use `tf.browser.toPixels`.
347
328
*
348
329
* @param img A rank-2 or rank-3 tensor. If rank-2, draws grayscale. If
349
330
* rank-3, must have depth of 1, 3 or 4. When depth of 1, draws
@@ -356,89 +337,7 @@ function fromPixels_(
356
337
async function toPixels (
357
338
img : Tensor2D | Tensor3D | TensorLike ,
358
339
canvas ?: HTMLCanvasElement ) : Promise < Uint8ClampedArray > {
359
- let $img = convertToTensor ( img , 'img' , 'toPixels' ) ;
360
- if ( ! ( img instanceof Tensor ) ) {
361
- // Assume int32 if user passed a native array.
362
- $img = $img . toInt ( ) ;
363
- }
364
- if ( $img . rank !== 2 && $img . rank !== 3 ) {
365
- throw new Error (
366
- `toPixels only supports rank 2 or 3 tensors, got rank ${ $img . rank } .` ) ;
367
- }
368
- const [ height , width ] = $img . shape . slice ( 0 , 2 ) ;
369
- const depth = $img . rank === 2 ? 1 : $img . shape [ 2 ] ;
370
-
371
- if ( depth > 4 || depth === 2 ) {
372
- throw new Error (
373
- `toPixels only supports depth of size ` +
374
- `1, 3 or 4 but got ${ depth } ` ) ;
375
- }
376
-
377
- const minTensor = $img . min ( ) ;
378
- const maxTensor = $img . max ( ) ;
379
- const min = ( await minTensor . data ( ) ) [ 0 ] ;
380
- const max = ( await maxTensor . data ( ) ) [ 0 ] ;
381
- minTensor . dispose ( ) ;
382
- maxTensor . dispose ( ) ;
383
- if ( $img . dtype === 'float32' ) {
384
- if ( min < 0 || max > 1 ) {
385
- throw new Error (
386
- `Tensor values for a float32 Tensor must be in the ` +
387
- `range [0 - 1] but got range [${ min } - ${ max } ].` ) ;
388
- }
389
- } else if ( $img . dtype === 'int32' ) {
390
- if ( min < 0 || max > 255 ) {
391
- throw new Error (
392
- `Tensor values for a int32 Tensor must be in the ` +
393
- `range [0 - 255] but got range [${ min } - ${ max } ].` ) ;
394
- }
395
- } else {
396
- throw new Error (
397
- `Unsupported type for toPixels: ${ $img . dtype } .` +
398
- ` Please use float32 or int32 tensors.` ) ;
399
- }
400
-
401
- const data = await $img . data ( ) ;
402
- const multiplier = $img . dtype === 'float32' ? 255 : 1 ;
403
- const bytes = new Uint8ClampedArray ( width * height * 4 ) ;
404
-
405
- for ( let i = 0 ; i < height * width ; ++ i ) {
406
- let r , g , b , a ;
407
- if ( depth === 1 ) {
408
- r = data [ i ] * multiplier ;
409
- g = data [ i ] * multiplier ;
410
- b = data [ i ] * multiplier ;
411
- a = 255 ;
412
- } else if ( depth === 3 ) {
413
- r = data [ i * 3 ] * multiplier ;
414
- g = data [ i * 3 + 1 ] * multiplier ;
415
- b = data [ i * 3 + 2 ] * multiplier ;
416
- a = 255 ;
417
- } else if ( depth === 4 ) {
418
- r = data [ i * 4 ] * multiplier ;
419
- g = data [ i * 4 + 1 ] * multiplier ;
420
- b = data [ i * 4 + 2 ] * multiplier ;
421
- a = data [ i * 4 + 3 ] * multiplier ;
422
- }
423
-
424
- const j = i * 4 ;
425
- bytes [ j + 0 ] = Math . round ( r ) ;
426
- bytes [ j + 1 ] = Math . round ( g ) ;
427
- bytes [ j + 2 ] = Math . round ( b ) ;
428
- bytes [ j + 3 ] = Math . round ( a ) ;
429
- }
430
-
431
- if ( canvas != null ) {
432
- canvas . width = width ;
433
- canvas . height = height ;
434
- const ctx = canvas . getContext ( '2d' ) ;
435
- const imageData = new ImageData ( bytes , width , height ) ;
436
- ctx . putImageData ( imageData , 0 , 0 ) ;
437
- }
438
- if ( $img !== img ) {
439
- $img . dispose ( ) ;
440
- }
441
- return bytes ;
340
+ return browserToPixels ( img , canvas ) ;
442
341
}
443
342
444
343
/**
@@ -1210,7 +1109,7 @@ export const cumsum = op({cumsum_});
1210
1109
export const depthToSpace = op ( { depthToSpace_} ) ;
1211
1110
export const expandDims = op ( { expandDims_} ) ;
1212
1111
export const eye = op ( { eye_} ) ;
1213
- export const fromPixels = op ( { fromPixels_} ) ;
1112
+ export const fromPixels = fromPixels_ ;
1214
1113
export const multinomial = op ( { multinomial_} ) ;
1215
1114
export const oneHot = op ( { oneHot_} ) ;
1216
1115
export const pad = op ( { pad_} ) ;
0 commit comments