可能的未处理的承诺拒绝(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 上执行的代码相同,但在我的情况下不起作用。我尝试了很多方法但无法解决此错误。
这是我的源代码:
[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]