Open In App

Tensorflow.js tf.data.webcam() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.data.webcam() function is used for creating an iterator which generates Tensors from webcam video stream.



Note: It only works in Browser environment when the device contains webcam.

Syntax:



tf.data.webcam(webcamVideoElement?, webcamConfig?)

Parameters:

Return Value: It returns a Promise.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Webcam promise return
let camera = await tf.data.webcam(document.createElement('video'));
  
// Image camera capture promise return.
let image = await camera.capture();
  
// Printing the result.
image.print();
  
// Stopping the camera.
camera.stop();

Output:

Tensor
    [[[168, 176, 185],
      [164, 171, 180],
      [161, 169, 178],
      ...,
      [43 , 91 , 74 ],
      [37 , 90 , 70 ],
      [41 , 94 , 74 ]],

     [[165, 172, 181],
      [166, 174, 182],
      [164, 171, 180],
      ...,
      [45 , 93 , 77 ],
      [38 , 92 , 71 ],
      [41 , 94 , 74 ]],

     [[152, 174, 178],
      [156, 178, 182],
      [158, 178, 182],
      ...,
      [60 , 89 , 79 ],
      [56 , 90 , 74 ],
      [55 , 89 , 73 ]],

     ...
     [[150, 174, 98 ],
      [147, 171, 95 ],
      [136, 168, 87 ],
      ...,
      [13 , 27 , 2  ],
      [21 , 29 , 8  ],
      [21 , 29 , 8  ]],

     [[134, 170, 92 ],
      [138, 173, 96 ],
      [130, 169, 84 ],
      ...,
      [16 , 33 , 0  ],
      [22 , 33 , 7  ],
      [21 , 32 , 6  ]],

     [[132, 167, 90 ],
      [130, 165, 88 ],
      [128, 167, 82 ],
      ...,
      [19 , 36 , 3  ],
      [24 , 35 , 9  ],
      [22 , 33 , 7  ]]]

Reference:https://js.tensorflow.org/api/latest/#data.webcam

Article Tags :