I am trying to make an object detection application using tf.js with React Native. I’m trying to load the model after calling the tf.ready() function and I’m getting an error. I tried locally and using the cocoSsd package.
import React, { useState, useEffect } from "react";
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import * as tf from "@tensorflow/tfjs";
import "@tensorflow/tfjs-react-native";
import { bundleResourceIO } from "@tensorflow/tfjs-react-native";
import * as cocoSsd from '@tensorflow-models/coco-ssd';
const modelJson = require('./assets/model.json')
const modelWeight1 = require('./assets/group1-shard1of2.bin')
const modelWeight2 = require('./assets/group1-shard2of2.bin')
export default function App() {
const [isTfReady, setIsTfReady] = useState(false);
const [model, setModel] = useState();
useEffect(() => {
(async () => {
// Calling setBackend() method
// tf.setBackend("webgl");
// Calling ready() method and
// Printing output
await tf
.ready()
.then((a) => {
//console.log(JSON.stringify(tf.getBackend()));
setIsTfReady(true);
})
.catch((e) => console.log("hataa", e));
const model = await tf.loadGraphModel(bundleResourceIO(modelJson, [modelWeight1, modelWeight2]))
console.log(model);
const model2 = await cocoSsd.load();
console.log(model2);
})();
}, []);
console.log("isTfReady :", isTfReady);
//console.log("model :", model);
// if (!model) {
// return (<View style={styles.container}><Text>No model</Text></View>)
// }
return (
<View style={styles.container}>
<Text>Test app!</Text>
<StatusBar style="auto" />
</View>
);
}
I’m getting this error ;
WARN Possible Unhandled Promise Rejection (id: 0):
TypeError: undefined is not a function
TypeError: undefined is not a function
at isTypedArray (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:130930:39)
at inferShape (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:133020:21)
at tensor (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:133247:35)
at decodeWeights (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:133447:29)
at anonymous (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:201901:44)
at anonymous (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:201881:32)
at tryCallOne (/Users/kudo/01_Work/Repos/expo/expo/android/versioned-react-native/ReactAndroid/hermes-engine/.cxx/MinSizeRel/5a4n485h/arm64-v8a/lib/InternalBytecode/Intern
alBytecode.js:53:16)
at anonymous (/Users/kudo/01_Work/Repos/expo/expo/android/versioned-react-native/ReactAndroid/hermes-engine/.cxx/MinSizeRel/5a4n485h/arm64-v8a/lib/InternalBytecode/Interna
lBytecode.js:139:27)
at apply (native)
at anonymous (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:29651:26)
at _callTimer (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:29570:17)
at _callReactNativeMicrotasksPass (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:29600:17)
at callReactNativeMicrotasks (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:29763:44)
at __callReactNativeMicrotasks (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:2793:46)
at anonymous (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:2605:45)
at __guard (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:2777:15)
at flushedQueue (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:2604:21)
at callFunctionReturnFlushedQueue (http://192.168.0.102:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:2589:33)