本文介绍了tf.browser.fromPixels 只返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里 tf.browser.fromPixels 返回一个只有零的张量.image.data 包含预期的值(不仅仅是零).tfjs 是使用脚本标签加载的.tfjs 1.0.0 但更改版本无济于事.

Here tf.browser.fromPixels returns a tensor with only zeros. image.data contains values as expected (not only zeros). tfjs is loaded using a script tag. tfjs 1.0.0 but changing release does not help.

有什么问题?大概是什么蠢事.我是 Tensorflow.js 的新手...

What can be wrong? Probably something stupid. I am new to Tensorflow.js...

<canvas id="canvas" width="100" height="100" style="border:1px solid #FF0000;"></canvas>
....
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var image = ctx.getImageData(0, 0, canvas.height, canvas.width);
var x = tf.browser.fromPixels(image);

推荐答案

创建后,画布的所有像素值都设置为 0.

When created, the canvas has all its pixel values set to 0.

var canvas = document.getElementById('canvas');

canvas 是一个只有 0 个值的 imageData.创建的张量的值为 0 并不奇怪,因为用于创建它的画布除了 0 值之外没有任何图像数据.

canvas is an imageData with only 0 values. It is no surprise that the tensor created has its values to 0 since the canvas used to created it does not have any image data except 0 values.

CSS 样式不会改变 canvas 的底层值.结果,即使有设置为红色的border-color,也不会反映在imageData上.

The CSS style does not change the underlying value of canvas. As a result, even if there is a border-color that is set to red, it is not reflected on the imageData.

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var image = ctx.getImageData(0, 0, canvas.height, canvas.width);
var x = tf.browser.fromPixels(image);

console.log(x.shape) // height, width, 3
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
  </head>

  <body>
  <canvas id="canvas" width="100" height="100" style="border:1px solid #FF0000;"></canvas>
  </body>
</html>

这篇关于tf.browser.fromPixels 只返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:01