如何解决尽管提供了现有路径,但仍无法使用 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) 返回错误或如何让它顺利运行。
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 举报,一经查实,本站将立刻删除。