Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

tensorflow.js - How to transform a webcam image to grayscale using TensorflowJS?

I am trying to use a model to classify grayscale images.

I used the Webcam class provided in some of the TensorflowJS examples to get an image using the webcam. It worked well, but it returns an RGB image.

What is the best way to transform this image to grayscale? Do I need to compute this getting each element from the tensor and compute a new matrix that represents the grayscale image or is there a more simple or convenient way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you need to have it in the form [width, height, 1]:

tf.browser.fromPixels(image)
    .mean(2)
    .toFloat()
    .expandDims(-1)

If you need in the form [1, width, height, 1]:

tf.browser.fromPixels(image)
    .mean(2)
    .toFloat()
    .expandDims(0)
    .expandDims(-1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...