javascript - 无法裁剪图像,错误为 : failed to compile fragment shader Tensorflow. js

标签 javascript reactjs react-native tensorflow tensorflow.js

我试图根据边界框从 BlazeFaceModel 中裁剪脸部,但出现以下错误。它在我的控制台中抛出一个巨大的错误,其中有很多看起来像 C++ 的代码。我以前没有见过这个问题,并且不知道如何解决它。

    const tensorDims = { height: 224, width: 224, depth: 3 };
    const returnTensors = true;
    const faces = await bfModel
      .estimateFaces(tensor, returnTensors)
      .catch(e => console.log(e));
    const tensorReshaped = tensor.reshape([1, 224, 224, 3]);
    const scale = {
      height: styles.camera.height / tensorDims.height,
      width: styles.camera.width / tensorDims.width
    };

    // Faces is an array of objects
    if (!isEmpty(faces)) {
      setModelFaces({ faces });
      faces.map((face, i) => {
        const { topLeft, bottomRight } = face;
        const boxes = tf.concat([topLeft, bottomRight]).reshape([-1, 4]);
        const width =
          (bottomRight.dataSync()[0] - topLeft.dataSync()[0]) * scale.width;
        const height =
          (bottomRight.dataSync()[1] - topLeft.dataSync()[1]) * scale.height;
        const crop = tf.image.cropAndResize(
          tensorReshaped,
          boxes,
          [0],
          [height, width]
        );
        console.log(crop);
      });
    }

盒子

Tensor {
  "dataId": Object {},
  "dtype": "float32",
  "id": 63582,
  "isDisposedInternal": false,
  "kept": false,
  "rankType": "2",
  "scopeId": 119566,
  "shape": Array [
    1,
    4,
  ],
  "size": 4,
  "strides": Array [
    4,
  ],
}

最佳答案

问题出在宽度和高度缩放导致浮点值。 width 和 height 用于裁剪张量的形状,因此应该是整数。您可以使用 Math.RoundMath.floor 来获取整数值

关于javascript - 无法裁剪图像,错误为 : failed to compile fragment shader Tensorflow. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63914731/

相关文章:

javascript - 无法使用 JavaScript 从数组中删除重复值

javascript - 如何在javascript中编辑css

reactjs - 如何更新 React.js 中的元标记?

reactjs - 如何将展开图标放置在 Accordion 的右侧,同时将文本保持在中心?

javascript - React Native - 缓存 DOM?

reactjs - React Native ScrollView/FlatList 不滚动

react-native - 如何在 native react 中制作动态复选框

javascript - 从 python 调用外部 javascript 函数

javascript - 无法在 JavaScript 对象字面量中定义变量

javascript - 如何管理 CSS 框架以在我的 React 应用程序中仅使用一个组件