cameraWithTensors 应该开始将传入视频中的帧发送到处理相机输入的函数。问题是该函数仅在我导航到页面并再次保存代码时才被调用,没有这个函数永远不会被调用。这很奇怪,因为没有记录错误。
cameraWithTensor 帧被传递给 handleCameraStream
<TensorCamera
style={styles.camera}
type={type}
zoom={0}
cameraTextureHeight={textureDimsState.height}
cameraTextureWidth={textureDimsState.width}
resizeHeight={tensorDims.height}
resizeWidth={tensorDims.width}
resizeDepth={tensorDims.depth}
onReady={imageAsTensors => handleCameraStream(imageAsTensors)}
autorender={AUTORENDER}
/>
handleCameraStream 将来自 TensorCamera 的帧作为输入,并不断循环对帧进行预测
blazeFaceModel 和 loadedModel 最初设置为 null ,当加载两个模型时,我从 useEffect() 设置它们的值
const handleCameraStream = imageAsTensors => {
if (!imageAsTensors) {
console.log("Image not found!");
}
const loop = async () => {
const imageTensor = imageAsTensors.next().value;
if (loadedModel !== null && blazeFaceModel !== null) {
console.log("Started");
await getPrediction(imageTensor).catch(e => console.log(e));
}
tf.dispose(imageAsTensors);
requestAnimationFrameId = requestAnimationFrame(loop);
};
//loop infinitely to constantly make predictions
loop();
};
useEffect() 包含我最初需要设置的数据,这里设置了loadedModel和blazeFaceModel
useEffect(() => {
if (!frameWorkReady) {
(async () => {
const { status } = await Camera.requestPermissionsAsync().catch(e =>
console.log(e)
);
if (Platform.OS == "ios") {
setTextureDims({ height: 1920, width: 1080 });
} else {
setTextureDims({ height: 1200, width: 1600 });
}
setHasPermission(status === "granted");
await tf.ready().catch(e => console.log(e));
setTFReady(true);
setModelLoaded(await loadModel().catch(e => console.log(e)));
setBlazeFaceModel(
await loadBlazeFaceModel().catch(e => console.log(e))
);
setFrameWorkReady(true);
})();
}
}, []);
我想只添加一个按钮来刷新页面,但如果我不必这样做,我会更喜欢。