当摄像头打开时,会出现一个空白的摄像头画面几秒钟,然后总是给出下面的相同输出并停止。
prediction: [{"className":"nematode, nematode worm, roundworm","probability":0.050750732421875},{"className":"matchstick","probability":0.043731689453125},{"className":"lighter, light, igniter, ignitor","probability":0.021453857421875}]
有什么办法可以让实时预测正常工作吗?避免像上面那样只有一次的错误预测
以下是摄像头屏幕的代码,用户在扫描特定环境时,预测应在实时摄像头馈送中进行
export function CameraScreen() { const [word, setWord] = useState(""); const [predictionFound, setPredictionFound] = useState(false); const [hasPermission, setHasPermission] = useState(); const [model, setModel] = useState(); const TensorCamera = cameraWithTensors(Camera); let requestAnimationFrameId = 0; const textureDims = Platform.OS === "ios" ? { width: 1080, height: 1920 } : { width: 1600, height: 1200 }; const tensorDims = { width: 152, height: 200 }; async function loadModel() { try { const model = await mobilenet.load(); setModel(model); console.log("set loaded Model"); } catch (err) { console.log(err); console.log("failed load model"); } } useEffect(() => { (async () => { const { status } = await Camera.requestPermissionsAsync(); setHasPermission(status === "granted"); await tf.ready(); await loadModel(); console.log("after model load"); })(); }, []); const getPrediction = async (tensor) => { if (!tensor) { return; } const prediction = await model.classify(tensor); console.log(`prediction: ${JSON.stringify(prediction)}`); if (!prediction || prediction.length === 0) { cancelAnimationFrame(requestAnimationFrameId); console.log("no predictions found"); setPredictionFound(false); return; } else { setPredictionFound(true); } }; const handleCameraStream = (imageAsTensors) => { const loop = async () => { const nextImageTensor = await imageAsTensors.next().value; await getPrediction(nextImageTensor); requestAnimationFrameId = requestAnimationFrame(loop); }; if (!predictionFound) { loop(); } }; const renderCameraView = () => { return ( <View style={styles.cameraView}> <TensorCamera style={styles.camera} type={Camera.Constants.Type.back} zoom={0} cameraTextureHeight={textureDims.height} cameraTextureWidth={textureDims.width} resizeHeight={tensorDims.height} resizeWidth={tensorDims.width} resizeDepth={3} onReady={handleCameraStream} /> </View> ); }; useEffect(() => { return () => { cancelAnimationFrame(requestAnimationFrameId); }; }, [requestAnimationFrameId]); return ( <View style={styles.container}> <View style={styles.header}> <Text style={styles.title}>My Pictionary</Text> </View> {model ? ( <View style={styles.body}>{renderCameraView()}</View> ) : ( <Text>Still loading</Text> )} </View> );}
回答:
在handleCameraStream
函数中,一旦找到预测结果,你就会停止循环函数。在你的情况下,你希望不断运行循环,因为你想对所有帧进行预测,而不仅仅是一个帧。
const handleCameraStream = (imageAsTensors) => { const loop = async () => { const nextImageTensor = await imageAsTensors.next().value; await getPrediction(nextImageTensor); requestAnimationFrameId = requestAnimationFrame(loop); }; loop(); };