【发布时间】:2021-11-05 12:28:40
【问题描述】:
我使用可教机器创建了一个 tensorflow 模型,并希望在 react native 中实现它。我使用 cameraWithTensor 来获取这里的输入是相机视图
<TensorCamera
// Standard Camera props
style={styles.camera}
type={Camera.Constants.Type.front}
// Tensor related props
cameraTextureHeight={textureDims.height}
cameraTextureWidth={textureDims.width}
resizeHeight={320}
resizeWidth={240}
resizeDepth={3}
onReady={makeHandleCameraStream()}
autorender={true}
/>
这里是makeHandleCameraStream 函数
const makeHandleCameraStream = ()=> {
return (images, updatePreview, gl) => {
const loop = async () => {
const nextImageTensor = images.next().value;
try {
// const predictions = await model.estimateHands(nextImageTensor);
const predictions = await model.predict(nextImageTensor); //this is the line where it breaks
console.log(predictions)
setPredictions(predictions)
} catch (error) {
// console.log(error.message)
}
requestAnimationFrame(loop);
};
loop();
};
}
这是我尝试使用model.predict时遇到的错误
Error when checking : expected input_1 to have 4 dimension(s), but got array with shape [320,240,3]
尝试更改这两行
let expandedImageTensor = tf.expandDims(nextImageTensor,0)
// error encountered without using reshape :
// Error when checking : expected input_1 to have shape [null,224,224,3] but got array with shape [1,320,240,3]
const predictions = await model.predict(expandedImageTensor.reshape([null,240,240,3]));
//error after adding .reshape : Size(230400) must match the product of shape ,240,240,3
【问题讨论】:
标签: react-native tensorflow machine-learning tensor tensorflow.js