我该如何解决这个可能的未处理的承诺拒绝错误? [英] How can i solve this Possible Unhandled promise rejection error?

查看:71
本文介绍了我该如何解决这个可能的未处理的承诺拒绝错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的未处理的承诺拒绝(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

Possible Unhandled Promise Rejection (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

我正在尝试使用移动网络对图像进行分类并打印结果.我正在使用 async-await 并且我的承诺被拒绝.这与我试图从 youtube 上执行的代码相同,但在我的情况下不起作用.我尝试了很多方法但无法解决此错误.

I am trying to classify the image using mobile net and print the result. I am using async-await and my promise is rejected. This is the same code I am trying to do from youtube but it doesn't work in my case. I tried many ways but unable to solve this error.

这是我的源代码:

[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]
       
         

推荐答案

这是一个错误tfjs-react-native.如果您将@tensorflow/tfjs 降级到 3.0.0,它应该可以工作.

This is a bug with tfjs-react-native. If you downgrade @tensorflow/tfjs to 3.0.0, it should work.

这篇关于我该如何解决这个可能的未处理的承诺拒绝错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆