尽管提供了现有路径,但仍无法使用 RNFS 读取文件

问题描述

我正在尝试遵循此存储库中的“用法”示例:https://github.com/rhdeck/react-native-coreml

对于 RNFS 的参考: https://github.com/itinance/react-native-fs

所以我的代码非常相似:

import {compileModel,classifyTopValue} from 'react-native-coreml';
const NetworkMLModelPath =
  'https://github.com/hollance/MobileNet-CoreML/raw/master/MobileNet.mlmodel';

const coreml = async (pathToImage: string) => {
  try {
    const {jobId,promise} = RNFS.downloadFile({
      fromUrl: NetworkMLModelPath,toFile: `${RNFS.DocumentDirectoryPath}MobileNetV2.mlmodel`,});

    await promise;
    console.log('jobId',jobId);

    const [{name,path,isFile,isDirectory}] = await RNFS.readDir(
      `${RNFS.DocumentDirectoryPath}`,);
    console.log('name',name,'path',isFile(),isDirectory());

    const modelPath = await compileModel(path);

    // const {label,confidence} = await classifyTopValue(pathToImage,modelPath);
    // console.log('The image is a ' + label + '. I think. ');
  } catch (error) {
    console.log(error);
  }
};

我的输出

 LOG  jobId 1
 LOG  name MobileNetV2.mlmodel path /var/mobile/Containers/Data/Application/4A549020-958B-4B83-99EE-782E4DAAEA9C/Documents/MobileNetV2.mlmodel true false
 ERROR  TypeError: null is not an object (evaluating 'RNCoreML.mainBundlePath')
 LOG  [TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[6],"react-native-coreml").compileModel')]

所以我的 RNFS.downloadFile() 似乎工作正常,因为我有一个 jobId,但我没有我的文件的路径,因为我的文件“不存在”。任何帮助或建议将不胜感激!

更新 1 我进一步阅读了文档,RNFS.downloadFile(); 返回的 Promise 有一个名为 statusCode 的成员。我使用它并看到我的下载是 statusCode 200。所以我然后手动下载模型以查看文件是否正确。我测试了模型,它确实可以正常工作。 RNFS.readDir() 为 isFile 返回 true,为 isDirectory 返回 false。所以我认为我的文件下载正确。我需要帮助弄清楚为什么 compileModel(path) 返回错误或如何让它顺利运行。

更新 2 我已将代码简化为以下内容

import {compileModel,classifyTopValue} from 'react-native-coreml';
const NetworkMLModelPath =
  'https://github.com/hollance/MobileNet-CoreML/raw/master/MobileNet.mlmodel';

const coreml = async (pathToImage: string) => {
  const path = 'MobileNetV2.mlmodel';
  try {
    const {promise} = await RNFS.downloadFile({
      fromUrl: NetworkMLModelPath,toFile: RNFS.MainBundlePath + path,});

    console.log('promise',(await promise).statusCode);

    const modelPath = await compileModel(path);

    // const {label,modelPath);
    // console.log('The image is a ' + label + '. I think. ');
  } catch (error) {
    console.log(error);
  }
};

输出

 LOG  promise 200
 LOG  [TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[5],"react-native-coreml").compileModel')]

更新 3

添加了以下内容

const exists = await RNFS.exists(`${RNFS.MainBundlePath}/MobileNetV2.mlmodelc`);
console.log('exists',exists);

它返回true。但是 compileModel() 仍然不起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)