javascript - 无法裁剪图像并出现错误:无法编译片段着色器 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,
],
}
解决方案
问题来自缩放导致浮点值的宽度和高度。宽度和高度用于裁剪张量的形状,因此应该是整数。您可以使用Math.Round
或Math.floor
获取整数值
推荐阅读
- css - 网站在手机上处理背景图片
- django - 一个ManyToMany字段可以包含2个表吗?-- 姜戈
- javascript - JavaScript TypeError:无法读取未定义的属性“startsWith” - 不和谐机器人
- android - 检测是否添加了新键盘
- ios - 在 UITableViewCell 中获取自定义单元格的高度
- android - 如何按布尔值对recyclerview进行排序
- couchbase - 标记为删除的 Couchbase 数据
- r - 根据跨多列的条件将 df 中的值输入到新变量中
- php - 如何将图像移动到另一台服务器 PHP
- java - 打印多个字符与打印一个字符串