尝试访问应用程序文件时出现错误。

5
我正在构建一个React Native应用程序,使用TensorFlow来识别图像,我正在按照这个tutorial中的步骤进行操作。
我按照说明进行了一切操作,包括“获取文件”部分。我创建了资产文件夹并将文件放入其中(路径正确)。
但是当我运行以下代码时:
const tfImageRecognition = new TfImageRecognition({
    model: require('./assets/tensorflow_inception_graph.pb'),
    labels: require('./assets/tensorflow_labels.txt'),
});

该应用程序出现以下错误:

enter image description here

我已经尝试创建一个新项目,导入了react-native-tensorflow import { TfImageRecognition } from 'react-native-tensorflow';,更新了缓存,删除了node_modules文件夹,并创建了文件“rn-cli.config.js”,该文件在教程中要求以便访问assets文件夹中的文件。有什么想法如何解决这个问题吗?
我正在使用expo在移动设备(android)上运行应用程序。
  • npm: 5.51
  • expo: 51.4.0
  • react-native: 0.54.0
  • react-native-cli: 2.0.1

1
你确定 " ./assets/tensorflow_inception_graph.pb " 中的 " . " 指的是你期望的同一个目录吗?你可以尝试打印/验证根目录...或者尝试输入完整的文件路径以进行故障排除。在我看来,这似乎是唯一的解释。 - Nerdy Bunz
你可以尝试添加 var modelFile = require('./assets/tensorflow_inception_graph.pb'); 和 var labelFile = require('./assets/tensorflow_labels.txt'); 然后将这些变量分配到模型和标签属性中。 - Aavgeen singh
5个回答

1

有一种更好的方法来做这件事。

import model from './assets/tensorflow_inception_graph.pb';
import labels from './assets/tensorflow_labels.txt';

const tfImageRecognition = new TfImageRecognition({
    model,
    labels
});

重新启动您的服务器。


1

我尝试了你提到的相同例子,我得到了访问图像和文本。我将文件存储在相同目录下的assets中。你能分享一下你遇到的错误产生的代码吗?

  async recognizeImage() {

    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.image
      })

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

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

根据您提到使用的expo,我假设已经运行了npm eject。由于react-native-tensorflow库需要本地更改,因此需要注意。


感谢您的关注,我会尽快测试您的建议。如果您想查看代码或者整个项目,请访问以下链接:代码 或者 项目 - Mathiasfc

1

为了要求 tensorflow_inception_graph.pb 和 tensorflow_labels.txt,您必须在 rn-cli.config.js 中添加扩展。

module.exports = {
  getAssetExts() {
    return ['pb', 'txt']
  }
}

1

我没有遇到这个问题。尝试运行 react-native start --reset-cache,然后再次运行应用程序。


我尝试了这个,但是仍然出现同样的错误。我已经尝试创建一个新项目,但错误仍然存在。非常感谢您的关注。 - Mathiasfc

0

将 ./ 替换为 ../ ,最终代码如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接