【发布时间】:2020-10-22 06:08:55
【问题描述】:
我正在使用 react native 0.63 开发人脸识别应用程序。我正在使用react-native run-android 运行我的项目。我得到未定义的组件异常不是对象(评估'_this')。我是本机反应的新手,我不明白这个错误的含义。
我正在关注这个应用程序的教程,但教程很旧,这就是为什么我无法将代码更新到最新版本的 react native。这是教程的链接
Face Recognition using react native。请查看并解决我的问题。
import React from 'react';
import {StyleSheet,Text,View,Image} from 'react-native';
import NativeModules, { ImagePickerManager } from 'react-native';
import Button from './Button';
import RNFetchBlob from 'react-native-fetch-blob';
import _ from 'lodash';
const Detector = props => {
this.state = {
photo_style: {
position: 'relative',
width: 480,
height: 480
},
has_photo: false,
photo: null,
face_data: null
};
return (
<View style={styles.container}>
<Image
style={this.state.photo_style}
source={this.state.photo}
resizeMode={"contain"}
>
{ this._renderFaceBoxes.call(this) }
</Image>
<Button
title="Pick Photo"
onPress={()=>{this._pickImage.bind(this)}}
button_styles={styles.button}
button_text_styles={styles.button_text} />
{ this._renderDetectFacesButton.call(this) }
</View>
);
}
const _pickImage = () => {
this.setState({
face_data: null
});
ImagePickerManager.showImagePicker(this.props.imagePickerOptions, (response) => {
if(response.error){
alert('Error getting the image. Please try again.');
}else{
let source = {uri: response.uri};
this.setState({
photo_style: {
position: 'relative',
width: response.width,
height: response.height
},
has_photo: true,
photo: source,
photo_data: response.data
});
}
});
}
const _renderDetectFacesButton = () => {
if(this.state.has_photo){
return (
<Button
title="Detect Faces"
onPress={()=>{this._detectFaces.bind(this)}}
button_styles={styles.button}
button_text_styles={styles.button_text} />
);
}
}
const _detectFaces = () => {
RNFetchBlob.fetch('POST', 'https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceAttributes=age,gender', {
'Accept': 'application/json',
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': this.props.apiKey
}, this.state.photo_data)
.then((res) => {
return res.json();
})
.then((json) => {
if(json.length){
this.setState({
face_data: json
});
}else{
alert("Sorry, I can't see any faces in there.");
}
return json;
})
.catch (function (error) {
console.log(error);
alert('Sorry, the request failed. Please try again.' + JSON.stringify(error));
});
}
const _renderFaceBoxes = () => {
if(this.state.face_data){
let views = _.map(this.state.face_data, (x) => {
let box = {
position: 'absolute',
top: x.faceRectangle.top,
left: x.faceRectangle.left
};
let style = {
width: x.faceRectangle.width,
height: x.faceRectangle.height,
borderWidth: 2,
borderColor: '#fff',
};
let attr = {
color: '#fff',
};
return (
<View key={x.faceId} style={box}>
<View style={style}></View>
<Text style={attr}>{x.faceAttributes.gender}, {x.faceAttributes.age} y/o</Text>
</View>
);
});
return <View>{views}</View>
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
alignSelf: 'center',
backgroundColor: '#ccc'
},
button: {
margin: 10,
padding: 15,
backgroundColor: '#529ecc'
},
button_text: {
color: '#FFF',
fontSize: 20
}
});
export default Detector
【问题讨论】: