【发布时间】:2020-12-31 13:00:45
【问题描述】:
我需要裁剪在 BlazeFace 模型中检测到的人脸,然后将图像发送到我制作的自定义模型。我已经使用边界框实现了人脸检测,但一直无法裁剪人脸。
我有地标和 bottomRight 和 topLeft 的坐标,但我不知道该怎么做。在带有 tensorflow 的 python 中,它们存在这样做的函数,但是对于 tensorflow.js,我找不到任何东西。
在面部渲染边界框
const faces = await bfModel
.estimateFaces(tensor, returnTensors)
.catch(e => console.log(e));
console.log(faces);
// Faces is an array of objects
if (!isEmpty(faces)) {
setModelFaces({ faces });
}
const renderBoundingBoxes = () => {
const { faces } = modelFaces;
const scale = {
height: styles.camera.height / tensorDims.height,
width: styles.camera.width / tensorDims.width
};
const flipHorizontal = Platform.OS === "ios" ? false : true;
if (!isEmpty(faces)) {
return faces.map((face, i) => {
const { topLeft, bottomRight } = face;
const bbLeft = topLeft.dataSync()[0] * scale.width;
const boxStyle = Object.assign({}, styles.bbox, {
left: flipHorizontal
? previewWidth - bbLeft - previewLeft
: bbLeft + previewLeft,
top: topLeft.dataSync()[1] * scale.height + 20,
width:
(bottomRight.dataSync()[0] - topLeft.dataSync()[0]) * scale.width,
height:
(bottomRight.dataSync()[1] - topLeft.dataSync()[1]) * scale.height
});
return <View style={boxStyle}></View>;
1;
});
}
};
console.log(faces) 的输出:
Array [
Object {
"bottomRight": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 220600,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 426282,
"shape": Array [
2,
],
"size": 2,
"strides": Array [],
},
"landmarks": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 220602,
"isDisposedInternal": false,
"kept": false,
"rankType": "2",
"scopeId": 426286,
"shape": Array [
6,
2,
],
"size": 12,
"strides": Array [
2,
],
},
"probability": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 220592,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 426249,
"shape": Array [
1,
],
"size": 1,
"strides": Array [],
},
"topLeft": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 220599,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 426280,
"shape": Array [
2,
],
"size": 2,
"strides": Array [],
},
},
]
【问题讨论】:
-
哪条线正在裁剪面部?我猜
tensor包含图像,对吧? -
我没有进行裁剪,因为我不知道如何裁剪它。但是 faces 包含对象数组,例如地标、概率、topLeft 和 bottomRight。我用它们来绘制边界框。
-
是张量具有传递给模型的完整图像。
-
我已经添加了来自 console.log(faces) 的输出
标签: reactjs react-native tensorflow deep-learning tensorflow.js