尝试使用TFjs和React Native进行实时对象检测,总是给出相同的预测结果,并且在打开摄像头时停止

当摄像头打开时,会出现一个空白的摄像头画面几秒钟,然后总是给出下面的相同输出并停止。

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();  };

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注