reneweb / react-native-tensorflow

A TensorFlow inference library for react native
Apache License 2.0
358 stars 59 forks source link

Undefined is not an object (tensorflow ImageRecognition) #29

Closed SpQuyt closed 6 years ago

SpQuyt commented 6 years ago

When trying to integrate a pretrained tensorflow model with expo (react-native), the following error occurs within these lines:

   _graph = async () => {

        var preder2 = null;
        var items = "";
        this.setState({result: "", value: null});

        let result = await ImagePicker.launchImageLibraryAsync({
            allowsEditing: true,
            aspect: [4, 3],
            base64: true,
        });

        if (!result.cancelled) {
            this.setState({ image: result});
        };

        try {
            const tfImageRecognition = new TfImageRecognition({
                model: require('./assets/tensorflow_inception_graph.pb'),
                labels: require('./assets/tensorflow_labels.txt')
            });

            const results = await tfImageRecognition.recognize({
              image: this.state.image
            }); 
            results.forEach(
              result => ((preder2 = result.confidence), (items = result.name))
            );
            await tfImageRecognition.close();  
            this.setState({
              result: items,
              value: preder2 * 100 + "%"
            });
            console.log(this.state.result);
          } catch (err) {
            this.setState({
              result: "No Internet",
              value: "Please connect to the internet"
            });
            console.log(err);
          }
    }

Which generates the following error

10:10:40 AM: undefined is not an object (evaluating 'RNImageRecognition.initImageRecognizer')

I have been trying to find the reason why this is not working but I cannot find a definite solution. The relative paths linking to the assets are correct and the extensions are present in the app.json. Furthermore the model is trained using the tensorflow api which should make it compatible with the react-native implementation.

I observed that after running

const tfImageRecognition = new TfImageRecognition({

                model: require('./assets/tensorflow_inception_graph.pb'),
                labels: require('./assets/tensorflow_labels.txt')

            });

The code changed immediately to "catch(err)" branch, which means it could not load the model and labels?

I am using expo SDK version 28.0.0, Expo XDE and react-native-tensorflow version ^0.1.8

NazerkeT commented 6 years ago

I am struggling with the same problem as well. Is there somebody to help?

SpQuyt commented 6 years ago

Well I stopped using expo and then I used react-native init, then react-native run-android. And it worked. I don't know why but the most suitable answer for this is that this library cannot be used in expo?

Isti01 commented 6 years ago

I ran into the same problem with the react-native init then react-native run-android method. The relative path links are correct too. The package versions that i am using:

"react": "16.3.1",
"react-native": "0.55.4",
"react-native-tensorflow": "0.1.8"
Isti01 commented 6 years ago

I accidentally forgot to link the package. $ react-native link react-native-tensorflow maybe you too.

BNazh commented 6 years ago

Good part of the day! By the way, I am using react native init with react-native run-ios. I am pretty sure that I have linked all packages with react native link as well, as I have responding message on successful linkage as follows: rnpm-install info Platform 'ios' module react-native-tensorflow is already linked rnpm-install info Platform 'android' module react-native-tensorflow is already linked Pod is already installed and I have absolutely copied and past example content for checking purposes. However, still I have the issue with: TypeError: undefined is not an object(evaluating 'RNImageRecognition.init-ImagerRecognizer')

Any help, hint, advise is appreciated!

UPDATE I guess the problem is with cocoa pods. I have installed its using following commands: pod remove repo master pod setup pod install

What could I do?

reneweb commented 6 years ago

sorry for late reply - @SpQuyt it is indeed not possible to use native modules with expo, thus this lib can't be used with it

SpQuyt commented 6 years ago

@reneweb thanks for your reply. I have changed from expo to react-native init and it's all good.