世博会/ RN-获取人脸地标

时间:2018-11-02 11:18:13

标签: javascript node.js react-native expo

我有一个基于官方文档中简单示例的工作代码: https://docs.expo.io/versions/latest/sdk/camerahttps://docs.expo.io/versions/latest/sdk/facedetector

据说“ 在检测人脸时,FaceDetector将发出以下形状的对象事件...

但是我不明白如何访问这些对象的值。尝试了所有可能的组合-没有成功。我做错了什么?有人可以帮忙吗?

在检测到人脸但无法访问数据时调用功能“ handleFacesDetected ”。

这是代码:

import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Camera, Permissions, FaceDetector } from 'expo';

export default class CameraExample extends React.Component {
  state = {
    hasCameraPermission: null,
    type: Camera.Constants.Type.back,
  };

  async componentWillMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === 'granted' });
  }

  handleFacesDetected(){
     // it gets here while the face is detected. how to access the data?
  }

  render() {
    const { hasCameraPermission } = this.state;
    if (hasCameraPermission === null) {
      return <View />;
    } else if (hasCameraPermission === false) {
      return <Text>No access to camera</Text>;
    } else {
      return (
        <View style={{ flex: 1 }}>
          <Camera
            style={{ flex: 1 }}
            type={this.state.type}
            onFacesDetected={this.handleFacesDetected}
            faceDetectorSettings={{
               mode: FaceDetector.Constants.Mode.accurate,
               detectLandmarks: FaceDetector.Constants.Mode.all,
               runClassifications: FaceDetector.Constants.Mode.all,
            }}
          >
            <View
              style={{
                flex: 1,
                backgroundColor: 'transparent',
                flexDirection: 'row',
              }}>
              <TouchableOpacity
                style={{
                  flex: 0.1,
                  alignSelf: 'flex-end',
                  alignItems: 'center',
                }}
                onPress={() => {
                  this.setState({
                    type: this.state.type === Camera.Constants.Type.back
                      ? Camera.Constants.Type.front
                      : Camera.Constants.Type.back,
                  });
                }}>
                <Text
                  style={{ fontSize: 18, marginBottom: 10, color: 'white' }}>
                  {' '}Flip{' '}
                </Text>
              </TouchableOpacity>
            </View>
          </Camera>
        </View>
      );
    }
  }
}

1 个答案:

答案 0 :(得分:1)

使用此

handleFacesDetected  = async ({ faces }) => {
    if(faces.length === 1){
      this.setState({ face: true });
    }
  }