I'm trying to follow the 'Usage' example in this repo: https://github.com/rhdeck/react-native-coreml
And for reference to RNFS:https://github.com/itinance/react-native-fs
So my code is fairly similar:
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', 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); }};
And my output is
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')]
So my RNFS.downloadFile() seems to have worked since I have a jobId, but I don't have a path to my file since my file "doesn't exist". Any help or suggestions would be appreciated!
UPDATE 1I read the docs further and the Promise returned by RNFS.downloadFile();
has a member called statusCode. I used that and saw my download is statusCode 200. So I then manually downloaded the model to see if the file is correct. I tested the model and it is indeed functional. RNFS.readDir()
returns true for isFile, and false for isDirectory. So I think my file is downloaded properly. I need help figuring out why compileModel(path) is returning an error or how to make it run smoothly.
UPDATE 2I've simplified the code to the following:
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, confidence} = await classifyTopValue(pathToImage, modelPath); // console.log('The image is a '+ label +'. I think. '); } catch (error) { console.log(error); }};
output:
LOG promise 200 LOG [TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[5], "react-native-coreml").compileModel')]
UPDATE 3
I added in the following:
const exists = await RNFS.exists(`${RNFS.MainBundlePath}/MobileNetV2.mlmodelc`);console.log('exists', exists);
and it returned true. But compileModel() still doesn't work.