gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:31:46 25 4
gpt4 key购买 nike

可能未处理的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com