Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...