(args: {
inputs: FromPixelsInputs,
backend: WebGPUBackend,
attrs: FromPixelsAttrs
})
| 32 | let willReadFrequently = env().getBool('CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU'); |
| 33 | |
| 34 | export function fromPixels(args: { |
| 35 | inputs: FromPixelsInputs, |
| 36 | backend: WebGPUBackend, |
| 37 | attrs: FromPixelsAttrs |
| 38 | }): TensorInfo { |
| 39 | const {inputs, backend, attrs} = args; |
| 40 | let {pixels} = inputs; |
| 41 | const {numChannels} = attrs; |
| 42 | |
| 43 | if (pixels == null) { |
| 44 | throw new Error('pixels passed to tf.browser.fromPixels() can not be null'); |
| 45 | } |
| 46 | |
| 47 | const isVideo = typeof (HTMLVideoElement) !== 'undefined' && |
| 48 | pixels instanceof HTMLVideoElement; |
| 49 | const isImage = typeof (HTMLImageElement) !== 'undefined' && |
| 50 | pixels instanceof HTMLImageElement; |
| 51 | const isCanvas = (typeof (HTMLCanvasElement) !== 'undefined' && |
| 52 | pixels instanceof HTMLCanvasElement) || |
| 53 | (typeof (OffscreenCanvas) !== 'undefined' && |
| 54 | pixels instanceof OffscreenCanvas); |
| 55 | const isImageBitmap = |
| 56 | typeof (ImageBitmap) !== 'undefined' && pixels instanceof ImageBitmap; |
| 57 | |
| 58 | const [width, height] = isVideo ? |
| 59 | [ |
| 60 | (pixels as HTMLVideoElement).videoWidth, |
| 61 | (pixels as HTMLVideoElement).videoHeight |
| 62 | ] : |
| 63 | [pixels.width, pixels.height]; |
| 64 | const outputShape = [height, width, numChannels]; |
| 65 | |
| 66 | const importVideo = |
| 67 | env().getBool('WEBGPU_IMPORT_EXTERNAL_TEXTURE') && isVideo; |
| 68 | const isVideoOrImage = isVideo || isImage; |
| 69 | if (isImageBitmap || isCanvas || isVideoOrImage) { |
| 70 | let resource; |
| 71 | if (importVideo) { |
| 72 | resource = backend.device.importExternalTexture( |
| 73 | {source: pixels as HTMLVideoElement}); |
| 74 | } else { |
| 75 | if (isVideoOrImage) { |
| 76 | const newWillReadFrequently = |
| 77 | env().getBool('CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU'); |
| 78 | if (fromPixels2DContext == null || |
| 79 | newWillReadFrequently !== willReadFrequently) { |
| 80 | willReadFrequently = newWillReadFrequently; |
| 81 | fromPixels2DContext = document.createElement('canvas').getContext( |
| 82 | '2d', {willReadFrequently}); |
| 83 | } |
| 84 | fromPixels2DContext.canvas.width = width; |
| 85 | fromPixels2DContext.canvas.height = height; |
| 86 | fromPixels2DContext.drawImage( |
| 87 | pixels as HTMLVideoElement | HTMLImageElement, 0, 0, width, height); |
| 88 | pixels = fromPixels2DContext.canvas; |
| 89 | } |
| 90 | |
| 91 | const usage = GPUTextureUsage.COPY_DST | |
nothing calls this directly
no test coverage detected
searching dependent graphs…