Error when implementing TensorFlow in React Native

0

Save! I'm trying to implement the TensorFlow library in my React Native application, based on ese e in this tutorials.

But I can not even run the project, getting the following error:

  

Unable to resolve module ../assets/graph.pb from C:\aplicativos\myApp\components\Index.js : The module ../assets/graph.pb could not be found.

The error only occurs because of file extension ( .pb ), because testing with image files in the same directory all happens well.

I then tried the approach made by this StackOverflow response , also with no result and the error persists. Finally, I tried to read the file .pb with the react-native-fs , resulting in a memory allocation error, since the file in question weighs more than 80Mb.

My code looks like this:

import { TfImageRecognition, TensorFlow } from 'react-native-tensorflow';

clickHandle = () => {

    const graph = require('../assets/graph.pb'); //gera erro
    const text = require('../assets/a.txt'); //gera erro

    try {
      const tfImageRecognition = new TfImageRecognition({
        model: graph,
        labels: text
      });

      const results = await tfImageRecognition.recognize({
        image: this.image
      });

      const resultText = 'Name: ${results[0].name} - Confidence: ${results[0].confidence}';
      this.setState({texto: resultText});

      await tfImageRecognition.close();
    } catch(err) {
      alert(err);
    }
 }

Folder structure:

root
|--assets
|      |_ a.txt
|      |_ graph.pb
|
|--components
|      |_ Index.js

EDIT:

I made a copy of the files to the android/app/src/main/assets folder and modified the code as follows:

clickHandle = async () => {

    const text = {uri: 'asset:/a.txt'};
    const graph = {uri: 'asset:/graph.pb'};

    try {
      const tfImageRecognition = new TfImageRecognition({
        model: graph,
        labels: text
      });

      const results = await tfImageRecognition.recognize({
        image: require('../assets/YodaWhiteHouse.jpg')
      });

      const resultText = 'Name: ${results[0].name} - Confidence: ${results[0].confidence}';
      this.setState({texto: resultText});

      await tfImageRecognition.close();
    } catch(err) {
      alert(err);
    }

Resulting in error:

    
asked by anonymous 30.10.2018 / 17:03

1 answer

0

I was able to solve the problem in question after analyzing the ResourceManager.java file contained in the node_modules\react-native-tensorflow\android\src\main\java\com\rntensorflow folder.

The new code looks like this:

clickHandle = async () => {

const text = 'a.txt';
const graph = 'graph.pb';

try {
  const tfImageRecognition = new TfImageRecognition({
    model: graph,
    labels: text
  });

  const results = await tfImageRecognition.recognize({
    image: require('./assets/yodawhitehouse.jpg')
  });

  const resultText = 'Name: ${results[0].name} - Confidence: ${results[0].confidence}';
  this.setState({texto: resultText});

  await tfImageRecognition.close();
} catch(err) {
  alert(err);
}

}

with the files txt and pb within the android/app/src/main/assets folder.

Now I get an error related to Graph, but stay for another question, the problem of that has already been solved.

    
06.11.2018 / 14:40