react-native - 我该如何解决这个 Possible Unhandled promise rejection 错误?

标签 react-native tensorflow

可能未处理的 promise 拒绝(id:11): TypeError: undefined is not an object (evaluating 'a.substr') isMobile@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:150239:1956

我正在尝试使用移动网络对图像进行分类并打印结果。我正在使用异步等待,但我的 promise 被拒绝了。这与我尝试从 youtube 执行的代码相同,但在我的情况下不起作用。我尝试了很多方法都无法解决这个错误。

这是我的源代码:

[enter image description here][1]import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';
import * as mobilenet from '@tensorflow-models/mobilenet';
import { fetch, decodeJpeg} from '@tensorflow/tfjs-react-native';
// import { Asset, Constants, FileSystem, Permissions } from 'react-native-unimodules';


import React from 'react';
import { 
    SafeAreaView,
    View, 
    Text,
    TextInput,
    StyleSheet,
    TouchableOpacity,
    Image,
    Button,
    ScrollView,
    FlatList, 
    Touchable} from 'react-native';
import { icons, images, SIZES, COLORS, FONTS } from '../constants';

export default function Trip(){
    const [url, seturl]= React.useState('https://oceana.org/sites/default/files/tiger_shark_0.jpg')
    const [displayText, setDisplayText] = React.useState('loading')

    async function getPrediction(url){
        setDisplayText("Loading Tensor Flow")
        await tf.ready()
        setDisplayText("Loading Mobile Net")
        const model= await mobilenet.load()
        setDisplayText("Fetching image")
        const response =await fetch(url, {}, {isBinary: true})
        setDisplayText("Getting image buffer")
        const imageData = await response.arrayBuffer() 
        setDisplayText("Getting Image tensor")
        const imageTensor = imageToTensor(imageData)
        setDisplayText("Loading Result")
        const prediction = await model.classify(imageTensor)
        console.log(prediction)
        setDisplayText(JSON.stringify(prediction))

    }

    function imageToTensor(rawData){
        const {width, height, data}= jpeg.decode(rawData, true)
        const buffer = new Uint8Array(width*height*3)
        let offset = 0;
        for(let i=0; i<buffer.length; i+=3){
            buffer[i]=data[offset]
            buffer[i+1]=data[offset+1]
            buffer[i+2]=data[offset+2]
            buffer +=4

        }

        return tf.tensor3d(buffer, [height, width, 3])

    }

    return(
        <View style={styles.container1}>
            <Text>Works with only JPEG Images</Text>
            <TextInput style={{height:40, width:"90%", borderColor:'gray', borderWidth: 1}}
                onChangeText={text => seturl(text) }
                value={url}/>
            <Image style={styles.imageStyle} source={{uri:url}}></Image>
            <Button title="Classify" onPress={()=>getPrediction(url)}></Button>
        </View>
    )
}


[Mobile error pic][1]
       
         

最佳答案

This is a bug with tfjs-react-native .如果将 @tensorflow/tfjs 降级到 3.0.0,它应该可以工作。

关于react-native - 我该如何解决这个 Possible Unhandled promise rejection 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66647388/

相关文章:

python - 没有名为 'uff' 的模块

react-native - 使用 Flex 时图像消失

android - react native 任务 :app:multiDexListDebug FAILED

react-native - 使用 react-native-community/datetimepicker,如何在 Android 上显示日期时间选择器?

reactjs - Axios 发布请求在 React Native 中不起作用

c++ - 如何使用 Clion 或 Netbeans 正确导入 Tensorflow 源代码

javascript - 需要帮助尝试在 native react 中刷新我的 token

python - 为什么张量板中没有此 DNN 二元分类器 tf.estimator 模型的评估图?

indexing - Tensorflow 相当于 Numpy 的 array[indices] = scalar

python - 在 Tensorboard Projector 中可视化 Gensim Word2vec 嵌入