【问题标题】:Error while trying to access files in the app尝试访问应用程序中的文件时出错
【发布时间】:2018-04-10 17:32:01
【问题描述】:

我正在构建一个使用 tensorflow 识别图像的 react-native 应用程序,我正在按照tutorial 中的步骤操作。

我按照说明做了一切,包括“获取文件”部分。我创建了assets文件夹并将文件放入其中(路径正确)。

但是当我运行这段代码时:

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

应用程序出现以下错误:

我已经尝试创建一个新项目,我导入了 react-native-tensorflow import { TfImageRecognition } from 'react-native-tensorflow';,我更新了缓存,我删除了文件夹 node_modules 并创建了文件“rn-cli.config。 js”,在教程中要求访问资产文件夹中的文件。知道如何解决这个问题吗?

我正在使用 expo 在移动设备 (android) 上运行应用程序。

  • npm: 5.51
  • 世博会:51.4.0
  • 反应原生:0.54.0
  • react-native-cli: 2.0.1

【问题讨论】:

  • 您确定“./assets/tensorflow_inception_graph.pb”中的“.”指的是您希望它指的同一个目录吗?您可以尝试打印/验证根目录...或尝试输入完整的文件路径以进行故障排除。这似乎是我唯一的解释。
  • 你可以尝试添加 var modelFile = require('./assets/tensorflow_inception_graph.pb');和 var labelFile = require('./assets/tensorflow_labels.txt');然后在模型和标签属性中分配这些变量。

标签: android reactjs react-native tensorflow expo


【解决方案1】:

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

【讨论】:

  • 我尝试了这个并继续同样的错误,我已经尝试创建一个新项目并且错误继续,非常感谢您的关注。
【解决方案2】:

有更好的方法。

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

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

重启你的服务器。

【讨论】:

    【解决方案3】:

    我尝试了与您提到的相同示例,我访问了图像和文本。 我将文件存储在同一目录中的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,这个库需要本地更改

    【讨论】:

    • 感谢您的关注,我会尽快测试您的提示。如果您想看看code 甚至project,欢迎您
    【解决方案4】:

    您必须在您的 rn-cli.config.js 中添加扩展,以便需要 tensorflow_inception_graph.pb 和 tensorflow_labels.txt

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

    【讨论】:

      【解决方案5】:

      将 ./ 替换为 ../ ,因此最终代码将是 -

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        相关资源
        最近更新 更多